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

大憨熊 提交于 2019-12-23 18:40:21

问题


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.


回答1:


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.




回答2:


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>



回答3:


Your select element should have a name attribute and you must use that name in request.getParameter()

<select id="selectBox" name="selurl"">
   <option value="1">Option #1</option>
   <option value="2">Option #2</option>
</select>


String val = request.getParameter("mySelect");

EDIT:

If you want the server request to be made on the select element's onchange event, you must Ajax.

Using jQuery,

$.post('SampServlet', {selectedValue: selectedValue}, function(data) {
//Update view
});



回答4:


You're missing the <submit> button.

<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>
  <input type="submit" /> <!-- MISSING!! -->
</form>

Add the button and click on it to submit the form to your servlet with the selected value sent as selurl. Please, note that your form's action attribute is pointing to SampServlet which seems to be a servlet. For jsps we usually have something like action="page.jsp" in the form.

EDIT:
If you want to post the form automatically when the user selects a value from the drop-down just set your onchange to: (you won't even need the <submit> button then)

onchange="this.form.submit()"


来源:https://stackoverflow.com/questions/16643798/how-to-get-the-value-from-javascript-code-to-jsp-scriptlet-with-in-the-same-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!