File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes How to access the request variable in the java script? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to access the request variable in the java script?" Watch "How to access the request variable in the java script?" New topic
Author

How to access the request variable in the java script?

sudheshna Iyer
Ranch Hand

Joined: Aug 20, 2004
Posts: 71
How to access the request variable in the java script?

This is what I want to do:

I want to set a boolean variable showMessage to true or false in servlet code and when the page loads

based on this variable if showMessage = true, I want to show a pop up asking user whether he wants to continue or cancel.

I can I access request variable "showMessage" in the Javascript onload event?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Just let JSP print it out as a JS variable. Let the JS which runs during onload use that variable.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Remember that a JSP is just a template for creating an HTML page. So you can create any text to send to the browser you want -- including any JavaScript statements.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nandan Jain
Ranch Hand

Joined: Feb 14, 2006
Posts: 44
Which way you are getting redirected to JSP from Servlet ?

1) If you are using response.sendRedirect("") method then at the end of the argument put "?showMessage=true" and read this variable in JSP.

2) If you using view.forward() then set the variable in request by request.setParameter("showMessage","true");

Hope this helps.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Nandan Jain wrote:Which way you are getting redirected to JSP from Servlet ? ... Hope this helps.

I don't see how any of that is relevant to the problem.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
He's apparently confusing JS with JSP.
Nandan Jain
Ranch Hand

Joined: Feb 14, 2006
Posts: 44
To access request variable in JS first you have to catch it in JSP variable then JS. like this:

<%

String str = (String) request.getParameter("showMessage");

%>


<SCRIPT>
alert(<%=str%>);

</SCRIPT>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Nandan Jain wrote:To access request variable in JS first you have to catch it in JSP variable then JS. like this:

No, you don't.

First of all, that's badly antiquated scriptlet code that should no longer be used in JSPs. Secondly, using the EL, accessing any parameter is just a matter of an expression such as ${param.paramName}.

This is easy to embed in any template text that represents JavaScript statements.

let's not make this any more complicated than it needs to be.
sudheshna Iyer
Ranch Hand

Joined: Aug 20, 2004
Posts: 71
Do you mean we should do:


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Would that create valid JavaScript? (close, but evaluate what will happen carefully).
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Hint: check the generated output. Do a 'view source' in webbrowser used.
sudheshna Iyer
Ranch Hand

Joined: Aug 20, 2004
Posts: 71
alert(${aaa}); ==> works and prints whatever is the value of "aaa"

But the following is not working. Based on the variable, I need to show a confirm dialog box to proceed or not proceed.
I am seeing ${aaa} as empty in the firebug when I view source with the following code.

Any idea why " works butdoesn't work?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

SHowing us the JSP code is irrelevant. All that matters is the text that is sent to the browser. Did you do a View Source of what is being sent to the browser as Bauke instructed? Is it valid JavaScript? (Hint: it is not.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to access the request variable in the java script?
 
Similar Threads
passing javascript variable in java method
passing string to a servlet
Java Script variable in JSP
getting and setting environment variables
Use JavaScript Variable In Side Java Code