Why is JSP not mapped in web.xml while servlet doe

2020-02-14 10:17发布

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.

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?

1条回答
Emotional °昔
2楼-- · 2020-02-14 11:04

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.

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>

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 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.

查看更多
登录 后发表回答