JSP technology of Java Web Servlet Technology of Java Web

The full name of JSP is java server pages. Like Servlet Technology, it is a technology defined by Sun company 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 users with static data, while JSP technology allows java code to be nested in the page to provide users with dynamic data.

1. JSP operation principle

When a user visits a JSP page for the first time, the page will be translated into a servlet source file by the jsp servlet, and then the source file will be translated into Class file. Servlet source files and Class files are generally placed under the current workspace In metadata, you can search the corresponding servlet source files and Class file. The following is a simple JSP program with the file name index jsp

Search the corresponding servlet source files and Class file. The file structure is as follows:

As you can see, index The JSP file is translated into index_ jsp. Java and index_ jsp. Class, open index_ jsp. Java file. The translated servlet source code is as follows:

As you can see from the code, index The servlet class after JSP translation is index_ JSP, which does not implement the servlet interface, but inherits org apache. jasper. runtime. Httpjspbase class. Check the source code of httpjspbase class in Tomcat source file. It can be concluded that httpjspbase inherits httpservlet, that is, index_ The JSP class is also a servlet. The service () in httpjspbase calls directly_ Jspservice () method, that is, index is called_ In JSP_ Jspservice() method.

2. JSP basic syntax

JSP expression

JSP expression is used to output program data to the client. It directly encapsulates the variables or expressions to be output in the tag ending with "<% = expression% >".

JSP script fragment

JSP script fragments refer to one or more Java program codes nested in "<%" and "% >". These Java codes must follow the Java syntax specification, otherwise the compilation will report an error.

JSP declaration

When a JSP page is translated into a servlet program, the script fragments, expressions and template elements contained in the JSP will be converted into servlets_ The program code of the jspservice () method, which is that the variables defined in the JSP script fragment will become_ The program code in jspservice (), at this time, the methods defined in the JSP script fragment will be inserted into_ In jspservice (), this obviously causes syntax errors. To solve this problem, the JSP provides a declaration to "<%!" Start with '% >' end. The format is as follows:

JSP comments

JSP has its own annotation method, and the syntax format is as follows:

3. JSP instruction

  JSP2. 0 defines three instructions: page, include and taglib, and each instruction defines its own attribute.

Page instruction

Main common attributes of the page instruction

Program example using page instruction

Include instruction

Sometimes, when a JSP page needs to contain an HTML file or text file, it can be implemented through the include instruction. The syntax format is as follows:

4. JSP implicit object

In JSP pages, some objects need to be used frequently. Therefore, JSP provides 9 implicit objects. They are created by JSP by default and can be used directly on JSP pages. The following are 9 implicit objects of JSP.

Out object

In JSP pages, when you need to send text content to the client, you can use the out object, which is javax servlet. jsp. Instance object, function and servletresponse of jspwriter The printwrite object returned by getwrite () is similar. The difference is that the out object is a printwrite with caching function, and its buffer size can be set by the page instruction.

Note: after the out object writes data through print, the data of the out object input buffer is really written to the buffer provided by the servlet until the end of the whole JSP page, and the response getWrite(). The print statement writes the content directly to the buffer provided by the servlet.

Pagecontext object

To get an implicit object in a JSP page, you can use the pagecontext object, which is javax servlet. jsp. The instance of pagecontext represents the running environment of the current JSP page and provides some columns for obtaining other implicit objects.

reference material

1. Servlet Technology of Java Web

***

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>