Java – when I publish the form to another page, the JSP form parameters disappear

If I delete the action attribute from my form, it will be sent back to the same JSP, and I can easily read the request parameters However, when I add an action attribute to handle a form with a separate JSP, the request parameter is null This is a short example (formtest. JSP), which shows how I read requests

<HTML>
    <HEAD>
        <TITLE>FormTest.jsp</TITLE>
    </HEAD>
    <BODY>
        <H3>Using a Single Form</H3>
        <%
            String command = request.getParameter("submit");
        %>
            You clicked <%= command %>

        <FORM NAME="form1" METHOD="POST">
            <INPUT TYPE="SUBMIT" NAME="submit" VALUE="First">
            <INPUT TYPE="SUBMIT" NAME="submit" VALUE="Second">
            <INPUT TYPE="SUBMIT" NAME="submit" VALUE="Third">
        </FORM>
    </BODY>
</HTML>

The above page works as expected The initial page prints you click null with the form Clicking any of the three buttons will change the message to "first" and so on

Now I only change one line in the above page to add the action attribute:

<FORM NAME="form1" METHOD="POST" ACTION="FormHandler.jsp">

I added a separate JSP to the project to read the request parameters, as follows:

<HTML>
    <HEAD>
        <TITLE>FormHandler.jsp</TITLE>
    </HEAD>
    <BODY>
        <H3>Form Handler</H3>
        <%
            String command = request.getParameter("submit");
        %>
            You clicked <%= command %>
    </BODY>
</HTML>

I want the new formhandler JSP only prints out the button pressed on another page, but it seems that the request parameter is always null

What might interfere with the request parameters being sent to a separate JSP?

to update:

The project also has a JSF configuration file I change the action attribute to action = "formhandler. Faces". The above code can work, but I don't quite understand why This is a redirect to JSP end of the request method

public void doFilter(ServletRequest req,ServletResponse res,FilterChain chain)
        throws ServletException,IOException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    String uri = request.getRequestURI();

    if (uri.endsWith(".jsp")) {
        int length = uri.length();
        String newAddress = uri.substring(0,length - 3) + ".faces";
        response.sendRedirect(newAddress);
    }
    else { //Address ended in "/"
        response.sendRedirect("login.faces");
    }
}

Now I think I need to know 1) how to determine whether this is the root cause of the problem, and 2) is there a way to retain the request parameters when redirecting the response?

This project's web There is also an entry in the XML configuration file to set the filter mapping

<filter-mapping>
    <filter-name>faces-redirect-filter</filter-name>
    <url-pattern>*.jsp</url-pattern>
</filter-mapping>

I think (but it may be clear now that I'm a novice in JSF, so if I'm wrong, someone will correct me), use it in my action attribute The faces extension bypasses this filter

Solution

The post parameter is missing because sendredirect() sends 302 moved temporary redirection, instructing the browser to load the specified page using the get request

To preserve the parameters, you need to use 307 temporary redirection – which instructs the browser to repeat the post request for the specified URI:

response.setHeader("Location",newAddress); 
response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>