• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets & javascript

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try something like this in your web page:
<SCRIPT LANGUAGE="Javascript">
var myJSstring = '<%= myJSPClass.getValue() %>';
var myJSnumber = <%= myOtherJSPClass.getValue() %>;
</SCRIPT>
[This message has been edited by Edgar Sanchez (edited October 22, 2000).]
 
Prabhu Chandrasekaran
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
Bye.

------------------
 
S Chandra Mohan
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai,
u can take the value of the varialble directly into javascript
like
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'
bye
mohan
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic