JSP usage and code examples, and parsing the jump process

Source: Internet
Author: User


Jsp:

Life cycle:
1 The construction method of the current class is executed first
2 Execute the Init () method again
3 Execute service ()/doget () method
4 Execution of the Destroy () method when closing

JSP is the abbreviation of Javaservletpage

The nature of the JSP is a servlet, which is a special Java file
Servlet: the usual. Java class, inherited from the HttpServlet class, and must overwrite its service () or Doget () method

JSP is to write Java statements in HTML

The parsing process of JSP at Tomcat Runtime:

**.jsp---> Tomcat parse out **.jsp <%%>,<%@%>,<%=%>,<%! %&gt, and generate a Java (servlet) file---> **.java---> Javac compile, Generate **.class---> **.class---> Load into the JVM virtual machine, Parse out a virtual HTML table----> browser to display the virtual HTML table---> When we commit, it is in the browser to find, loaded into the JVM virtual machine inside the **.class (servlet file)---> and then perform the operation

Four different forms:
<%%>, <%! %>, <%@%>, <%=%>

1 <%%>://Inside can write arbitrary Java code is the Doget () method or service () method (the same two methods) (Java code to add ";") Semicolon, because it is the method body, how to write and how to write it)
The content inside is the method body that writes the Doget () method
int accesscount=0;//is a local variable

2 <%! %>://class constituent elements
The variable declared here is a member variable
The declared method is a member method
Equivalent to the class element inside the Servlet class
Member variables, multiple clients accessing the same copy each thread has a cache of member variables and then this cache is periodically synchronized with the main thread
So, try not to use member variables!!!!
int accesscount=0;//member Variable

3 <%@%>://To guide the package and set the encoding format
Such as:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "Utf-8"%>

4 <%=%>://For direct output, that is out.println ();
The content inside is out.println ("content") to output the content of the string, (so do not add a semicolon---";")
<%= request.getparameter ("name")%>
↓↓↓↓↓↓
<% out.println (Request.getparameter ("name")); %>


Servlet:

Servlets are writing HTML statements in Java

Include_file://static inclusions

Static inclusion: When the JSP compiler compiles, it already contains the corresponding file and generates only one Java (servlet) file--Javac the same class file when the JVM is running, he cannot pass two between the two, they use the same request,reponse (equivalent to merging two files into one file)

<%@ include file= "file name. jsp" where the%>;//is statically included, where to merge the specified files (location)

ErrorPage://Page Error/Jump File/Jump page

Error page: When the current file error, jump to the specified file, but specify the file settings (encoding ...), must be consistent with the current file, and must indicate that the specified file
such as: <%@ page errorpage= "file. jsp"%>//error page

Included files (Files specified): <%@ page iserrorpage= "true"%>//indicates that I am the specified file (the file to jump to) Iuserservice

Include_page://Dynamic loading

Note: 1 JSP tags need to be closed by themselves
2 jsp:include is dynamic loading! Loading the page and loading the page when the JSP is compiled will be parsed into two Java (servlet) files!
3 Javac compiler compiles two times to generate two class files
And then, in the process of running the JVM virtual machine, this time contains a file to go back to call the B class file
This is the dynamic loading
4 They are not the same request object!!!!!!!
5 definitely needs flush.
6 The problem of the transfer value

Such as:
<jsp:include page= "_02_date.jsp" flush= "true"/>//dynamic loading
<jsp:param value= "<%=value1%>" name= "v1"/>//parameters

Sendredirect:

Jump
Using the promised (response) call, so the JVM virtual machine has been out, and then the specified page address bar will change
Does not prevent the following run
is equivalent to the Doget () method of the generated Java file.
Response.sendredirect ("Specify file. jsp"), or//used to agree (response), so the JVM virtual machine has been out, then the specified page
Because he was working on the browser. So the browser will show the address of the second page
It also means they're definitely not the same object.
They're different request objects.

Forward:

Jump
Call using request, so in the JVM virtual machine, directly bring the specified page, use, the address bar does not change, is the dynamic loading concept
<jsp:forward/>
Will prevent the following run
The equivalent is written in the Doget method:
This.getservletconfig (). Getservletcontext ()----Here is through the current servlet
The servlet configuration properties are obtained and the servlet context is obtained
. Getrequestdispatcher (URL)--Call dispatcher
Pass to the specified URL path The forward method is the true jump
Carrying the current request response object
. Forward (request, response);
Although their memory addresses are different, the properties inside their objects are the same.

Turnover between their two pages has not been through the browser all the time inside the virtual machine
That's why he's faster.

Usage <forward>
<jsp:param Key value pairs >
This is equivalent to adding a key-value pair based on the original request object.
</jsp:forward>--Note no spaces

Usebean:

<jsp:usebean id= "variable" class= "file name" ></jsp:useBean>
Here is the reference to the object that gets the bean, which is the new bean. Counterbean
<jsp:usebean id= "CD" class= "Counterbean" ></jsp:useBean>
Instantiate the Counterbean class file inside the Bean folder and assign the object reference to the variable CD

Page:

The range page is valid for the current page
<jsp:usebean id= "variable" scope= "page" class "File name"/>
<jsp:usebean id= "Conuterbean" scope= "page" Class "Bean. Counterbean "/>

Request:

Scope current Request Object valid
<jsp:usebean id= "variable" scope= "Request" class "File name"/>
<jsp:usebean id= "Counterbean" scope= "request" class= "bean. Counterbean "/>

Application:

Scope of all access, can access
<jsp:usebean id= "Variable" scope= "Application" class "File name"/>
<jsp:usebean id= "Counterbean" scope= "Application" class= "bean. Counterbean "/>

Session:

Range current Browser
<jsp:usebean id= "Variable" scope= "Session" Class "file name"/>
<jsp:usebean id= "Counterbean" scope= "session" class= "Bean. Counterbean "/>

SetProperty:

<jsp:setproperty name= "variable (to get object reference)" Property= "Property (member variable)" value= "value (value to set)"/>
Setting the value
<jsp:setproperty name= "CD" property= "Count" value= "/>"
The count variable in this reference to the CD is assigned a value of 23 setcount () method of the underlying call (written by itself) to let the outside world set the value of the private member property

GetProperty:

Get value
<jsp:getproperty name= "variable (to get object reference)" Property= "Property (member variable)"/>
<jsp:getproperty name= "CD" property= "Count"/>
Gets the value of the count variable inside the CD reference, which is called by the GetCount () method (written by itself), for the outside world to get the value of the private member property

This_getservletconfig_getservletcontext_getrequestdispatcher:

This.getservletconfig (). Getservletcontext ()
. Getrequestdispatcher ("/_12_servlet_jsp/_01_servletusejsp.jsp")
. Forward (request, response);
The current class object (this), called Getservletconfig (), gets the configuration before calling the Getservletcontext () method to get the context, in the call to the Getrequestdispatcher () dispatcher, distributed to the specified file, When the current request and response are passed in
Getservletconfig:

This.getservletconfig ();//Get Configuration Properties

Getservletcontext:

This.getservletconfig (). Getservletcontext ();//Get context

Getrequestdispatcher:

This.getservletconfig (). Getservletcontext ()
. Getrequestdispatcher ("/_12_servlet_jsp/_01_servletusejsp.jsp")
. Forward (request, response);//distributor, distributed to the specified location;

JSP usage and code examples, and parsing the jump process

Related Article

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.