[Servlet & JSP] From JSP to Servlet
JSP and Servlet are both sides of the Integration. JSP will eventually be translated into Servlet Source Code by containers, automatically compiled into. class files, loaded into. class files, and then generated Servlet objects.
The Servlet class after container translation has methods such as _ jspInit (), _ jspDestroy (), and _ jspService (). The name has an underscore prefix, indicating that these methods are maintained during container translation, we should not write these methods. If you want to perform JSP initialization or final work, you should define the jspInit () or jspDestryoy () method.
Indicator Element
The main purpose of the JSP Directive element is to indicate the information that the container must comply with when translating JSP into Servlet source code. The syntax of the indicator element is as follows:
<% @ Indicates the type [property = "value"] * %>
There are three common indication types in JSP: page, include, and taglib.
The page indicates how the container translates the current JSP page. The include indicator type informs the container to include other JSP pages for translation. The taglib indicates how the container translates the Tag Library on this page ).
The indicator element can have many pairs of attributes/values. If necessary, the same indicator type can be set with several indicator elements.
<%@ page import="java.util.Date" %><%@ page language="java" contentType="text/html; utf-8" pageEncoding="utf-8"%>
The import in the page indication type informs the container that the import statement included in the source code must be translated into JSP. You can also use commas (,) to separate multiple import contents in an import statement. The page indicates that the contentType attribute in the type informs the container that setContentType () of HttpServletRequest must be used in JSP translation (), when calling a method, the input parameter is the value of contentType. The pageEncoding attribute is used to inform the container of how to process the text encoding in the JSP page during translation and compilation, and the charset settings attached to the content type. If a webpage contains characters that are not within the ASCII encoding range (for example, Chinese characters), you must specify the correct encoding format to avoid garbled characters.
You can write one row when you use the page indication type, or in the same element.
The page indicator type also has some configurable attributes, which are described as follows:
Info attributes
Used to set the basic information of the current JSP page. This information is finally converted to the information obtained by using getServletInfo () in the Servlet type.
AutoFlush attributes
Used to automatically clear output streams. The default value is true. If it is set to false, an exception occurs when the buffer is full and flush () is not called to send data to the client.
Buffer attribute
This parameter is used to set the size of the output streaming buffer to the client. The unit must be specified when the buffer is set. Inbound buffer = "16kb". The default value is 8 KB.
ErrorPage attributes
Used to set which page to forward to handle this exception when a JSP running error occurs.
Extends attributes
It is used to specify the class that should be inherited after JSP web page translation is a Servlet program. Take Tomcat as an example. By default, it inherits from HttpJspBase (it inherits from HttpServlet ). This attribute is rarely used.
IsErrorPage attributes
Sets whether the JSP page is an abnormal page. This attribute must be used with errorPage.
Language attributes
Specifies the language syntax used by the container to translate JSP pages. However, currently, only Java syntax is used and the default syntax is used.
Session attributes
Sets whether the Servlet Source Code after translation has the statement for creating an HttpSession object. The default value is true. If some pages do not require session management, setting it to false can increase performance.
IsELIgnored
Sets whether to ignore the expression language in the JSP webpage. The default value is false. This setting will overwrite
.
IsThreadSafe attributes
Tells the container whether to notice thread security when writing JSP. The default value is true. If it is set to false, the Servlet after translation will implement the SingleThreadMOdel interface. Each request will create a Servlet instance to serve the request. Although thread security issues can be avoided, this will affect the performance, it is not recommended to set this parameter to false.
The incude type is used to notify the container to translate the content that contains another web page.
<%@include file="/WEB-INF/header.jsp" %><%@include file="/WEB-INF/foot.jsp" %>
When using the include command element to include other webpage content, it is a static inclusion method because the Servlet content after translation is determined during the translation period.
A tag dynamically includes other webpages for response at runtime.
Declaration, Scriptlet, and expression Element
JSP web pages are converted into Servlet classes. After the translation, the Servlet classes should include class members, method declarations, or statements. When compiling JSP, you can use the Declaration statement) element, Scriptlet element, and Expression element.
Declaration Element
The syntax for declaring elements is as follows:
<%! Class member or method declaration %>
In<%!
And%>
The program code declared between them will be converted into class members or methods in the Servlet.
In use<%!
And%>
When declaring variables, you must be careful with data sharing and thread security issues. By default, containers use the same Servlet instance to serve requests of different users. Each request is a thread, and<%!
And%>
The declared variables correspond to the class variable members, so there is a thread sharing access problem.
If you want to execute some initialization operations during JSP loading, You can override the jspInit () method or use jspDestroy () to define the end action. The two methods are defined in<%!
And%>
In this way, the source code of the Servlet after translation will have a corresponding segment.
Scriptlet Element
The Scriptlet element syntax is as follows:
<% Java Statement %>
You can write Java statements in the Scriptlet element, just like writing statements in the Java method. In fact, the content included in the statements will be translated into the Servlet Source Code's _ jspService () method.
HTML directly written in JSP will become the output content of the out object. The order in which the Scriptlet appears, that is, the order in which the statements appear in _ jspService () after the Servlet is translated.
Expression Element
The expression element syntax is as follows:
<% = Java expression %>
The result of the expression operation is directly output as part of the webpage.You do not need to add (;) to the expression element (;).
Implicit object
Implicit object |
Description (Object of the translated object) |
Out |
JspWriter-> PrintWriter |
Request |
HttpServletRequest |
Resposne |
HttpServletResponse |
Config |
ServletConfig |
Application |
ServletContext |
Session |
HttpSession |
PageContext |
PageContext, which encapsulates JSP page Resources |
Exception |
Throwable, which represents the exception object thrown by the JSP page |
Page |
This |
You can use pageContext to set four range attributes, instead of using individual pageContext, request, session, and application. Using pageContext to provide a single API to link the scope of a property, you can use the following methods to set it.
getAttribute(String name, int scope)setAttribute(String name, Object value, int scope)removeAttribute(String name, int scope)
Scope can be specified using the following constants:
PageContext. PAGE_SCOPE pageContext. REQUEST_SCOPE pageContext. SESSION_SCOPE pageContext. APPLICATION_SCOPE
In fact, pageContext is rarely used. pageContext provides a single access interface for containers when translating JSP into Servlet.
Error Handling
After all, JSP will be translated as Servlet, so the error may occur in the following three times:
When JSP is converted to Servlet Source Code
When the Servlet Source Code is compiled, such as a syntax error
Failed to deploy related classes and other Servlets to load containers for service tickets. An error occurred while running.
Such as NullPointerException