The JSP full name is Java Server Pages, which, like Servle technology, is a technology defined by sun to develop dynamic Web resources. The biggest feature of JSP technology is that writing JSP is like writing HTML, but it can only provide static data to the user compared to HTML, while JSP technology allows to nest Java code in the page and provide dynamic data to the user.
1. JSP Operating principle
When a user accesses a JSP page for the first time, the page is translated into a servlet source file by Jspservlet and the source file is translated into a. class file. The servlet source files and. class files are generally placed in the. Metadata of the current work space, where you can search for the corresponding servlet source files and. class files. The following is a simple JSP program, the file name is called index.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%>
Search for the corresponding servlet source and. class files, and the file structure is as follows:
As can be seen, the index.jsp file was translated into Index_jsp.java and Index_jsp.class, open Index_jsp.java file, translated servlet source code as follows:
/* * Generated by the Jasper component of Apache Tomcat * Version:apache tomcat/8.0.30 * Generated at:2016-05-27 01:49:5 2 UTC * Note:the Last modified time of this file is set to * the last modified time of the source file after * Generation to assist with modification tracking. */package org.apache.jsp;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;p ublic final Class Index_jsp extends Org.apache.jasper.runtime.HttpJspBase implements Org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports {private static Final Javax.servlet.jsp.JspFactory _jspxfactory = Javax.servlet.jsp.JspFactory.getDefaultFactory (); private static java.util.map<java.lang.string,java.lang.long> _jspx_dependants; private static final java.util.set<java.lang.string> _jspx_imports_packages; private static final java.util.set<java.lang.string> _jspx_imports_classes; static {_jspx_imports_packages = new java.util.hashset<> (); _jspx_imports_packages.add ("Javax.servlet"); _jspx_imports_packages.add ("Javax.servlet.http"); _jspx_imports_packages.add ("javax.servlet.jsp"); _jspx_imports_classes = null; } private volatile javax.el.ExpressionFactory _el_expressionfactory; private volatile Org.apache.tomcat.InstanceManager _jsp_instancemanager; Public java.util.map<java.lang.string,java.lang.long> getdependants () {return _jspx_dependants; } public java.util.set<java.lang.string> Getpackageimports () {return _jspx_imports_packages; } public java.util.set<java.lang.string> Getclassimports () {return _jspx_imports_classes; } public javax.el.ExpressionFactory _jsp_getexpressionfactory () {if (_el_expressionfactory = = null) {Synchroniz Ed (this) {if (_el_expressionfactory = = null) {_el_expressionfactory = _jspxfactory.getjspapplicationcon Text (Getservletconfig (). Getservletcontext ()). Getexpressionfactory(); }}} return _el_expressionfactory; } public Org.apache.tomcat.InstanceManager _jsp_getinstancemanager () {if (_jsp_instancemanager = = null) {SYNCHR Onized (This) {if (_jsp_instancemanager = = null) {_jsp_instancemanager = Org.apache.jasper.runtime.Insta Ncemanagerfactory.getinstancemanager (Getservletconfig ()); }}} return _jsp_instancemanager; } public void _jspinit () {} public void _jspdestroy () {} public void _jspservice (final JAVAX.SERVLET.HTTP.HTTPSERVL Etrequest request, final javax.servlet.http.HttpServletResponse response) throws Java.io.IOException, Javax.servlet . servletexception {final java.lang.String _jspx_method = Request.getmethod (); GET ". Equals (_jspx_method) &&!" POST ". Equals (_jspx_method) &&!" HEAD ". Equals (_jspx_method) &&!javax.servlet.dispatchertype.error.equals (Request.getdispatchertype ())) { Response.senderror (httpservletresponse.sc_method_not_allowed, "JSPs Only permit GET POST or HEAD "); return;} Final Javax.servlet.jsp.PageContext PageContext; Javax.servlet.http.HttpSession session = NULL; Final Javax.servlet.ServletContext Application; Final Javax.servlet.ServletConfig config; Javax.servlet.jsp.JspWriter out = null; Final Java.lang.Object page = this; Javax.servlet.jsp.JspWriter _jspx_out = null; Javax.servlet.jsp.PageContext _jspx_page_context = null; try {response.setcontenttype ("text/html; Charset=utf-8 "); PageContext = _jspxfactory.getpagecontext (this, request, response, NULL, True, 8192, true); _jspx_page_context = PageContext; application = Pagecontext.getservletcontext (); Config = Pagecontext.getservletconfig (); Session = Pagecontext.getsession (); out = Pagecontext.getout (); _jspx_out = out; Out.write ("\ r \ n"); Out.write ("\ r \ n"); Out.write ("As seen from the code, the index.jsp translated Servlet class is index_jsp, which does not implement the Servlet interface, But inherited the Org.apache.jasper.runtime.HttpJspBase class, in the Tomcat source file to view the Httpjspbase class source code can be derived: Httpjspbase inherited HttpServlet, that is, index The _jsp class is also a servlet. The service () in Httpjspbase calls the _jspservice () method directly, which is called the _jspservice () method in index_jsp.
2. Basic JSP syntaxJSP expressions
A JSP expression is used to output program data to the client, and the variable or expression that it will output is encapsulated directly in a tag that ends with "<%= expression%>".
<%= expression%>
JSP script Fragment
JSP script fragment refers to one or more Java program code nested in "<%" and "%>", these Java code must follow the Java syntax specification, otherwise compile an error message.
<% int num = 1; OUT.PRINTLN (num);%>
JSP declaration
When a JSP page is translated into a servlet program, the JSP contains script fragments, expressions, and template elements that are converted to the program code of the _jspservice () method in the servlet, which is that the variables defined in the JSP script fragment become _jspservice () , the methods defined in the JSP script fragment are inserted into _jspservice (), which clearly causes a syntax error. In order to solve this problem, the JSP provides a declaration to "<%!" At the beginning, the "%>" ends. The format is as follows:
<%! Java Code%>
JSP annotations
JSP has its own way of commenting, the syntax format is as follows:
<%--Annotation Information--%>
3. JSP directivepage, include, and taglib three directives are defined in JSP2.0, each of which defines its own properties.
Page directive
<%@ Page Property name = "Property value"%>
Main common Properties of page directives
Property name |
Temperament Range |
Function |
Language |
Java |
The language used by the JSP file, the default Java |
Import |
Any registration, class name |
Specify the imported package or class |
Session |
True or False |
Whether the JSP built-in Session object, the default session property is True |
Buffer |
None or digital +kb |
Specify the cache size, which is the out buffer size |
Iserrorpage |
True or False |
Whether the page is an error handling page |
ErrorPage |
Path to a JSP page |
Develop an error handling page |
Using the Page directive program sample
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" utf-8 "%><%@ page import=" Java.util.Date "%>
Include directives
Sometimes the JSP page needs to include an HTML file, a text file, can be implemented by the include directive, the syntax format is as follows:
<%@ include file= "Relativeurl"%>
4. JSP Implicit ObjectJSP pages, some objects need to be used frequently, so JSP provides 9 implicit objects, they are created by default JSP, can be used directly on the JSP page, the following is the JSP 9 implicit objects.
Implicit object name |
type |
function |
out |
Javax.servlet.jsp.JspWriter |
page output |
request |
javax.servlet.http.request |
get user request information |
response |
javax.servlet.http.Response |
server response Information | /tr>
config |
javax.servlet.ServletConfig |
Server configuration to get initialization parameters |
SE Ssion |
javax.servlet.http.HttpSession |
Save user information |
application |
JAV Ax.servlet.ServletContext |
information shared by all users |
page |
java.lang.Object |
when Previous page converted Servlet class instance |
pagecontext |
javax.servlet.jsp.PageContext |
jsp page Container |
exception |
java.lang.Throwable |
JSP page exception occurred, only in error page function |
/table>
Out Object
In a JSP page, when you need to send text content to a client, you can use an Out object, which is an instance object of Javax.servlet.jsp.JspWriter, action, and Servletresponse.getwrite () The returned Printwrite object is similar. In contrast, an out object is a printwrite with a cache function, and its buffer size can be set by a page directive.
Note: After the Out object writes data through print, it knows that the entire JSP page ends and the data for the out-object input buffer is actually written to the servlet-supplied buffer, and 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 an instance of Javax.servlet.jsp.PageContext, represents the current environment of the JSP page, and provides some methods for retrieving other implicit objects.
Method |
Function |
Jspwrite Getout () |
Get an out implicit object |
Object GetPage () |
Gets the page implicit object |
ServletRequest Getrequest () |
Get request Implicit Object |
Servletresponse GetResponse () |
Get response Implicit Object |
HttpSession getsession () |
Gets the session implicit object |
Exception getexception () |
Get exception Implicit object |
ServletConfig Getservletconfig () |
Get config Implicit object |
ServletContext Getservletcontext () |
Get application Implicit Object |
JSP Technology of Java Web