HTTP Status 405 - JSPs only permit GET POST or HEAD

前端 未结 2 844
暗喜
暗喜 2020-12-09 17:06

Since JSP 2.3 (Tomcat 8) only supported method for JSP is GET POST or HEAD:

https://jcp.org/aboutJava/communityprocess/maintenance/jsr245/245-MR3.html http://svn.apa

2条回答
  •  执笔经年
    2020-12-09 17:23

    As pointed by @MarkThomas you can make any HTTP request if you declare your JSP page as errorPage like this

    If you don't want to do that then there is one more alternative
    1. Create a filter (if you directly want to call a JSP page) or create a servlet (which will eventually call JSP page)
    2. Do this in doFilter() or in case of servlet doPut()/doDelete()

    Here I'm doing this in Filter where request is ServletRequest object.
    I'm using HttpRequestWrapper to wrap the original request with a fake request and telling it to return POST for DELETE and PUT requests so JSP thinks its a POST request and the page is executed, only downside is you won't be able to tell what the original request was; this can also be covered if you set an attribute with original method name, like this

    HttpServletRequest req = (HttpServletRequest) request;
    request.setAttribute("method", req.getMethod());
    req.getRequestDispatcher("/WEB-INF/resources/" +  resourceName + ".jsp").forward(new HttpServletRequestWrapper(req) {
        @Override
        public String getMethod() {
            String method = super.getMethod();
            if (method.equalsIgnoreCase("delete") || method.equalsIgnoreCase("put")) {
                return "POST";
            } else {
                return method;
            }
        }
    }, response);
    

提交回复
热议问题