Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

How do you set a javascript variable from jsp?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have two
target=
 
Ted Dong
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Peter and Mike.
I was able to get it to work.
 
reply
    Bookmark Topic Watch Topic
  • New Topic