I have a paragraph with id = story and I want to change its text dynamically using a servlet. How do I do this? I'm new and using getWriter().println() seems to create a new document instead of appending to the existing one.
Thanks
Simple answer - you can't.
Server-side code cannot change a response which has already been sent to the client.
To change text inside an HTML tag, use Javascript on the browser.
See http://www.w3schools.com/ajax/
Related
I need an input in my html file (I think via javascript) and I need to parse in my Java file.
To get user input in html, it's easy, but I don't know how to parse in my java file.
Any suggestions or tutorials? Thank you!
You can use jsoup like this.
String url = "https://stackoverflow.com/questions/50083471/input-in-html-to-java-file";
Document doc = Jsoup.parse(new URL(url), 3000);
System.out.println("title: " + doc.select("title").text());
System.out.println("users:");
for (Element e : doc.select("div.user-details a"))
System.out.println(" " + e.text());
This code parse this page and print title and user names.
result:
title: Input in html to java file - Stack Overflow
users:
l1den
truekiller
Ethan Three
saka1029
Ceate a form in html and when you submit call a servlet to handle the request. In servlet receive the value you want to handle by using request.getParameter(name) . There are many framework for web MVC like struts2 , spring MVC .
Yes, there are way many way to do it.
The simplest apporach is using html form, your html should include javascript that is able to submit a form (the data from user) to server (POST to an url) as a request.
and then server should program to response the request (the url you submit to).
and then you pickup a framework (serverlet is simple one, but if you have an existing project, check how it process form data).
Using java servlet i need to do a back-end DB query and populate the results in an excel file and provide to client user.
I have a working code of downloading Excel by setting the HtmlServletResponse object's contentType and Header like below:
response.setContentType("application/vnd.ms-excel; charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename="+ "Report" + ".xls");
But my question is in addition to providing this Excel sheet as a download i also need to send the 'initial search criteria' selected by the user as a HTML. And for html, i need to set the content type like below.
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
I think it's not possible to set the content type to 2 different values. How to solve this problem?
To phrase the problem in a different way -- "I have a HTML search form based on which user selects a search criteria - While providing the results in an excel save as file, i need to repopulate the same html so that search criteria selected by user is not lost".
I am new to Servlets and not sure if this is very straight forward. Thanks for the help.
You can not respond to the Service Request with more than one response. You'd need to show the search page with the search criterea and have the search page create the excel-file (in an iframe or something).
You can try to save your search criteria in cookies.
Here is some info about it http://www.journaldev.com/1907/java-servlet-session-management-tutorial-with-examples-of-cookies-httpsession-and-url-rewriting
I am stuck in a Java application.
I have a doubt that is there any way to convert HTML template to DOC Template with Image in HTML file using Java.
I have tried Aspose API but I cant use it because it is not open.
I fetch HTML template from database and store the whole template into string and now I want this string output in a WORD DOC including the images.
Here is my piece of code:
proc_stmt = con.prepareCall("{call PROCEDURECALL(?)}");
proc_stmt.registerOutParameter(1, Types.CLOB);
proc_stmt.execute();
String htmltemplate = proc_stmt.getString(1);
I am storing the HTML template in a String and now I want it to be converted in WORD DOC.
It also have a image src=local path link.The whole template is working fine but the image is not being posted so can anyone help me with it?
Thank you all for the time and help.
I tried docjx4j API 2.8.1 and it wors like wonder.
It had ConvertinXHTMLinFile and it works fine.
If anyone wants the code I will post it.
Here is the link that helped me :
https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/samples/ConvertInXHTMLFile.java
Once again, Thank you all.
Vrinda.
I have a Jsp page and I want automaticly update one of the form on it using Js. Can somebody suggest something
Thanks)
You can dynamically update the elements of the form using basic JavaScript, if thats what you are looking for. Here are some dirty examples:
Eg.
If the id of your form is myForm, you can use
document.forms["myform"].action = "somepage"; //to change the action
var elem1 = document.getElementById("elementID")` //to get an element
var elem2 = document.forms["myform"].element //other way to get an element
childElement = document.createElement("option"); //to create a new element
myform.appendChild(childElement); //to append some child-element to the form
etc. The values/attributes/styles can be changed for the elements too using simple JavaScript. Any JavaScript tutorial on the internet should be helpful.
Have a look at the jQuery library (http://jquery.com/), that will allow you to manipulate HTML elements on the page, including the form element and it's children to automatically update them however you please :)
For an update from server side i.e Data from database or some other file on server use ajax
Now it depends on you to go for javascript or jquery to make this work.
Google this you can find good solutions.
I need to know if there is a way in java/servlet to make documents(doc,pdf) stored in database available for download to users in requested way(please see below),
for example there is a web page and the link for document in it
right now it is done this way:
if the user clicks that link than a new blank window opens and there the download dialog box is shown and the user is able to download the document but that blank window stays open
and the user have to close it manually
but wish to do it this way:
If the User clicks that link than directly staying on that page a download dialog box should show up asking them to save the file
a servlet url handles the download of the document which is responsible for extracting the doc form database and makes available for download to users
thank you for your time and effort
You need to add following headers in your servlet to make it a downloadable content so browsers don't try to display it,
String value = "attachment;filename=\"" + URLEncoder.encode(filename, "UTF-8") +'"';
response.setHeader("Content-Disposition", value);
response.setHeader("Content-Transfer-Encoding", "binary");
The filename is proposed filename and user can change it.
I wonder if your link html doesn't have something like:
<a href="/foo" **target="_blank"** ....>download</href>
Otherwise, it should work as you want.
This is a bug in IE which depends on several things, the content type is one of them. We had the same problem a few years ago but I don't remember the correct solution anymore, only that we struggled with this for quite some time. Try this:
Use the correct content type (application/pdf)
If that doesn't work, use a wrong file type (like application/octet-stream) which should tell IE to leave the file alone. You may have problems with the file extension, though.
Send or don't send the correct file size
Check which chunking mode you're using.
One of these things made IE behave. Good luck.
You need to remove target="_blank" from your <a> element.
Edit: as per your comments: you need to set Content-Disposition header to attachment. You can find here examples of a simple fileservlet and an advanced fileservlet to get some insights.