Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes JSP and the fly likes Javascript variable inside jsp scriplet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Javascript variable inside jsp scriplet" Watch "Javascript variable inside jsp scriplet" New topic

Javascript variable inside jsp scriplet

Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315

is there any way to pass a javascript variable value to a variable inside a scriplet in a JSP. ( this needs to be achieved without submitting the form)

Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

I don't think it's possible. You can pass JSP value to javascript, but not the other way around.

SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

One thing that needs to be understood is that Javascript and your JSP scriptlets are interpreted at different times, on different machines.

The scriptlet is interpreted on the server. It's output is the HTML and javascript that goes to the browser.
The HTML is rendered, and the Javascript is interpreted in the client (browser). The scriptlet code never goes to the browser and it has already finished being interpreted at this point.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63355

Originally posted by Freddy Wong:
You can pass JSP value to javascript,

No, you can't. What you can do is to use the value of JSP variables to create JavaScript that contains these values. But you can't "pass" anything.

Please read this article to understand why there can be no "passing" in either direction.
[ July 31, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Prafulla N. Patil
Ranch Hand

Joined: Mar 20, 2006
Posts: 106

in process.jsp you can get value of "javascriptvar"

getting value in that page itself is not possible

Thanks, Prafulla Patil
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63355

Submitting a value to another JSP is not what the original poster was asking about.
I agree. Here's the link:
subject: Javascript variable inside jsp scriplet
jQuery in Action, 3rd edition