As we all know, every JSP page is compiled into a Java class by the Web container for the Web container to call and generate HTML foliar feedback to the user. Understanding the variant methods and rules is very beneficial for us to learn JSP. We can say that we have learned most of the JSP knowledge after learning this compilation principle, the rest of the work is just to memorize some tablib and apply it repeatedly to make yourself more proficient ..
First, let's take a look at the basic structure of the class corresponding to the JSP page. Every JSP page is compiled into the following format. First, let's give a general impression. The detailed description is later.
public class My$jsp extends HttpJspBase {
static {}
public date$jsp() {}
private static boolean _jspx_inited = false;
public final void _jspx_init()
throws org.apache.jasper.runtime.JspException {};
public void _JSP pageservice(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;
String _value = null;
try {
if (_jspx_inited == false) {
synchronized (this) {
if (_jspx_inited == false) {
_jspx_init();
_jspx_inited = true;
}
}
}
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=ISO-8859-1");
pageContext = _jspxFactory.getPageContext(this, request, response,
"", true, 8192, true);
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
// HTML // begin
out.write("""The date is/r/n");
// end
// begin
out.println((new java.util.Date()).toString());
// end
// HTML // begin
out.write("/r/n // end
} catch (Throwable t) {
if (out != null && out.getBufferSize() != 0) {
out.clearBuffer();
}
if (pageContext != null) {
pageContext.handlePageException(t);
}
} finally {
if (_jspxFactory != null) {
_jspxFactory.releasePageContext(pageContext);
}
}
}
}
We can clearly see that the most important function here isPageserviceAfter the Web Container compiles a JSP class, it applies for the Class Object and directly calls pageservice to obtainResponseAnd then return it to the customer. We can summarize the details as follows:
- All the classes translated from JSP pages areHttpjspbaseAnd namedPagename $ JSP
- When the pageservice function is called for the first time, the class will be initialized. The initialization function is_ Jspx_initIf you want to, you can also customize this function to initialize the JSP page.
- <%> What is this code converted?
Such code is directly converted into Java code and put into the pageservice function.
- <%! %> What is this code converted?
Such code is translated into member functions and member variables. That is to say, these declarations exist throughout the lifecycle of JSP.
- What about HTML code?
HTML code is directly writtenPrintwriterTo the user. Very direct
- Why are there so many write methods for JSP pages, such as session, out, page, and context.
This is all inPageserviceThe temporary variables defined in. For specific initialization, refer to the above example code. Each time the JSP page is called, these variables will be reinitialized once. Of course, we can also easily declare our own variables.
- Save writing mode <% = object. dosomething () %> so understand? This method calls the tostring () of the object obtained by dosomething, and then directly writes it to out. Equivalent:
Out. Print (object. dosomethiing (). tostring ())
- Scope in JavaBean defines the scope. What does this scope mean?
This is the meaning of the place where the bean object handle is saved. We can imagine thatPageThe bean in the range is only a local variable in pageservice. After a processing, the variable will be recycled by the Java Virtual Machine. The session variable. WhileRequestThe bean at the level should be a member variable of the JSP page. Session and application cannot be saved in the JSP page class, but should be saved in the call object of the JSP page.
- For the <% @ Page %> Command, this is too simple, but one by one correspondsResponse. setcontenttype ().
- About JSP page redirection. This statement is translated into getservletcontext (). getrequestdispatcher ("/list. jsp"). Forward (req, Res); statement.
- <% @ Include file = "embedded ded. jsp" %> In this statement, the JSP translator will mix the code of this file with the code of the current file and compile it together to generate a JSP class. This method is good. Let's unify the Document Style. For example, let's write the header into a file, and write the footer into a JSP and include these two files in index.html, no matter how the content changes, the up and down styles will not change, which is conducive to the unification of styles.
The above is a brief discussion of the JSP translation process. For more details, refer to the source code of Tomcat. Understanding these principles is very important for learning JSP and can greatly improve the learning efficiency.