简体   繁体   中英

Tomcat: Servlet sendRedirect to JSP says: “HTTP Status 405 - HTTP method GET is not supported by this URL”

Below is code for a simple Servlet(Process.java), JSP page(index.jsp) and Model(Model.java).

index.jsp :

<%@ page import="com.example.*" %>

<html>
<head>
<title> Myapp </title>
</head>

<body>
<form action="process.do" method="POST">
UserName: <input type="text" name="username">
<br>
UserID: <input type="text" name="userid">
<br>
<input type="submit">
<br>

<%
Model m = (Model) request.getAttribute("model");

if( m != null) {
out.println("Username: " + m.getUserName() );
out.println("UserID: " + m.getUserID() );
}
%>

</form>
</body>
</html>

Process.java :

package com.example;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Process extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Model m = new Model();
        m.setUserName( request.getParameter("username") );
        m.setUserID( Integer.parseInt( request.getParameter("userid") ) );

        request.setAttribute("model", m);
        response.sendRedirect( request.getRequestURI() );
    }
}

Model.java :

package com.example;

public class Model {

    private String userName = "";
    private int userID = -1;

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setUserID(int userID) {
        this.userID = userID;
    }

    public String getUserName() {
        return userName;
    }

    public int getUserID() {
        return userID;
    }
}

web.xml :

<servlet>
    <servlet-name>Process</servlet-name>
    <servlet-class>com.example.Process</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Process</servlet-name>
    <url-pattern>/process.do</url-pattern>
</servlet-mapping>

I am using Tomcat7 and I have deployed this app in context /myapp. I am able to view index.jsp page correctly, but when I submit the form, I am getting below error:

HTTP Status 405 - HTTP method GET is not supported by this URL

What you are trying to accomplish with response.sendRedirect( request.getRequestURI() ); ?

This will instruct browser to send a GET request to /process.do which is not handled in servlet(You are extending only doPost). If you want to go back to index.jsp just have response.sendRedirect( "index.jsp");

EDIT:

Because you want model attribute to be accessed in index.jsp, we cant actually do a browser redirect. Instead a server redirect is needed.

request.getRequestDispatcher("index.jsp").include(request, response) instead of response.sendRedirect() should work for you.

You are not redirecting to JSP . Let me explain what happens is when you post the form url changes to http://domain.com/../process.do and when you use request.getRequestedUri() it gives to the url of servlet and it doesn't have doGet() method hence you are getting that error. You should use response.sendRedirect("index.jsp") to redirect to your index.jsp file .

当您使用“ response.sendRedirect()”方法时,它将请求对象移交给浏览器。因此,您将无法再处理请求对象。请使用“ RequestDispacther”代替“ sendRedirect”。希望如此可能会让您更清楚。如果我没有联系到您,请别介意。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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