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 [file="/date.jsp";from=(0,0);to=(7,6)]
out.write("" +
"" +
"The date is");
// end
// begin [file="/date.jsp";from=(7,8);to=(7,57)]
out.println((new java.util.Date()).toString());
// end
// HTML // begin [file="/date.jsp";from=(7,59);to=(10,7)]
out.write(" ");
// 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 is pageservice. After a web Container compiles a JSP class,
Apply for the object of this class, and directly call pageservice to obtain the Response, and finally return it to the customer.
We can summarize the details as follows:
- All the classes translated from JSP pages are inherited from HttpJspBase and named PageName $ jsp.
- When the pageservice function is called for the first time, this class will perform initialization, and this initialization function is _ jspx_init. If we want to, we 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?
The html code is directly written into PrintWriter and fed back to the user. Very direct
- Why are there so many write methods for JSP pages, such as session, out, page, and context.
These are all temporary variables defined in pageservice. For more information about initialization, see the example code above. Each time you call the JSP page, these variables are reinitialized. 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 that a Bean in the page range is only a local variable in pageservice. After a processing is completed, this variable will be recycled by the Java Virtual Machine. The session variable. The request-level Bean 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. It is only a one-to-one statement corresponding to response. 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.