JSP Structure
Web servers require a JSP engine, i.e., a container, to handle JSP pages. The container is responsible for intercepting requests for JSP pages. This tutorial uses Apache with an embedded JSP container to support JSP development.
The JSP container works together with the web server to provide the necessary runtime environment and other services for the normal operation of JSP, and can correctly identify special elements exclusive to JSP pages.
The following figure shows the position of the JSP container and JSP files in a web application.
JSP Processing
The following steps show how a web server uses JSP to create web pages:
-
Just like other ordinary web pages, your browser sends an HTTP request to the server.
-
The web server recognizes that this is a request for a JSP page and passes the request to the JSP engine. This is done by using the URL or the .jsp file.
-
The JSP engine loads the JSP file from disk and converts it into a Servlet. This conversion simply changes all template text to println() statements and converts all JSP elements into Java code.
-
The JSP engine compiles the Servlet into an executable class and passes the original request to the Servlet engine.
-
A component of the web server will call the Servlet engine, then load and execute the Servlet class. During execution, the Servlet produces output in HTML format and embeds it in the HTTP response, which is sent back to the web server.
-
The web server returns the HTTP response to your browser in the form of a static HTML page.
-
Finally, the web browser processes the dynamically generated HTML page in the HTTP response just as it would process a static page.
The steps mentioned above can be represented by the following figure:
Generally, the JSP engine checks whether the Servlet corresponding to the JSP file already exists, and checks whether the JSP file's modification date is earlier than the Servlet. If the JSP file's modification date is earlier than the corresponding Servlet, the container can determine that the JSP file has not been modified and the Servlet is valid. This makes the entire process more efficient and faster compared to other scripting languages (such as PHP).
In summary, a JSP page is another way to write a Servlet without needing to be a Java programming expert. Except for the interpretation phase, a JSP page can almost be treated as an ordinary Servlet.
Other Extensions