Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Quick JSP question

  Asked By: Aditi    Date: Jun 14    Category: Java    Views: 513
  

I have very quick question. I have javascript varable and JSP
variable.

var j = "Test" ;

<%
String s = "" ; %>

I would like to assign j value to s.

I tried this

<% String s = %> j ; ( Did not work )
<% String s %> = j ; ( Did not work )

if you know hwo to do it. that would really help me. Once again,
thanks you for your help and time.

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Leonard Pierce     Answered On: Jun 14

Can't do this because the <% ... %> is run on the
server while the var  j = "" is run on the client.

In other words, one is java while the other is
javascript.

 
Answer #2    Answered By: Elisabeth Bell     Answered On: Jun 14

j is just a string and your only choice is to send it to the server side
like so.

<form name="foo" method="post">
<input type="hidden" name="var_j">
<input type="submit">
<script>document.var_j = j</script>

After submitting the form you can assign  it on the server side.
<% String s = request.getParameter("var_j")%>

 
Answer #3    Answered By: Midissia Lopez     Answered On: Jun 14

This will work,

<%
String s="JSP";
%>
<script>
var j="Java Script";
<%=s%>=j
alert(<%=s%>)
</script>

 
Didn't find what you were looking for? Find more on Quick JSP question Or get search suggestion and latest updates.




Tagged: