• Post Reply Bookmark Topic Watch Topic
  • New Topic

pass a value from a servlet to a HTML/JavaScript function  RSS feed

 
Ravi Desigan
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends,

I had a requirement on some course I am doing to:

1. Create a Servlet subclassing "strictly" generic Servlet (as opposed to HttpServlet).
2. Pass a value from the servlet to a newer HTML page.
3. Display the message from the servlet on the HTML page. If there is no message, display null (nothing).

1. At the servlet side, I am passing info through URL like this:

   

2. At the Javascript side, I have a transparent, disabled textbox to display the info passed (or nothing if no message passed):
  
   

3. Finally, using a JavaScript function, on window.onload of the HTML page, I am setting the value of the above textbox, with the input from the URL parameters as follows:



There could be other simpler ways to do the same. I would like to know about them, if any!

I hope the above code really helps someone out there, I spent A LOT and A LOT of time to figure this out, there was not any helpful internet resource to figure out the above!

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65898
135
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As this is in the servlet environment, why not use the session to pass the value rather than going through all the mayhem of parsing the URL?
 
Ravi Desigan
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To do that way, (by passing through session.setAttribute() and use the session object, I need to use JSP.

This solution is strictly for accessing something like a 'Insert success' message passed from a servlet (via JavaScript alone) and have that info displayed on the HTML page.
 
Paul Clapham
Sheriff
Posts: 22205
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The goal of the servlet is to send a response to whatever requested it in the first place.

So if you want the servlet just to send a single piece of information to a JavaScript function which is sitting in the browser, then that JavaScript must have been the thing which sent the request to the servlet. That would require an AJAX call.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!