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

user2365917 picture user2365917 · May 20, 2013 · Viewed 33.6k times · Source

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.

Answer

me_digvijay picture me_digvijay · May 20, 2013

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.