19

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.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/servlet/JspServlet.java?view=diff&r1=1497877&r2=1497878&pathrev=1497878

But, I suppose, it is a big incompatible change as, for example, for exception handler it is used to forward to JSP for rendering exception and iso JSP view since JSP 2.3 response is:

Method Not Allowed
HTTP Status 405 - JSPs only permit GET POST or HEAD 

description The specified HTTP method is not allowed for the requested resource.
Apache Tomcat/8.0.3

If we use REST and Spring HandlerExceptionResolver in case of exception we bump into this problem for sure. Are there any workaround for this problem (iso change http method type)?

4

2 回答 2

4

抱歉,(目前)没有解决方法。我对 EG 的建议是处理的方法是可配置的。那个建议被拒绝了。我建议你向他们提出这个特定的问题,因为这是一个很好的论据,可以让支持的方法在每个 JSP(或 JSP 组)的基础上进行可配置。

同时,我将在您为此提出的错误下使用某种形式的 Tomcat 特定配置来看看使其可配置:https ://issues.apache.org/bugzilla/show_bug.cgi?id=56568

更新:从 Tomcat 8.0.9 开始,当使用 JSP 生成错误页面时,将允许任何 HTTP 方法。

于 2014-05-27T15:12:30.033 回答
3

正如@MarkThomas 所指出的,如果您像这样HTTP声明您的 JSP 页面,您可以提出任何请求errorPage
在此处输入图像描述

如果您不想这样做,那么还有另一种选择
1. 创建一个过滤器(如果您直接要调用JSP页面)或创建一个 servlet(最终将调用JSP页面)
2. 在 doFilter() 或在 servlet doPut()/doDelete() 的情况下, 我在 Filter where is object 中执行此操作。 我用一个假请求来包装原始请求,并告诉它返回POST和请求,所以认为它是一个请求并且页面被执行,唯一的缺点是你将无法分辨原始请求是什么;如果您设置具有原始方法名称的属性,也可以涵盖这一点,如下所示
在此处输入图像描述
requestServletRequest
HttpRequestWrapperDELETEPUTJSPPOST

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);
于 2017-09-29T12:40:03.947 回答