javascript 如何在同一jsp页面中从javascript代码获取值到jsp scriptlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16643798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:29:15  来源:igfitidea点击:

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

javascriptscriptlet

提问by user2365917

below is my code (1.jsp)

下面是我的代码(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.

在这里,我已将此代码插入到一个 jsp 页面中。并在同一个 jsp 中从 javascript 获取“selectedValue”的值到 scriptlet。

<% 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.

我将选择的值设为 null 作为输出。如果我打印 javascript selectedValue 参数,它会为我提供正确的输出,即,作为选择的选项输出。但是在 scriptlet 中,我得到了 null。错误在哪里。我包含了所有标题和指令。请帮助我。

回答by me_digvijay

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.

在您的网络浏览器中,您只有 html、javascript 和 css。所有 JSP 代码都应该在服务器上运行。所以你只能得到jsp文件的输出。在此之后,您将无法更改 jsp 代码。

回答by 151291

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>

回答by c.P.u1

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

您的 select 元素应该有一个 name 属性,并且您必须在 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.

如果您希望在 select 元素的 onchange 事件上发出服务器请求,则必须使用 Ajax。

Using jQuery,

使用jQuery,

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

回答by Ravi Thapliyal

You're missing the <submit>button.

你缺少<submit>按钮。

<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 actionattribute is pointing to SampServletwhich seems to be a servlet. For jsps we usually have something like action="page.jsp"in the form.

添加按钮并单击它以将表单提交给您的 servlet,并将选定的值作为selurl. 请注意,您的表单action属性指向的SampServlet似乎是一个 servlet。对于jsps,我们通常有类似action="page.jsp"的形式。

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

编辑
如果您想在用户从下拉列表中选择一个值时自动发布表单,只需将您的设置onchange为:(<submit>那时您甚至不需要按钮)

onchange="this.form.submit()"