将参数从jsp传递到servlet [英] pass parameter from jsp to servlet

查看:342
本文介绍了将参数从jsp传递到servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用不属于session的form形式的表单将参数从jsp传递到servlet.我认为代码可能看起来像下面的示例,但对我不起作用.plz帮助我.

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.

在index.jsp中:-

in 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中:-

in servlet.java:-

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

推荐答案

您的表单需要提交,例如有一个提交按钮.并且您需要将参数作为输入.在窗体内调用request.setAttribute不会执行任何操作.设置请求属性的目的是要在使用分派器转发请求时使用,而不是在使用表单时.

Your form needs to be submitted, e.g. have a submit button. And you need to have your parameter as an input. Calling request.setAttribute inside the form doesn't do anything. 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>

这篇关于将参数从jsp传递到servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆