Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a JSP page detected and converted to a Servlet by Tomcat?

Tags:

java

jsp

tomcat

Is a JSP page detected by the page extension of .jsp only? Is there any other way it can be detected?

like image 824
pvsk10 Avatar asked May 05 '11 03:05

pvsk10


People also ask

How does JSP page converted to servlet give an example?

html. The JSP engine loads the JSP page from disk and converts it into a servlet content. This conversion is very simple in which all template text is converted to println( ) statements and all JSP elements are converted to Java code. This code implements the corresponding dynamic behavior of the page.

How a JSP page is converted into servlet and response is generated for the client?

A JSP container is responsible for converting the JSP page into a servlet (known as the JSP page implementation class ) and compiling the servlet. These two steps form the translation phase . The JSP container automatically initiates the translation phase for a page when the first request for the page is received.

Is JSP gets converted into servlet during the deployment process?

Note: When a JSP is accessed through a web browser, Tomcat decides whether to transforms a JSP into its Java servlet version ( . java file) and then compiles the Java servlet (i.e., resulting in a . class file). We do not need to compile a JSP file.


2 Answers

JSP pages in Tomcat are handled by a specific servlet that is meant to handle all requests that terminate with .jsp or .jspx in the HTTP request. This configuration exists in the global $CATALINA\conf\web.xml file where one can find the following significant lines. Note that is for Tomcat 6.

JSP Servlet registration

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

JSP Servlet URL mapping

<!-- The mapping for the JSP servlet -->
<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jspx</url-pattern>
</servlet-mapping>

You could possibly add more URL mappings for other file extensions that are not already mapped to other servlets, in order to trigger the Jasper compiler, that is eventually responsible for translation of the JSP files into corresponding Java servlets, which are then compiled (using the Eclipse JDT compiler, by default). More information on configuring some of the options in the process can be obtained from the Tomcat documentation on configuring Jasper.

like image 90
Vineet Reynolds Avatar answered Oct 05 '22 22:10

Vineet Reynolds


Here's a brief introduction from Built In Servlet Definitions section in $TOMCAT_HOME/conf/web.xml

The JSP page compiler and execution servlet, which is the mechanism
used by Tomcat to support JSP pages.  Traditionally, this servlet
is mapped to the URL pattern "*.jsp".

And JSP page detection is done via servlet mapping (Built In Servlet Mappings section in $TOMCAT_HOME/conf/web.xml):

<!-- The mapping for the JSP servlet -->
<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jspx</url-pattern>
</servlet-mapping>
like image 35
LiuYan 刘研 Avatar answered Oct 06 '22 00:10

LiuYan 刘研