简体   繁体   English

将JSP中的隐藏输入传递给Servlet时,它会产生null

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

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.println produces : null at the Console 然后System.out.println在控制台上生成: null

Why do I get a null of 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 null is still presented at the console . 控制台上仍然显示null

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. 另外,如果您的目的不是将发件人发送到服务器,而只是传递隐藏输入的值,则应将其值添加为GET请求中编码的参数。 Something like: 就像是:

  /employeeTransaction1?hdField=myValue

To achieve this, you need some client processing, ie when the user clicks the link, the hidden input should be added to the get and then the request should be issued. 为此,您需要进行一些客户端处理,即,当用户单击链接时,应将隐藏的输入添加到get中,然后发出请求。

Using an href tag does not submit your form, ie it does not pass the parameters defined in the form to the request. 使用href标签不会提交您的表单,即,它不会将表单中定义的参数传递给请求。 You should use input type="submit" or button tags instead. 您应该改用输入type =“ submit”按钮标签。 Also make sure the form action matches your @WebServlet definition. 还要确保表单操作与您的@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>

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

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