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

2024-03-27

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

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

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

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


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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在同一个jsp页面中从javascript代码获取值到jsp scriptlet 的相关文章

随机推荐