1 各类Web服务器默认servlet名称
Web容器 | 默认servlet名称 |
---|---|
Tomcat, Jetty, JBoss, and GlassFish | default |
Google App Engine | _ah_default |
Resin | resin-file |
WebLogic | FileServlet |
WebSphere | SimpleFileServlet |
如果 web.xml 中配置了某个 servlet 拦截了所有的请求,那么一些静态资源的访问如 jpg,css,js 也会被处理。
如果不希望这些请求被你配置的servlet处理,那么可以把服务器自带的默认 servlet 先去拦截掉这些资源,但是要保证默认的 servlet 写在你配置的 servlet 前面。
如果不希望这些请求被你配置的servlet处理,那么可以把服务器自带的默认 servlet 先去拦截掉这些资源,但是要保证默认的 servlet 写在你配置的 servlet 前面。
查看默认servlet
以 tomcat 8 为例,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>
在默认的情况下,默认 servlet 在 Web 应用启动时被装载,目录列表可被使用,日志调试功能被关掉。
spring MVC有个标签:<mvc:default-servlet-handler/>
标签说明如下:
Configures a handler for serving static resources by
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
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); }
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>
在tomcat中,什么是DefaultServlet?功能是什么?
首先所有的请求进入tomcat,都会流经servlet,如果没有匹配到任何应用指定的servlet,那么就会流到默认的servlet
应用的一些静态资源就可以交给该servlet去处理,以减轻服务器压力,节省资源! 如果你用SpringMVC。就会有感觉,当你配置 org.springframework.web.servlet.DispatcherServlet 拦截所有请求时,对静态资源的请求也会被拦截,这时你就可以把静态资源交给 DefaultServlet 来处理!========================================
1.服务器启动实例化Servlet配置
Servlet的何时创建:默认第一次访问时创建。
为什么是默认?
当在servlet的配置时 加上一个配置 servlet对象在服务器启动 时就创建。
2.缺省Servlet
可以将url-pattern配置一个 / ,代表该servlet是缺省的servlet
3. 什么是缺省的servlet?
当你访问资源地址所有的servlet都不匹配时, 缺省的servlet负责处理
其实,web应用中所有的资源的响应都是servlet负责,包括静态资源。
========================================
链接:https://www.jianshu.com/p/792fa4ff28f0
https://www.cnblogs.com/liaojie970/p/7735687.html
https://www.cnblogs.com/liaojie970/p/7735687.html