Analysis of JSP page translation principles

Source: Internet
Author: User

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 will be compiled into the following format. First, let's give a general impression. The detailed description is below.

The most important function is pageservice. After the web Container compiles a JSP class, it applies for the Class Object, directly calls pageservice to obtain the Response, and finally returns it to the customer. We can summarize the details as follows:

1. All the classes translated from JSP pages are inherited from HttpJspBase and named PageName $ jsp.

2. 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.

3. What is this code converted?
Such code is directly converted into Java code and put into the pageservice function.

4. <%! %> 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.

5. What about HTML code?
The html code is directly written into PrintWriter and fed back to the user.

6. Why does JSP page translation save a lot of writing methods, 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.

7. What is the meaning of the Save write method <% = object. doSomething () %>?
This method calls the toString () of the Object obtained by doSomething, and then directly writes it to out. Equivalent:
Out. print (object. doSomethiing (). toString ())

8. scope in JavaBean defines the scope. What does this scope mean here?
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.

9. For the <% @ page %> Command, this is too simple. It is only a one-to-one statement corresponding to response. setContentType.

10. Question about JSP page translation turning. This statement is translated into getServletContext (). getRequestDispatcher ("/List. jsp"). forward (req, res); statement.

11. <% @ include file = "encoded ded. jsp "%> in case of 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 page 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.

  1. Establishment of JSP development environment
  2. Brief Introduction to JSP environment Configuration
  3. JSP, ASP, and PHP security programming
  4. Integrate JSP and PHP in Apache
  5. Security issues of JSP and Servlet applications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.