10/27/2019 Jsp Ajax File Upload Example
Gregg Bolinger wrote: Steve Dyke wrote:It appears that the code in the head tag of the child div JSP is not firing. Hm, when pulling snippets of code into a JSP via jquery, I usually don't have a head tag. An HTML page should only contain 1. That might be your problem. Ha, the part of your code you didn't show us. I don't understand. On my main(parent) JSP page I have a div.
![]()
I use jQuery.load to fire a Servlet which pushes a repsponse to a JSP thus populating the div. Are you saying the second JSP does not need a head tag? And if so where do I put a required taglib if the page is using JSTL? Steve Dyke wrote:Worked great. I think it must have been the $('#ft') that it could not find.
Anyway, thanks for the help. Well, part of the problem is that your jquery selector for the form is on the parent JSP. Any future DOM elements that come into existence won't matter. You'd have to trigger that code once the new stuff comes in. There is a $.live feature to help with this as well as a liveQuery plugin. But that is somewhat beside the point. I just wanted to explain to you why your selector didn't work in the first place; because the form didn't exist yet.
AJAX - Asynchronous Javascript and XML, is a technology that enables web applications to behave more like desktop applications by making asynchronous calls to the server. This eliminates the process of doing a complete page refresh while we need only a small part of the page to be updated. Google's Auto Suggest is a best example for AJAX implementation. As we type our search terms in the search box, Google gives us suggestions according to the search terms we type without refreshing the page.
You can read more about AJAX technology. AJAX is implemented using Javascript and XML. XMLHttpRequest is the object that does the job behind the scene. You can also use JSON ( Javascript Object Notation) instead of XML.
In this post, I am going to demonstrate with a simple example on how to make AJAX calls from a JSP page to a Servlet using JQuery and update the same JSP page back with the response from the Servlet. In other words, this post will give you an overview on how to implement AJAX calls in Java web applications. I am using JQuery library instead of implementing this in Javascript because it is quite tedious to make it work across all browsers in Javascript and JQuery simplifies this in a single function. Here are the steps to reproduce to create a simple AJAX enabled Java Web Application using Eclipse and Tomcat 7. When the user clicks on 'Ajax Submit' button, button click event is fired and the 'get' function executes the Ajax GET request on the Servlet(ActionServlet in the above example). The second argument of the get function is a key-value pair that passes the input value from JSP page to Servlet. The third argument is a function that defines what is to be done with the response that is got back from the servlet. For better understanding download the source code of the above example from the below link and run it yourself with small alterations in the Servlet and JSP page.
I think you have the answer in your question itself. Change the content-type attribute of the page directive to use UTF-8 instead of ISO-8859-1 and it will solve the problem.
I did not care much about the character set since I was using plain text for this example and the aim is to call the servlet asynchronously. But that is a good catch. It is always better to use UTF-8 encoding in all the layers. I will update the code soon.
Please do let me know whether this solved your issue. I recommend you to read this as well if the solution above does not solve your problem, http://weblogs.java.net/blog/joconner/archive/2005/07/charsettraps.html. Hi Saran, This post is a simple example and you can use the same code to get started with AJAX using jquery. I have used Tomcat server here, you can use Jboss server also and deploy your project. This pot explains how to retrieve plain text from servlet and update the part of the web page without reloading the whole page. If you want to send more complex java objects like lists,maps etc you have to use JSON and Jquery together.
Check this out, and start coding, Hope this helps! I have header.jsp, footer.jsp, leftpanel.jsp, rightpanel.jsp and middlebody.jsp, now i want to call servlet controller from leftpanel.jsp using ajax call.
![]()
And its response should be display on division tag of middlebody.jsp. Is it possible to do that?
And one more thing suppose i have requestdispatcher page and we need to display that page on div tag of middlebody.jsp. How could we do? Please ans if it is possible,if not then any other way to do that.
Actually i have tried to call servlet controller but i am confuse how to display requestdispatcher page. Thanks in advance. Hi RamMohan, I have not used javascript here.I have used jquery library to make AJAX calls to servlet. JQuery library internally uses XMLHttpRequest to make AJAX calls. Lines from my post for your reference, 'I am using JQuery library instead of implementing this in Javascript because it is quite tedious to make it work across all browsers in Javascript and JQuery simplifies this in a single function.' I suggest you to download the source code and run it in Tomcat Server to understand how this works. Thanks, Priya.
![]()
Hi, this code is good. Actually i am creating web application in that 3 buttons are there with three student names when we click on the first button all the details of first student will display in one div. If i click on second button all the second student details will replace the first student details on same div. Like third one also. For this i am using only ajax but i will get values but it wont replace with previous values of div. So plese send some reference to me. Thanks in Advance.
Hi Priya, Great example and advice. I have an example of where a user will interact with a JSP or HTML page submitting values via a HTML form. This in turn will interact with a Servlet, where inside the doPost, it must invoke a Web Service to a WorkFlow Engine. The Engine will then execute logic within a WF. This could take 1 minute or 10 minutes to complete all depending on many factors.
The WF returns a token, and using this token a subsequent set of API calls can be made to retrieve a status of the WF, if it is pending, running, completed, or failed. When I execute the Web Service method call I fork the thread so that the user is not waiting a long time for the doGet 'View' rendering to occur when the WF is finished executing. Is there a way inside a JSP page or Servlet doGet method to introduce AJAX such that it could show a progress until the web service status indicates it is completed? Once the WF shows a progress of completed, an additional call can return the result set, so ideally I would at the end display the result of the execution inside the page. So again, the user will just post data, and wait for a page to render. Without AJAX, the user gets a page telling them to check their email for details since the WF takes anywhere from 1 to 10 minutes to even 20 minutes to complete.
Give More Feedback
If the page had ajax flash a progress until the backend calls are completed it would be ideal. I could even have it return a different html output if a certain amount of time has been exhausted so the user is not waiting in front of the browser for 10 minutes. It could display progress until results for 2-5 minutes, beyond that, display message the the WF is still running and that the user should check their email.
See More On Stackoverflow
Any advice to this end will help, this is how I am trying to leverage AJAX in this case.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |