• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use Javascript variable value in JSP

 
sourabh bansal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one tell how to use java script varaible value in JSP.

Here code looks like....

<script type="text/javascript">
var test1=window.location.pathname
</script>

How can i use this "test1" in my jsp/jstl code.

Test path is <cut value='${test1}'>

how can print Scriplet genrated result in JSTL.


Thanks
cheers....
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSPs run at the server, while JavaScript runs at the client - never the twain shall meet.

The JSP already knows the URL of the page it's generating - what else are you looking for?
 
sourabh bansal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:JSPs run at the server, while JavaScript runs at the client - never the twain shall meet.

The JSP already knows the URL of the page it's generating - what else are you looking for?




Thanks Sherrif....Thanks for the reply,I want to use in tracking purpose...Is there any other way to do it....




 
Tudor Raneti
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The title here : http://www.jguru.com/faq/view.jsp?EID=22950
is
How can you copy a JavaScript variable to a JSP session variable without using cookies?

so there's an answer for you.

Try generating / editing a JSP page from JavaScript:
http://www.java2s.com/Code/JavaScript/HTML/HTML-Generate.htm
http://www.java2s.com/Code/JavaScript/HTML/UsingJavaScripttoCreateHTMLTags.htm
If you can generate HTML you can generate JSP IMO (or at least try resending the page with JSP markup to the server, if you can do that it'll work).

You can say you use a javascript variable in JSP too if you use it before the javascript is used :P - before you generate it that is.
http://coldjava.hypermart.net/servlets/jscalltag.htm
http://www.ibm.com/developerworks/web/library/wa-aj-simplejava1/index.html
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64964
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tudor Andrei Raneti wrote:If you can generate HTML you can generate JSP IMO

Ummm, no. You can't. Any JSP syntax generated on the client will just be gibberish.

JSP and JavaScript cannot directly interact as the JSP gets executed on the server long before the HTML page it generates is sent to the browser where the JavaScript is evaluated.
 
Tudor Raneti
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, I know JSP has to be parsed, and for that it needs sending to the server. The scenario i was thinking is the following: the problem is that the JSP on page is only temporary, so making a page refresh or self forward to self would return the old version of the page i think (because the server still has the old version, so a request will send the old version as a response). The chalange would be sending the new page as it is to the server and getting the parsed version of it back . It's beyond me at this point. Probably would need a special framework to do that, but i don't see the point.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64964
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no point as all that is not necessary in the face of techniques like Ajax. What the original poster needs to understand is how JSP really works and why what he's trying to do makes no sense and is non-causal.

Then he can post what he's really trying to accomplish and we can all figure out the best way to get it done.

Perhaps this article can help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic