Web容器默认的servlet

1.1 各类Web服务器默认servlet名称
Web容器默认servlet名称
Tomcat, Jetty, JBoss, and GlassFishdefault
Google App Engine_ah_default
Resinresin-file
WebLogicFileServlet
WebSphereSimpleFileServlet
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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值