开发者

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

If I leave the action attribute out of my form so it posts back to the same JSP I have no trouble reading the request parameters. However when I add an action attribute to handle the form with a separate JSP, the request parameters are null. Here's a short example (FormTest.jsp) that illustrates how I'm reading 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 null along with the form. Clicking any of the three buttons changes the message to You clicked First, etc.

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

<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>

I expected the new FormHandler.jsp to just print out which button was pressed on the other page, but it seems the request parameter is always null.

What could be interfering with the request parameters being sent to a separate JSP?

Update:

This project has a JSF configuration file as well. I changed the action attribute to ACTION="FormHandler.faces" and the code above works but I don't quite understand why yet. Here's the method that's redirecting requests that end in .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(newAd开发者_高级运维dress);
    }
    else { //Address ended in "/"
        response.sendRedirect("login.faces");
    }
}

Now I guess I need to know 1) how to figure out if this is the source of the problem, and 2) is there a way to preserve the request parameters when the response is redirected?

There's also an entry in the web.xml configuration file for this project that sets a filter mapping.

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

I suppose (but it's probably clear by now that I'm new to JSF, so someone correct me if I'm wrong) that using the .faces extension in my action attribute bypasses this filter.


POST parameters are lost because sendRedirect() sends a 302 Moved Temporarily redirect, which instructs the browser to load the specified page with GET request.

To retain parameters you need to use 307 Temporary Redirect instead - it instructs the browser to repeat a POST request to the specifed URI:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜