简体   繁体   English

将参数从jsp传递到servlet

[英]pass parameter from jsp to servlet

how to pass a parameter from jsp to servlet using form which is not belong to any field of form without using session.i think code may be look like below example but doesn't work for me.plz help me. 如何在不使用session的情况下使用不属于表单任何字段的表单将参数从jsp传递到servlet。我认为代码可能看起来像下面的示例,但不适用于我.plz帮助我。

in index.jsp:- 在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>

in servlet.java:- 在servlet.java中:

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

Your form needs to be submitted, eg have a submit button. 您的表单需要提交,例如有一个提交按钮。 And you need to have your parameter as an input. 并且您需要将参数作为输入。 Calling request.setAttribute inside the form doesn't do anything. 在表单内调用request.setAttribute不会执行任何操作。 Setting a request attribute is for when you are going to use a dispatcher to forward the request, not when you are using a form. 设置请求属性的目的是在您要使用调度程序转发请求时使用,而不是在使用表单时。

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

You can receive the parameters you submit in the form with the method: 您可以使用以下方法接收在表单中提交的参数:

request.getParameter("fieldname");

For intance, your servlet could get all the fields: 例如,您的servlet可以获取所有字段:

 @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {

                            String username= request.getParameter("username");
                            String password= request.getParameter("password");

            }
}

You can also send parameters from a link, eg: 您还可以从链接发送参数,例如:

<a href="Servlet?nameOfParameter=valueOFparameter">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM