JSP Technology
JSP Origin
In many dynamic web pages, most of the content is fixed, and only some content needs to be dynamically generated and changed.
If the Servlet program is used to output webpages with only some content that needs to be dynamically changed, and all the static content also needs to be generated by programmers using Java program code, the code of the entire Servlet program will be very bloated, writing and maintenance will be very difficult.
The design of a large number of static content artists and the compilation of related HTML statements are not the work of programmers, and programmers are not necessarily good at this. The design and production personnel of Web Page art cannot complete such work even though they do not understand Java programming.
To make up for Servlet defects, SUN introduced the JSP (Java Server Pages) technology as a solution based on Servlet.
JSP is a technology that simplifies Servlet writing. It combines Java code and HTML statements in the same file and uses Java code to write only the content to be dynamically generated on the webpage, the static content that remains unchanged is written in the form of common static HTML pages.
Establish an intuitive understanding of JSP
A JSP page is a common text file consisting of HTML statements and embedded Java code. The file extension of the JSP page must be. jsp.
The Java code written on the JSP page must be nested in <% and %>, and the Java code nested between <% and %> is called a script segment (Scriptlets ), NO content nested between <% and %> is called a JSP template element.
Java code in JSP can use the out. println statement to output the result strings generated by other Java program code to the client, or use the System. out. println statement to print them to the command line window.
JSP files, like common HTML files, can be placed in any directory in the WEB application except the WEB-INF and its subdirectories, the access path of a JSP page is the same as that of a common HTML page.
What is JSP?
JSP stands for Java Server Pages. Like servle technology, JSP is a technology defined by SUN for developing dynamic web resources.
The biggest feature of JSP technology is that writing jsp is like writing html, but compared with html, html can only provide static content for users, jsp technology allows you to nest java code on pages to provide dynamic data.
Jsp Quick Start: output the current time on the jsp page.
Both JSP and Servlet can be used to develop dynamic web resources. However, due to their respective characteristics, servlet is gradually used as a controller component in web applications in long-term software practices, JSP technology is used as a data display template.
The reason is that the program data is usually beautified before being output:
Making jsp generate dynamic data using java code and beautify it will make the page difficult to maintain.
Making servlet generate both data and embed html code inside to beautify data also results in poor program readability and difficulty in maintenance.
Therefore, the best way is to let these two technologies take responsibility for each of them. servlet is only responsible for responding to requests to generate data and bringing the data to jsp through forwarding technology, data Display jsp.
How JSP works
When the WEB Container (Servlet Engine) receives an access request from a URL with the. jsp extension, it will send the access request to the JSP Engine for processing. The JSP Engine in Tomcat is a Servlet program, which is responsible for interpreting and executing JSP pages.
When each JSP page is accessed for the first time, the JSP Engine translates it into a Servlet Source program, and then compiles the Servlet Source program into a Servlet class file, then, the WEB Container (Servlet Engine) loads and interprets and executes the Servlet program translated from the JSP page in the same way as calling a common Servlet program. Www.2cto.com
Tomcat 5. x place the Servlet source files and class files created for the JSP page in the "<TOMCAT_HOME> \ work \ Catalina \ The JSP specification does not explicitly require that the script program code in JSP be written in Java. The script code in JSP can be written in other scripting languages except Java. However, the JSP page must eventually be converted into a Java Servlet program.
You can pre-compile all the JSP pages into Servlet programs before the WEB application is officially released.
Analyze the Servlet code generated by JSP
The Servlet translated from the JSP page inherits org. apache. jasper. runtime. httpJspBase class: HttpJspBase class is a subclass of HttpServlet. Therefore, the Servlet translated from a JSP page is a grandson of HttpServlet. The HttpJspBase class implements some methods in the javax. servlet. jsp. HttpJspPage interface. Therefore, the HttpJspBase class is abstract.
SUN provides jsp web Container developers and JSP page developers with a set of Java classes specifically used to develop JSP programs. This set of Java classes is called JSP APIs. The HttpJspPage interface and JspPage interface are JSP APIs. Only one _ jspService method is defined in the HttpJspPage interface, but it inherits the JspPage interface. The JspPage interface defines two methods: jspInit () and jspDestroy ().
The init method of HttpJspBase calls the jspInit and _ jspInit methods. The destroy method calls the jspDestroy and _ jspDestroy Methods internally, and the service method calls the _ jspService method internally. The init, service, and destroy methods implemented in HttpJspBase are declared as final types.
Each text segment located on both <%> and <%> of each line on the JSP page is converted into an out line with the text as the parameter. the java code in the write statement and JSP script snippet (a piece of java code located in <%>) is moved to the corresponding position in the _ jspService method, JSP expressions (Content in <% = and %>) are converted to out with the variables or expressions as parameters. print statement.
JSP Execution Process
The execution process of JSP can be divided into the following points:
The client sends a request.
The Web Container translates JSP into Servlet source code.
The Web Container compiles the source code.
The Web container loads and executes the compiled code.
Return the execution result to the client.
JSP implicit object
Public void _ jspService (HttpServletRequest request,
HttpServletResponse response)
Throws java. io. IOException, ServletException
{
JspFactory _ jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
...
...
Throwable exception =
Org. apache. jasper. runtime. JspRuntimeLibrary. getThrowable (request );
If (exception! = Null ){
Response. setStatus (HttpServletResponse. SC _INTERNAL_SERVER_ERROR );
}