aspose file tools*
The moose likes Servlets and the fly likes How do you set a javascript variable from jsp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How do you set a javascript variable from jsp?" Watch "How do you set a javascript variable from jsp?" New topic
Author

How do you set a javascript variable from jsp?

Ted Dong
Greenhorn

Joined: May 16, 2001
Posts: 15
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

Joined: Jan 30, 2000
Posts: 3222
    
    5
You have two
target=

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Ted Dong
Greenhorn

Joined: May 16, 2001
Posts: 15
Thanks for the response but I don't quite understand what you mean by 'target='?
Can you elaborate?
Ted Dong
Greenhorn

Joined: May 16, 2001
Posts: 15
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: May 16, 2001
Posts: 15
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

Joined: Feb 20, 2001
Posts: 3695

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

Joined: May 16, 2001
Posts: 15
Thank you Peter and Mike.
I was able to get it to work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do you set a javascript variable from jsp?
 
Similar Threads
Assign Javascript value to JSP var
Intialize javascript variable using jsp
javascript passing value to JSP
Struts2 variable inside javascript
Pasing scriptlet variable to javascript