• Post Reply Bookmark Topic Watch Topic
  • New Topic

data passing from javascript to jsp

 
Soham Kapoor
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello guys I need your help in solving my problem .... My code goes like this but I am getting a null value in k. can I perform the same without button or click event ? Please help me


<% // some jsp code
String key="some value";
if(key!= null)
{
%>
<html>
<head>
<script type="text/javascript">
var val = prompt("Please enter R for Read or W for Write", "");
document.getElementById("hid").value=val;
</script>
</head>
<body>
<input type="hidden" id="hid" name="hid" />
</body>
</html>
<%
}
String k=request.getParameter("hid");
key=key+k;
System.out.println(key);
%>
 
Dave Tolls
Ranch Hand
Posts: 2284
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't put Java code in a JSP.
Also you should be submitting to a servlet that then forwards to the JSP page after doing whatever processing is needed.

In this case, you haven't even got a form to submit, so I'm not sure what you are trying to do?
 
Stefan Evans
Bartender
Posts: 1822
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't run some java, then run some javscript to get user input, then run some more java again all in the same process.
To pass data from javascript/web page to JSP you need to make a request to the server.

It's that whole JSP lifecycle.
Java/JSP runs - produces the HTML page. Stops running.
The page runs in your browser in HTML/Javascript.

To run java again, it needs to request to the server - either click a link, submit a form or make an AJAX request.

When you ask the user for Read/Write what do you want the user to do?
What should that input look like?
You then need to take that input and call another JSP/Servlet
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For an explanation of how JSP works, please read this article.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!