• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 59
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic