URL mapping in Tomcat to FrontController servlet

后端 未结 4 1814
太阳男子
太阳男子 2021-01-04 16:47

I\'m trying to follow the pattern at Design Patterns web based applications. It all works fine part from when it comes to maping \"root\" URLs.

I\'d like to put all

4条回答
  •  佛祖请我去吃肉
    2021-01-04 17:12

    You can extend the DefaultServlet of your web server.The extended servlet will be your front controller. In the doGET or doPOST method forward your static pages to the super class. DefaultServlet is the servlet that is mapped to url "/" by default. I have used it with jetty server but it can be implemented in tomcat as well.

    public class FrontController extends DefaultServlet {
    
    @Override
    public void init() throws UnavailableException {
        super.init();
    }
    
    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
    
        String uri = request.getRequestURI();
    
        /*
         * if request is trying to access inside /static then use the default
         * servlet. YOU CAN USE YOUR OWN BUSINESS LOGIC TO FORWARD REQUESTS 
         * TO DEFAULTSERVLET
         */
        if (uri.startsWith("/static/")) {
    
            super.doGet(request, response);
            return;
        } else {
    
            // else use your custom action handlers
        }
    }
    

    }

    In the above code samples I have forwarded all the requests starting with /static/ to the default servlet to process. In this way you can map the FrontController to "/" level .

    
    
    FrontController
    FrontController
    FrontController
    

    
    FrontController
    /
    

提交回复
热议问题