JSP form parameters disappear when I submit my form to another page

If I leave the attribute actionoutside the form so that it returns back to the same JSP, I could not read the request parameters. However, when I add an attribute actionto process the form using a separate JSP, the request parameters null. Here is a short example ( FormTest.jsp) that illustrates how I read the request.

<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. Initially, the page prints You clicked nullwith the form. Pressing any of the three buttons changes the message to You clicked First, etc.

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

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

I added a separate JSP to my project to read the request parameter as follows:

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

, FormHandler.jsp , , , null.

JSP?

:

JSF. action ACTION="FormHandler.faces", , , . , , .jsp.

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");
    }
}

, , 1) , , 2) ?

web.xml , .

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

(, , , JSF, - , ), .faces action .

+3
1

POST , sendRedirect() 302 Moved Temporarily, GET.

, 307 Temporary Redirect - POST URI:

response.setHeader("Location", newAddress); 
response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT); 
+7

Source: https://habr.com/ru/post/1767943/


All Articles