1 Introduction
The JSP (Java Server Page) and servlet are the two basic members of the Java EE specification and are the focus of javaweb development.
The nature of the JSP is the servlet, which, when the user sends a request to the specified servlet, uses theoutput stream to dynamically generate the HTML page, including every static HRML tag and all the The content that appears in the HTML page.
JSP page content is composed of 2 parts
- Static section: Standard HTML tags, static page content.
- Dynamic parts: Java -programmed content, which is dynamically generated by Java scripts.
2
working principle
1. the JSP file must be running within the JSP server.
2. The JSP file must be generated by a servlet to execute.
3. The first visitor for each JSP page is slow because the Servlet must be generated by the JSP compilation .
4. A JSP page visitor does not need to install any client (including the Java environment), because the JSP page is delivered to the client's standard HTML page.
3
basic Syntax
The basic syntax of a JSP is similar to Java, with a few special syntax below.
3.1 Notes
<%--JSP comments section, after generating HTML does not display--%><!--add JSP comments, generated after HTML display--
3.2 Declaration Section
<%! Declaration section%> <%--Example--%><%! Public int count = 0; Public String Greet () { count++ ; return "Hello for" + Count + "times!" ; } %>
View Code
member variables and member methods declared in parts of the declaration are converted to member variables and member methods for the Servlet class.
When you enter a JSP page that contains the declaration part more than once , you can see that the value of the Count member variable continues to change. Because all clients share the same servlet class, they share the same member variable and member method. These variables and methods are not saved until the Servlet class is destroyed.
3.3 Output Expression
<%= Expression%><%--Example--%><%--will be output on the page 3--%><%= 1 + 2%><%--will be in page Output 1--%><%! Public int a = 1; %><%= a%>
View Code
3.4 JSP script
You can use <%%> to include any executable Java code. And all executable Java code can be embedded in HTML pages using JSP scripts .
<ul> <% for (int i = 0; i < ten; i++) { %> <li> <p> This is <%= i%> line </p></li> <% }%></ul>
View Code
It is important to note that variables declared in the JSP script section are local variables and cannot be used with public ,private these access control modifiers are not used static decoration.
3.5 3 compiler directives for JSP
- Page: the directive is for the current page
- Include: Used to specify that another page is included
- Taglib: Used to define and access custom labels
Use the above compiled instruction format as follows
<%@ compiler directive Name Property name = "attribute value"%>
3.5.1 page directive
The page directive is located at the top of the JSP page code, and a JSP page can use multiple page directives.
Property name |
Property value |
Default value |
Language |
Declaring the current JSP page using the kind of scripting language |
Java |
Extends |
Specifies the parent class Underlying the Java class that the JSP page compiles from |
No |
Import |
Used to import packages |
Default Import java.lang.*, javax.servlet.*, javax.servlet.jsp.*, javax.servlet.http.* |
ContentType |
Set the file format and coded character set of the generated Web page |
Format: text/html, Character set: Iso-8859-1 |
Pageencoding |
Generate the coded character set for a Web page |
No |
Session |
Set whether this JSP page requires httpsession |
No |
Buffer |
Specifies the size of the output buffer, which can be set to none |
8KB |
AutoFlush |
If the output buffer is about to overflow, it is necessary to force the contents of the output buffer,true for the normal output,false overflow when thrown out |
No |
Info |
Set the information for this JSP program, which can be seen as a description, call Getserletinfo () to get |
No |
ErrorPage |
Specifying error handling pages |
No |
Iserrorpage |
Sets whether this JSP page is an error handler, and if the page is already an error handler, you do not need to set the errorpage value |
No |
Import= "java.util.*"%>
View Code
3.5.2 include directives
The directive can be used anywhere in the file. Using this directive, you can embed an external file into the current JSP file and parse the JSP statements in the page .
The include can contain static pages, or it can contain dynamic jsp pages.
If the embedded file often needs to be changed, it is recommended to use <jsp:include>, because it is a dynamic inclusion.
<%--example--%><%@ include file= "examplejsp"%><jsp:include file= "examplejsp" >
View Code
7 action commands for 3.6 JSP
- Jsp:forward
- Jsp:param
- Jsp:include
- Jsp:plugin
- Jsp:usebean
- Jsp:setproperty
- Jsp:getproperty
3.6.1 forward Directive
Used to forward a page response to another page (static, dynamic, or Servlet).
<%--Example--%><%--JSP 1.0--%><jsp:forward page= "" ><%--JSP 1.1 and above--%><jsp:forward page= "xxx. JSP "> <jsp:param name=" xx "value=" xxx "/></jsp:forward>
View Code
The instruction forwards the request, but the client's request parameter does not change, that is, the address requested by the user does not change.
3.6.2 include directives
The directive is a dynamic include directive.
<%--example--%><jsp:inlcude page= "xx.jsp" flush= "true" > <jsp:param name= "xxxx" value= "xxx"/></ Jsp:include>
View Code
The Flush property is used to specify whether the output cache is transferred to the file being imported.
True to be included in it. false, and vice versa.
3.6.3 Usebean, SetProperty, GetProperty
Pending update
3.7 9 built-in objects in a JSP script
Pending update
4 Servlet
Introduction
A servlet is a server-side applet that runs on a server-side program to process and respond to requests from clients.
The servlet provides different methods for responding to client requests.
- Doget: Responds to a client get request.
- DoPost: Responds to a client post request.
- DoPut: Responds to a put request from the client .
- DoDelete: Responds to the client's delete request.
The other two methods.
- Init (servletconfig config): When you create a servlet instance, the initialization servlet resource for that method is called . When overriding this method, you need to call Super.init (config) first.
- Destroy (): When the servlet instance is destroyed, the recycle resource for the method is automatically called.
5 Filter
Introduction
A servlet that can be seen as " enhanced" , primarily for preprocessing user requests or for post- processing of HttpServletResponse .
function :
- intercept the client's httpservletrequest before HttpServletRequest arrives at the servlet.
- You can also modify the HttpServletRequest header and data as needed to check the HttpServletRequest .
- Intercept HttpServletResponse before HttpServletResponse arrives at the client.
- You can also modify the HttpServletResponse header and data as needed to check the HttpServletResponse .
category : User-authorized filter,log filter,filter that is responsible for decoding, XSLT filter that can change XML content, and so on.
three basic methods :
- void init (filterconfig config): for completion of initialization.
- void Destroy (): The collection of some resources is completed before the filter is destroyed.
- void DoFilter (ServletRequest request, servletresponse response, Filterchain chain): Implements the filtering function, adding additional processing to each request and response.
5.1 Url Rewrite
URL rewrite can be used to implement pseudo-static of URLs.
6 Listener
Common Web Event listener interfaces:
- Servletcontextlistener: Used to listen for Web app startup and shutdown.
- Servletcontextattributelistener: Used to listen for changes in the properties of the ServletContext range (application).
- Servletrequestlistener: Used to listen for user requests.
- Servletrequestattributelistener: Used to listen for changes in the properties of the ServletRequest range (request).
- Httpsessionlistener: Used to listen to the beginning and end of the user session.
- Httpsessionattributelistener: Used to listen for changes in the properties of the HttpSession range (session).
Jsp/servlet related