美文网首页
Web容器默认的servlet

Web容器默认的servlet

作者: 水煮鱼又失败了 | 来源:发表于2020-05-20 08:22 被阅读0次

    目录

    [TOC]

    1.1 各类Web服务器默认servlet名称

    Web容器 默认servlet名称
    Tomcat, Jetty, JBoss, and GlassFish default
    Google App Engine _ah_default
    Resin resin-file
    WebLogic FileServlet
    WebSphere SimpleFileServlet

    1.2 查看默认servlet

    以tomcat8为例,conf/web.xml文件中有如下配置。拦截了所有的请求:

    首先所有的请求进入tomcat,都会经过servlet,如果没有匹配到任何应用指定的servlet,那么就会流到默认的servlet。

    <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    ......
    
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    springMvc有个标签:

    <mvc:default-servlet-handler/>
    

    标签说明如下:

    Configures a handler for serving static resources by forwarding to the Servlet container's default Servlet.
    Use of this handler allows using a "/" mapping with the DispatcherServlet while still utilizing the Servlet
    container to serve static resources.
    This handler will forward all requests to the default Servlet. Therefore it is important that it remains last
    in the order of all other URL HandlerMappings. That will be the case if you use the "annotation-driven" element
    or alternatively if you are setting up your customized HandlerMapping instance be sure to set its "order"
    property to a value lower than that of the DefaultServletHttpRequestHandler, which is Integer.MAX_VALUE.

    注意点,此标签作用,为将静态资源,转发到Servlet容器的默认Servlet。对应源码如下:

    文件DefaultServletHttpRequestHandler:

    public class DefaultServletHttpRequestHandler implements HttpRequestHandler, ServletContextAware {
    
        /** Default Servlet name used by Tomcat, Jetty, JBoss, and GlassFish */
        private static final String COMMON_DEFAULT_SERVLET_NAME = "default";
    
        /** Default Servlet name used by Google App Engine */
        private static final String GAE_DEFAULT_SERVLET_NAME = "_ah_default";
    
        /** Default Servlet name used by Resin */
        private static final String RESIN_DEFAULT_SERVLET_NAME = "resin-file";
    
        /** Default Servlet name used by WebLogic */
        private static final String WEBLOGIC_DEFAULT_SERVLET_NAME = "FileServlet";
    
        /** Default Servlet name used by WebSphere */
        private static final String WEBSPHERE_DEFAULT_SERVLET_NAME = "SimpleFileServlet";
        
        ......
            
        @Override
        public void handleRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            RequestDispatcher rd = this.servletContext.getNamedDispatcher(this.defaultServletName);
            if (rd == null) {
                throw new IllegalStateException("A RequestDispatcher could not be located for the default servlet '" +
                        this.defaultServletName + "'");
            }
            rd.forward(request, response);
        }
    

    1.3 tomcat解析JSP对应的servlet

    用途:解析JSP

    <servlet>
        <servlet-name>jsp</servlet-name>
        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
        <init-param>
            <param-name>fork</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>xpoweredBy</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>3</load-on-startup>
    </servlet>
    ......
    <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>*.jsp</url-pattern>
        <url-pattern>*.jspx</url-pattern>
    </servlet-mapping>
    

    相关文章

      网友评论

          本文标题:Web容器默认的servlet

          本文链接:https://www.haomeiwen.com/subject/rqfbohtx.html