This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSP and the fly likes How to use Javascript variable value in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to use Javascript variable value in JSP" Watch "How to use Javascript variable value in JSP" New topic
Author

How to use Javascript variable value in JSP

sourabh bansal
Greenhorn

Joined: Aug 20, 2007
Posts: 8
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
Marshal

Joined: Mar 22, 2005
Posts: 42367
    
  64
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?


Ping & DNS - my free Android networking tools app
sourabh bansal
Greenhorn

Joined: Aug 20, 2007
Posts: 8
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

Joined: Nov 29, 2009
Posts: 145
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

Joined: Jan 10, 2002
Posts: 61451
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tudor Raneti
Ranch Hand

Joined: Nov 29, 2009
Posts: 145
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

Joined: Jan 10, 2002
Posts: 61451
    
  67

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use Javascript variable value in JSP