File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlets & javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets & javascript" Watch "Servlets & javascript" New topic

Servlets & javascript

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I have a servlet which retrieves a string from the database. Can I pass that string to a javascript variable???
That variable is than sent to the client side to do further processing.
Edgar Sanchez

Joined: Oct 12, 2000
Posts: 21
Try something like this in your web page:
<SCRIPT LANGUAGE="Javascript">
var myJSstring = '<%= myJSPClass.getValue() %>';
var myJSnumber = <%= myOtherJSPClass.getValue() %>;
[This message has been edited by Edgar Sanchez (edited October 22, 2000).]
Prabhu Chandrasekaran
Ranch Hand

Joined: Oct 14, 2000
Posts: 36
One of the methods through which you can access java variables in JavaScript in servlets is through hidden variables.
After retreiving the string from the database, put it as a value for a hidden field using the foll. code :
out.println("<input type=hidden name=fieldname value='"+varname+"'>");
You can access the same in JavaScript for client side validations using document.formname.fieldname.value.
I have used the same in many of my servlets.
See if this helps your cause.

S Chandra Mohan
Ranch Hand

Joined: Oct 19, 2000
Posts: 75
u can take the value of the varialble directly into javascript
out.println(" function valchk(){ ");
out.println(" var sampval = " + mservval + " ; ");
or u can directly check like
out.println(" if(document.myhtm.t1.value = '"+mservval+" ) { ");
i think this can solve ur problem much
all the best'

Have a wonderful day and wish u success<p>S Chandra Mohan<br />
I agree. Here's the link:
subject: Servlets & javascript
It's not a secret anymore!