1

I am new to Java EE. In my application, I have a HTML page containing a text area filled with some information. I also have a form for the user. Once he submits the form, I use a servlet to process the information. Finally I need to forward the Result from the servlet to the same HTML page and update it in the existing textarea. I have done creation of HTML page, submitted the form to servlet. I am now stuck on how to access the textarea in the HTML page to update my result.

I googled a lot but everywhere people forward the result of servlet to JSP page and create a fresh HTML page using "out" object.

I need to use the same textarea of my HTML page to update the result. Please help me on achieving this.

Thanks in advance.

user4582135
  • 519
  • 1
  • 6
  • 14
  • Is this helpful to get started and grasp the basic concepts for whatever you're ultimately trying to achieve? http://stackoverflow.com/q/4112686 – BalusC Apr 17 '16 at 14:51

1 Answers1

0

There are two ways to do this.

Server-side rendering. You can re-draw the whole page, with a normal form submit. In this case you would put something in request scope in the servlet and then access that in the JSP when you draw the textarea. Note that you are not accessing the HTML textarea in the JSP - the JSP runs server-side code to generate HTML markup, but you are not accessing the browser DOM directly.

This might look like:

  • servlet: request.setAttribute("textareaContent", varWithTextareaContent)
  • JSP: <textarea>${textareaContent}</textarea>

Client-side rendering Instead, you could make an AJAX post request with jQuery (there are some plugins to help with this). In this case, your servlet would not forward to a JSP for HTML rendering - it would directly return a JSON object like {"textareaContent": ...} , and you would handle that client-side in your AJAX callback. In this case you would be accessing the textarea in the browser DOM directly, in Javascript (not JSP).

wrschneider
  • 17,913
  • 16
  • 96
  • 176
  • I prefer going with the first approach. But don't we have something in JSP like, getElelmentById on current page and update the content of it. – user4582135 Apr 17 '16 at 12:42
  • No. `getElementById` would apply if you were updating the page contents client-side. When you are rendering server-side in JSP, you are rendering brand new HTML DOM elements (usually the whole page), not updating existing ones. – wrschneider Apr 18 '16 at 00:37