Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the value from javascript code to jsp scriptlet with in the same jsp page

below is my code (1.jsp)

<html>
<head>
  <script type="text/javascript">

   function changeFunc() {
var selectBox = document.getElementById("selectBox");
var selectedValue = selectBox.options[selectBox.selectedIndex].value;
  document.write("\n value is"+selectedValue);
  }

 </script>
</head>
 <body>
<form method="post" action="SampServlet">
  <select id="selectBox" name="selurl" onchange="changeFunc();">
   <option value="1">Option #1</option>
   <option value="2">Option #2</option>
  </select>
</form>
 </body>
</html>

Here I have inserted this code into a jsp page.And getting the value of "selectedValue" from javascript to scriptlet with in the same jsp like this.

<% String val=(String)request.getParameter("selurl");
System.out.println("\n selected value is:"+val); %>

I am getting selected value as null as output. And if I print javascript selectedValue parameter it is giving me correct output i.e.,output as the option selected.But in scriptlet am getting null.Where is the error.I included all headers and directives.Please help me.

like image 220
user2365917 Avatar asked Jan 23 '26 06:01

user2365917


2 Answers

In your web browser you have only html, javascript and css. All JSP code is meant to be run on the server. So you get only the output of the jsp file. And after this you cannot change the jsp code.

like image 97
me_digvijay Avatar answered Jan 24 '26 19:01

me_digvijay


Use submit Button to get Your Selected value at same page and no need any function,no need onsubmit.

for example:

<form method="post" action="">
 <select id="selectBox" name="selurl">
<option value="1">Option #1</option>
<option value="2">Option #2</option>
</select>
<input type="submit" value="Submit" name="sub">
                                                //use scriplet tag 
<% String r=request.getParameter("sub");
if(r.equals("Submit"){
String s=request.getParameter("selurl");
System.out.println("selected value is "+s);
}%>
</form>
like image 34
151291 Avatar answered Jan 24 '26 20:01

151291