从jsp传递参数到servlet

2024-01-13

如何使用不属于表单任何字段的表单将参数从 jsp 传递到 servlet,而不使用 session。我认为代码可能类似于下面的示例,但对我不起作用。请帮助我。

在index.jsp中:-

<form method="Post" action="servlet">
        <input type="text" name="username">
        <input type="password" name="password">
          <% 
              int z=1;
              request.setAttribute("product_no", z);%>
        <input type='submit' />
</form>

在 servlet.java 中:-

 int x=Integer.parseInt(request.getAttribute("product_no").toString());

您需要提交表格,例如有一个提交按钮。并且您需要将参数作为输入。呼唤request.setAttribute表单内不执行任何操作。设置请求属性是在您要使用调度程序转发请求时,而不是在您使用表单时。

<% int z=1; %>
<form method="Post" action="servlet">
        <input type="text" name="username" />
        <input type="password" name="password" />
        <input type="hidden" name="product_no" value="<%=z%>" />
        <input type='submit' />
</form>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从jsp传递参数到servlet 的相关文章

随机推荐