为什么servlet不能在Web.xml中映射JSP [英] Why is JSP not mapped in web.xml while servlet does

查看:265
本文介绍了为什么servlet不能在Web.xml中映射JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道servlet和JSP在后门是相同的. JSP也是servlet,因此在执行JSP时,它首先转换为servlet,然后执行servlet.

We know servlet and JSP are same from back doors. JSP is also servlet, so when JSP executed, it first converted into servlet then servlet execution follows.

我的问题是,如果servlet和JSP相同,那么为什么我们在web.xml部署描述符文件中映射servlet,但不映射JSP?

My question is, if servlet and JSP are same then why we map servlet in web.xml deployment descriptor file, but we don't map JSP?

推荐答案

它已经映射到服务器自己的web.xml中,并且已应用于所有Web应用程序.因此,您无需在所有Web应用程序中明确注册它.

It's already mapped in server's own web.xml which get applied on all webapps. So you don't need to explicitly register it in all your webapps.

例如Tomcat,您可以在Tomcat安装的/conf/web.xml文件中找到以下与JSP Servlet相关的条目(行号与Tomcat 8.0.26匹配).

In case of e.g. Tomcat, you can find the below JSP servlet related entries in the /conf/web.xml file of the Tomcat installation (below line numbers match Tomcat 8.0.26).

245    <servlet>
255        <servlet-name>jsp</servlet-name>
256        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
257        <init-param>
258            <param-name>fork</param-name>
259            <param-value>false</param-value>
260        </init-param>
261        <init-param>
262            <param-name>xpoweredBy</param-name>
263            <param-value>false</param-value>
264        </init-param>
265        <load-on-startup>3</load-on-startup>
266    </servlet>

381    <servlet-mapping>
382        <servlet-name>jsp</servlet-name>
383        <url-pattern>*.jsp</url-pattern>
384        <url-pattern>*.jspx</url-pattern>
385    </servlet-mapping>

您会看到,任何与URL模式*.jsp*.jspx匹配的请求都将调用服务器自己的JspServlet,该服务器随后将执行所有JSP工作.

You see, any request which matches the URL pattern *.jsp or *.jspx will invoke the server's own JspServlet which will then do all the JSP works.

Tomcat不了解您的自定义" servlet,因此您不会在其中找到自定义" servlet,因此需要自己在webapp自己的web.xml中进行映射.

Tomcat has no knowledge about your "custom" servlets, so you won't find your "custom" servlets in there and you'd need to map them in webapp's own web.xml yourself.

这篇关于为什么servlet不能在Web.xml中映射JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆