The JSP(Java Server page)Java Service-side web page, which is a dynamic web-page technology, is the same as the functionality of ASP in. NET. JSP is a set of standards set by Sun , and also one of the specifications of the Java EE . Say something that's not right. "JSP is an upgrade to HTML , an extension, that is, static--> dynamic."
One, good, simple understanding of the JSP concept, we look at the principle and nature of JSP:
1, first look at an example, we visit the Home pageof this Index. JSP , the execution order of the program:
2, the principle and nature of JSP derived from the above example:
In the example aboveIndex_jsp.classclass inherits Httpjspbase, and httpjspbase inherits HttpServlet, so JSP is essentially a Servlet, and The Servlet is exactly the same. There are three stages of JSP: translation (Once), compilation (once), running (multiple). This also explains why the first time we visited a Jsp page, it was slow to run and the speed of access was very fast. But there is a big difference between the two: theJSP extracts the data to do the page display, namely the so-called View(presentation layer), and the Servlet mainly completes the business logic processing and the resource jump, namely the so-called Con troller ( control layer ).
two, since it is one of the specifications of the Java EE , then What are the basic rules of JSP syntax?
these are the specifications that need to be followed, and nine of the big built-in objects are especially important, so let me give you a quick introduction to these pieces of knowledge:
1,scriptLet (little): is the use of <%%> in the JSP file, the Java program that appears in the symbol is translated and placed in the Service method of the Servlet . That means we can put the Java program we want to know .
2,declaration ( declaration ): Just use <% in the JSP file ! %> The Java program that appears within the symbol will be stored in the same location as the Servlet 's service method. You can declare static variables, static methods, static code blocks, instance code blocks, instance variables, instance methods within the symbol.
3,expression: is the use of <%=%> in the JSP file , equivalent to out.print (); Generally, we need to print variables here, because all of the JSP inside is a string.
4, instruction (Directive): designed for the JSP engine, they do not directly produce any visible output, but simply tell the engine how to handle the JSP The rest of the page, such as some encoding, how to respond, and so on.
A, the most common page directive <% @page%>:
- Import attribute (translation of the import statement in the Java language generated)
- <% @page import= "Java.util.date,java.util.arraylist"%>
- ContentType Property (Specifies the JSP page Response content Type)
- <% @page contenttype= "text/html"%>
- Pageencoding Property (specifies the page character encoding of the JSP page response)
- <% @page pageencoding= "GB18030"%>
- Session Property (specifies whether the current JSP page can use the session's built-in object)
- <% @page session= "false"%> session built-in object is not available
- <% @page session= "true"%> session built-in object available ( default )
- ErrorPage Property (Specifies the resource path to jump after the current JSP page has an error)
- Iserrorpage Property (specifies that the current JSP page is an error page in order to use the built-in object exception)
- The Iselignored property specifies whether an EL expression in the current page is ignored, true means ignore, andfalse indicates that no
b,include directive <% @include%>:
Include directives represent static inclusions , such as writing <% @include file= "b.jsp"%> in a.jsp, which means the a.jsp of the translated The Java source program and the Java source program generated by the b.jsp translation are merged together and then compiled to generate a . class file. We often use this approach to make pages reusable. It's a bit like a master page in. NET.
c,taglib directive <% @taglib%>:
in order to make the JSP look more professional (just do the page display), reduce the number of Java code in the JSP page , we introduced the tag library. The tag library here can be a JSTL function library , or we can write it ourselves. About JSTL reference JSTL Baidu Branch hundred .
5, Actions (action)
A,forward action:
<jsp:forward page= "/2.jsp"/>
forwarding: in the same Servlet
Request.getrequestdispatcher ("/2.jsp"). Forward (REQ,RESP) method.
b, include action (note vs. include directive, one is static, one is dynamically included)
<jsp:includepage= "/b.jsp" ></jsp:include>
The Include action represents the dynamic inclusion , if the above code is written in the a.jsp page, the principle is to translate the a.jsp file into java source program and compile it to generate a a.class file, b.jsp file translation to generate Java source program, compile and build b.class file, and then through A.class to dynamically include B.class.
C,Usebean,setProperty,getProperty action
<usebean id= "C" class= " Full class name"scope=page/request/session/application" />
<setproperty name= "C" property= "name" value= "Jack"/>
<getproperty name= "C" property= "name"/>
The Usebean,setProperty, andgetProperty actions are intended to simplify Java code in JSP pages , creating Java objects as Usebean action . To create a Usebean class in a Range object , assign a value to a property and read a property.
6, take a look at the nine built-in objects in JSP, since it is a built-in object, which means that these objects are already declared in the Java program of the JSP bottom . We can directly use: more nine built-in objects
7,EL(Expression Language) expression: mainly in order to reduce the JSP page JAVA Code, make it more professional to do the page display role:
expression:${}
function: The main is to take data from the Range object, used to display on the JSP page
Example:
Request.setattribute ("sysTime", New Date ()); Save
<%=request.getattribute ("SysTime")%>// fetch data and output to browser
${systime} // use EL expression to fetch data and output to browser
For the use of specific El expressions, see this blog:el expression .
Based on a simple summary of JSP,JSP is a Java Web Application Dynamic page display layer, play a role in interaction with the user, and inside these grammar, or norms, we need to follow these to write.
The study of JSP