码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-05-06 11:44:59      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:tps   requests   xhtml   exception   let   nts   extend   rri   tac   

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)方法中去了.

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

标签:tps   requests   xhtml   exception   let   nts   extend   rri   tac   

原文地址:http://www.cnblogs.com/whatlonelytear/p/6815835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!