java JSP 中的隐藏输入在将其传递给 servlet 时产生 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11913609/
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-31 06:54:18  来源:igfitidea点击:

Hidden input in JSP produces null when passing it to the servlet

javahtmlformsjspservlets

提问by Hyman cole

In my JSP I do the following :

在我的 JSP 中,我执行以下操作:

<!-- Bank manager's permissions -->

<!--more stuff goes here -->
<fieldset>
  <legend>To open a new account</legend> 
  <form action="blablabla">    
      <input type="hidden" name="hdField" value="myValue" />  // note I pass a "myValue" as string 
      <a href="employeeTransaction1">Press here to continue</a>  
  </form>
</fieldset>

And in my Servlet I grab the hidden input :

在我的 Servlet 中,我获取了隐藏的输入:

@WebServlet("/employeeTransaction1")
public class Employee1 extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        String getHiddenValue=request.getParameter("hdField");
        System.out.println("Hidden field Value :"+getHiddenValue);
        // forwards to the page employeeOpenNewAccount.jsp
        request.getRequestDispatcher("/WEB-INF/results/employeeOpenNewAccount.jsp").forward(request, response);
    }



}

And System.out.printlnproduces : nullat the Console

System.out.println产生:null在控制台

Why do I get a nullof not the actual value is I pass ?

为什么我得到null的不是我通过的实际值?

Regards

问候

EDIT:

编辑:

After changing to :

更改为 后:

<fieldset>
  <legend>To open a new account</legend> 
  <form action="/employeeTransaction1" method="GET">
      <input type="hidden" name="hdField" value="myValue"/>
      <a href="employeeTransaction1">Press here to continue</a>  
  </form>
</fieldset>

A nullis still presented at the console .

Anull仍然显示在控制台上。

回答by Razvan

What you are trying to do is to send a form to the server. But, in fact, you don't do that. You just issue a GET request (when the user clicks your link: <a href="employeeTransaction1">Press here to continue</a>)

您要做的是将表单发送到服务器。但是,事实上,你不会那样做。你只是发出一个GET请求(当用户点击您的链接:<a href="employeeTransaction1">Press here to continue</a>

If you want to send the form make sure you set the attributes of the form tag properly and add a submit button to the form:

如果要发送表单,请确保正确设置表单标签的属性并向表单添加提交按钮

 <form action="/employeeTransaction1" method="GET">
 ...
 <input type="submit" value="Submit" />
 ...
 </form>

Depending on your preferred way of sending the form, you can change the method="GET"paramater to method="POST"and make sure that in the servlet you handle the form in the doPost()method

根据您发送表单的首选方式,您可以将method="GET"参数更改为method="POST"并确保在 servlet 中您在doPost()方法中处理表单

Alternatively, if your purpose is not to send the from to the server but just to pass the value of the hidden input, you should add its value as a prameter encoded in the GET request. Something like:

或者,如果您的目的不是将 from 发送到服务器,而只是传递隐藏输入的值,则应将其值添加为 GET 请求中编码的参数。就像是:

  /employeeTransaction1?hdField=myValue

To achieve this, you need some client processing, i.e. when the user clicks the link, the hidden input should be added to the get and then the request should be issued.

为此,您需要进行一些客户端处理,即当用户单击链接时,应将隐藏的输入添加到 get 中,然后发出请求。

回答by kpentchev

Using an hreftag does not submit your form, i.e. it does not pass the parameters defined in the form to the request. You should use input type="submit"or buttontags instead. Also make sure the form action matches your @WebServlet definition.

使用href标签不会提交您的表单,即它不会将表单中定义的参数传递给请求。您应该改用input type="submit"按钮标签。还要确保表单操作与您的 @WebServlet 定义匹配。

<fieldset>
  <legend>To open a new account</legend> 
  <form action="/employeeTransaction1">    
      <input type="hidden" name="hdField" value="myValue" />  // note I pass a "myValue" as string 
      <input type="submit" value="Submit" />
  </form>
</fieldset>