servlet dispatcher .forward(request, response); 进入其它servlet【原】

dispatcher .forward(request, response); 进入其它servlet

假如我们的web.xml配置如下

<servlet>
        <servlet-name>index</servlet-name>
        <servlet-class>king.server.IndexServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>index</servlet-name>
        <url-pattern>/indexHtml</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>allServlet</servlet-name>
        <servlet-class>king.server.HttpRequestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>allServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

IndexServlet代码如下

public class IndexServlet  extends HttpServlet{
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        try {
            TraceTool.traceParent();
            RequestDispatcher dispatcher = request.getRequestDispatcher("/index.html");
            dispatcher .forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

此时执行完IndexServlet的dispatcher .forward(request, response);后相当于去请求了contextPath/index.html,

于是被<url-pattern>/*</url-pattern>的allServlet拦截下来.又进到了allServlet的get(xxx)方法中去了.

posted @ 2017-05-06 09:57  苦涩泪滴  阅读(378)  评论(0编辑  收藏  举报