File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use a JSP variable in JavaScript? Plz Help

 
Daniel Iszatt
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly I am new to JavaScript.
I have a JSP page.
There is a chunk of JSP code that creates a String variable.
On this page there is also a chunk of code that I was given with encode and decode functions. These functions take a pair of strings and encrypt them. 1 Of the stings that I need to use in this function is created via JSP code.
So I need to get this JSP String variable to work in the encode function.
I know how to display JSP strings in HTML - but I dont know how to use them in JavaScript. Is it possible?
Also with the result of the encode function that is in JavaScript - I need to send the result in a form on that page.
My second question is how do you access JavaScript variables in HTML so that I can send it via a hidden value in a form?
 
Suresh Selvaraj
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
1.
<%
String s = "Java";
%>
<script>
...
document.[formName].[fieldName].value="<%= s %>";
...
</script>
2.
<%
String x1 = null;
%>
<script>
...
var s1 = "JAVA";
if(s1 != "")
{
<%
x1="Any Computed Value or JAVA";
%>
}
...
</script>
<INPUT TYPE=HIDDEN NAME="Counter" VALUE="<%=x1%>">
Hope this helps.
Suresh Selvaraj
Sun Certified Java Programmer
 
Daniel Iszatt
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou for that. It looks good.
I'll try it out next time I'm at work.
 
jini jane
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
The method of embedding the scriplet in the script is fine but when a user views the code it is clearly visible. in case the javascript is in a file how do we access a bean ?
 
Pete Harris
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no way of accessing a java bean from javascript as the bean is a sever side entity, whereas the javascript runs on the browser once the server request has been completed.
The only way of including jsp values in an included file is either to make the.js file a .jsp and then including it exactly as if it were a normal javascript file, or by declaring and defining variables within script tags in your main JSP, and referencing them in the included javascript file.
 
jini jane
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pete.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic