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

How do you set a javascript variable from jsp?

 
Ted Dong
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know how to pass a variable value from javascript to jsp. But how can I set a javascript value from a jsp page?
For example, I have an html page which contains a javascript variable.
<script language=javascript>
var myvar = "";
</script>
This page contains a form whose target is a jsp page
<form target=mypage.jsp target=_blank>
<input>....
</form>
When this form is submitted the jsp does some processing on the server. I want to set a result from the processing back into my javascipt variable myvar.
Does anyone know how to do this?
Thanks in advance.
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have two
target=
 
Ted Dong
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response but I don't quite understand what you mean by 'target='?
Can you elaborate?
 
Ted Dong
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. Nevermind my last post. I made a typo in my original post. It should have read:
<form action=mypage.jsp target=_blank>
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ted Dong:
When this form is submitted the jsp does some processing on the server. I want to set a result from the processing back into my javascipt variable myvar.

Remember, when the form is submitted it is a HTTP request. The server sends back the result page as the response. This is a whole new page! "Returning" a value from the JSP to JavaScript simply means initialising myvar to the value you want to "return":

I keep on quoting "return" because you're not returning values in the traditional sense. To the browser, it's really a whole new page and a whole new JavaScript program.
- Peter
 
Ted Dong
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter.
I think I understand what you are saying. In my jsp page what would I need to do to set the result?
Can you give me an example of jsp code to set the return value?
Many Thanks.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted, he has given you an example.

Remember that JSP is a server-side technology, so the JSP tags are evaluated before the client browser gets them. So... anything between <% %> or the <%= %> tags is evaluated first, on the server side.

So in your JSP somewhere, as Peter showed you, you'd have this: Notice the <%= tag? That will be evaluated on the server side, and replaced by the value that is in the 'result' JSP variable above, in the plain <% tags.
 
Ted Dong
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Peter and Mike.
I was able to get it to work.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic