Search code examples
jakarta-eewarapplication-server

Blocking POST/GET Method in a Web application


How to block POST/GET Method in a Web application deployed in application server.

so that http status code 405 [Method Not Allowed] will be thrown when it access via a blocked method.

Description for HTTP Status code [405] : A request was made of a resource using a request method not supported by that resource;[2] for example, using GET on a form which requires data to be presented via POST, or using PUT on a read-only resource.


Solution

  • If by blocking you mean not accepting any incoming GET / POST requests, you have two solutions:

    1. Define a Filter that will block the requests
    2. Do it at the web server level

    In the filter, you could do for instance:

    public void  doFilter(ServletRequest request, 
                          ServletResponse response, 
                          FilterChain chain)
        ((HttpServletResponse) response).sendError(405,
                                    "the error message" );                          
    }