• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to access the request variable in the java script?

 
sudheshna Iyer
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nandan Jain
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He's apparently confusing JS with JSP.
 
Nandan Jain
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean we should do:


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would that create valid JavaScript? (close, but evaluate what will happen carefully).
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: check the generated output. Do a 'view source' in webbrowser used.
 
sudheshna Iyer
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic