jsp summary
Static Web page
In web design, a Web page in pure HTML (an application under the standard Universal Markup Language) is often referred to as a "static Web page", and a static web page is a standard HTML file with an. htm,. html file name extension. Static Web page is the foundation of the construction of the site, the early sites are generally made of static web pages. Static Web pages are relative to dynamic Web pages, which means that there are no background databases, no programs, and no interactive Web pages.
Dynamic Web pages
The Web page file contains the program code, through the background database and the Web server information interaction, the background database provides real-time data updates and data query services. The dynamic website manifests in the webpage generally is to asp,jsp,php,aspx and so on end.
Difference between the two
Production language of Web page production: Static Web language: Hypertext Markup Language (an application of standard universal Markup Language); Dynamic Web pages Use language: Hypertext Markup Language +asp or hyper-text markup Language +php or Hypertext Markup Language +jsp, etc.
Whether the program is running on the server side is an important sign. On the server side of the program, Web pages, components, belong to dynamic Web pages, they will be different customers, different times, the return of different pages, ASP, PHP, JSP, ASPnet, CGI and so on. Programs, Web pages, plug-ins, and components that run on the client are static Web pages, such as HTML pages, Flash, JavaScript, VBScript, and so on, and they are always the same.
Popular Dynamic Web Technologies
ASP (Active Server Page)
ASP technology is a dynamic Web page technology based on the. NET platform. It has a very powerful background processing power, but there are some security, stability, cross-platform issues. The ASP only supports Windows platforms and is not supported for Linux or UNIX.
PHP (hypertext Preprocessor)
PHP original Personal Home page abbreviation, has been formally renamed as (Foreign name: Php:hypertext Preprocessor, Chinese name: "Hypertext Preprocessor") is a common open source scripting language. Grammar absorbs the C language, Java features, conducive to learning, widely used, mainly for the field of web development. PHP is an HTML-embedded language, which is similar to ASP, and PHP can be cross-platform. But PHP's database interface is not canonical. However, the development of each database interface greatly increases the burden on developers and lacks the support of enterprise-class applications.
Jsp/servlet (Java Server Page)
JSP technology is widely used on Java-based platforms and is a core component of Java Web development. JSP pages consist of HTML code and Java scripts embedded in them. JSPs can be written at once and run everywhere. This JSP is more advantageous than PHP technology, running on different system platforms without any changes to the code.
What is JSP
JSP (Java Server pages) is a Java services page that embeds Java script code in HTML.
JSP page elements:
Static content: HTML static text
Instruction: Start with "<%@" and End With "%>"
Small script: <%java code%>
Expression: <%=java expression%>
Disclaimer: <%! Method%>
Note:<!--client can see the-->,<%--client cannot see--%>
JSP (a) three major directives
①page directive
Define properties for an entire page by setting multiple properties inside
Grammar:
<%@ Page Property 1 = "Property Value" Property 2 = "Property value 1, property value 2" ... Property N= "Property value N"%>
②include directive (static included)
Write some common content into a separate file, and then reference the file by using the include directive
Include (static include): This will compile two pages into a class, the resources can be shared. (Include recompile first)
③TAGLIB directive
Import the JSTL tag library in the JSP page and replace the Java code snippet in the JSP
Grammar:
<% @taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "Alias"%>
JavaBean
Advantages of JavaBean
Resolve code duplication and reduce code redundancy
Clear functional Distinction
Improved Code maintainability
JavaBean is divided into: Data bean and business bean
Data bean: Private property, default empty construct, set, get method is the attribute class in Java
Business Bean: Use a data bean.
JSP (ii) seven big moves
A JSP action is a command that runs during a run, and the common ones are:
Jsp:usebean
Jsp:setproperty
Jsp:getproperty
Jsp:include
Jsp:forward
Jsp:param
Jsp:plugin
①include
Include dynamic inclusion (compiled separately): implemented with the Jsp:include action, it always checks for changes in the included files, is suitable for containing dynamic pages, and can take parameters. Flush property: True to indicate that the page can be refreshed. The default is false, which is not compiled by JSP engine such as Tomcat unless the command is executed when the include command is used.
Syntax: <jsp:include page= "page path" flush= "true"/>
Jsp:include (dynamically included): This compiles into two separate classes, where the resources cannot be shared. (first compile and then include)
Include (static include): This will compile two pages into a class, the resources can be shared. (Include recompile first)
What is the difference between static include and dynamic include?
Same point: The page is introduced to another page.
Different points:
Static include will make the referenced JSP file and the current JSP file into a class file, so the variables in the JSP can be accessed each other, two pages should try to avoid naming conflicts.
Dynamic include generates two completely different class files, so variables in the JSP cannot be accessed from one another.
Static contains a parameter that cannot be passed
Applicable occasions:
Static include does not check for changes to the included files, applies to include static pages, and directly includes the post-processing of the content.
Dynamic include always checks for changes in the included files, is suitable for containing dynamic pages, and can take parameters, compiled before processing.
②usebean
Usebean Action (JSP page uses JavaBean second way), scope defaults to page (valid on this page)
Syntax: <jsp:usebean id= "Object name" class= "package name. Class name" scope= "Scope (page/request/application/session)"/>
The scope property is used to specify the domain scope stored by the JavaBean instance object, which can only be one of the four values of page, request, session, and application, and the default value is page.
③getproperty
The GetProperty action (name is the ID in the Usebean action). Remove the property value from the object:
Syntax: <jsp:getproperty name= "JavaBean object" property= "JavaBean object property name"/>
④setproperty
SetProperty Action (name is the ID in the Usebean action):
To set property values for an object:
Syntax: <jsp:setproperty name= "JavaBean object" property= "JavaBean object property name" value= "value"/>
⑤param
Param action: Pass the parameters, reach the jump page can be request.getparameter ("parameter name") method to remove the parameter value
Syntax: <jsp:include page= "turn to page URL" >
<jsp:param name= "parameter name 1" value= "argument value 1" ></jsp:param>
<jsp:param name= "parameter name 2" value= "argument value 2" ></jsp:param>
</jsp:include>
Or:
<jsp:forward page= "The URL of the turn page" >
<jsp:param name= "parameter name 1" value= "argument value 1" ></jsp:param>
<jsp:param name= "parameter name 2" value= "argument value 2" ></jsp:param>
</jsp:forward>
⑥forward Action: Forwarding page
Syntax: <jsp:forward page= "login.jsp"/> Equivalent to Request.getrequestdispatcher ("Forward page"). Forward (Request,response);
⑦plugin
Plugin action: <JSP:PLUGIN>: Used to specify plug-ins to run on the client, infrequently used
JSP (three) nine large built-in objects
A JSP built-in object is a set of objects created by the Web container
JSP built-in object name is the reserved word of JSP, JSP built-in object is can be used directly on the JSP page object, without using "new", directly use.
A total of 9 such objects are pre-defined in JSP: request, Response, session, application, out, PageContext, config, page, exception.
①request Object (scope: one request)
The Request object is an object of type Javax.servlet.httpServletRequest. This object represents the client's request information and is primarily used to accept data that is delivered to the server over the HTTP protocol. (including header information, System Information, request method, request parameters, etc.). The request object is scoped to the requested one time.
Common methods:
String GetParameter (string name) gets the submission data based on the form component name
String[] Getparametervalues (String name) gets the request data when the form component corresponds to multiple values
void Setcharacterencoding (String charset) specifies the encoding for each request
RequestDispatcher getrequestdispatcher (String path) returns a RequestDispatcher object with the Forward () method used to forward the request
void SetAttribute (String key,object value) sets the key corresponding to the property value
Object getattribute (String key) gets the key corresponding to the property value
②response Object
The response object is an object of type Javax.servlet.http.httpServletResponse. Response represents the response to the client, primarily the object that the JSP container has processed back to the client. The response object also has scope, which is valid only within the JSP page.
Common methods:
void Sendredirect (String location) repositions the request to a different URL, which is page redirection
Forwarding of pages
The role of forwarding: to achieve the sharing of request data during multiple page interactions
Implementation of forwarding: RequestDispatcher object. Forward () method
The difference between redirection and forwarding
Difference One:
URL changes on the browser when redirected
Forwarding is the same as the URL on the browser
Difference Two:
Redirect actually produces two requests
Forward only one request
Redirect: Sends a request--the server runs a response request, returns a new address and response code to the browser, and the browser responds to the response code, determines that the response is redirected, automatically sends a new request to the server, and the request address is the address that was returned before-- The server runs and responds to requests to the browser
Forward: Send a request--server run--to make a request reset, for example via Request.setattribute (Name,value)--based on the forwarded address, get the address of the webpage--respond to the request to the browser
Difference Three:
The URL of the redirect can be any URL
The URL to be forwarded must be the URL of this site
Forward
Forwarding is performed on the server side, passing the commit information across multiple pages via forward ()
The address bar of the client browser does not show the post-turn address
Forwarding can pass parameters
Syntax: Request.getrequestdispatcher ("Forwarding url"). Forward (Request,response);
redirect
Redirection is done on the client side by requesting a new address to implement the page steering
In the Address bar, you can display the post-turn address
Redirection cannot be passed by parameter
Syntax: response.sendredirect ("redirect url");
③session Object (scope: one session)
The Session object is an object of type Javax.servlet.http.HttpSession. The Session object is an object that is automatically created by the server that is related to the user request. The server generates a Session object for each user that holds the user's information and tracks the user's operational status. The session object uses the map class internally to hold the data, so the format of the saved data is "Key/value". The value of the session object can make complex object types, not just string types.
Common methods:
void SetAttribute (String key,object value) saves the object value as a Key/value
Object GetAttribute (String key) Gets the values of the objects by key
void invalidate () Setting session object invalidation
String getId () gets SessionID
void setmaxinactiveinterval (int interval) sets inactivity time for session
int Getmaxinactiveinterval () Gets the active inactivity time (in seconds) of the session
void RemoveAttribute (String key) removes the object corresponding to the specified name (key) from the session
④application Object (scope: one service)
The Application object is an object of type Javax.servlet.ServletContext. The Application object saves information in the server until the server shuts down, otherwise the information saved in the Application object will be valid throughout the application. The Application object has a longer life cycle than the session object, similar to the system's global variables.
Common methods:
void SetAttribute (String key,object value) saves the object value as a Key/value
Object GetAttribute (String key) Gets the values of the objects by key
void RemoveAttribute (String name) deletes the corresponding property based on the property name
String Getrealpath (string path) returns the true path of the relative path
Enumeration Getattributenames () Gets the name of the property
String Getcontextpath () Gets the root directory of the current Web application
String Getinitparameter (string name) gets the initialization parameter value based on the initialization parameter name
⑤out Object
An Out object is an object of type Javax.servlet.jsp.JspWriter. The Out object is used to output information within a Web browser and to manage the output buffers on the application server. When you use an Out object to output data, you can manipulate the data buffers to clear the remaining data in the buffer and make buffer space for the other output. When the data output is complete, the output stream should be closed in time.
⑥pagecontext Object
The PageContext object is an object of type Javax.servlet.jsp.PageContext. The function of the PageContext object is to obtain any range of parameters, through which can get the JSP page out, request, reponse, session, application and other objects. The creation and initialization of PageContext objects is done by the container, and the PageContext object can be used directly in the JSP page.
Common methods:
void SetAttribute (String key,object value) sets the property value
Object getattribute (String key) Gets the property value
void RemoveAttribute (String key) removes the specified property
PageContext itself is also a domain object that can manipulate the data of the other three domain objects (request,response,application)
Ways to manipulate other domain objects:
void SetAttribute (String key,object value,int Scope)
Object getattribute (String key,int Scope)
void RemoveAttribute (String key,int Scope)
Scope:
Pagecontext.page_scope;
Pagecontext.request_scope;
Pagecontext.session_scope;
Pagecontext.application_scope;
⑦config Object
The Config object is an object of type Javax.servlet.ServletConfig. The main purpose of the Config object is to obtain the server configuration information. A config object can be obtained by using the Getservletconfig () method of the Pageconext object. When a servlet initializes, the container passes some information through the Config object to the servlet. Developers can provide initialization parameters for servlet programs and JSP pages in the application environment in the Web. xml file.
⑧page Object (Scope: a page)
The Page object is an object of type Java.lang.Object. The Page object represents the JSP itself and is only legal within the JSP page. The page implied object essentially contains the variables referenced by the current servlet interface, similar to the Thi⑨exception object in Java programming
The exception object is an object of type java.lang.Throwable. The purpose of the exception object is to display exception information that can be used only on pages that contain iserrorpage= "true", which will not compile JSP files in a generic JSP page.
Common methods:
String GetMessage () returns a message describing the exception
String toString () returns a short description message about the exception
void Printstacktrace () shows exceptions and their stack traces
Throwable Fillinstacktrace () overriding the execution stack trajectory of the exception
Set of characters that support Chinese characters in Page Setup
Garbled processing mode one (when data is submitted in get mode)
<%
Read user name and password
String name = Request.getparameter ("name");
Character encoding of request data
name = new String (name.getbytes ("iso-8859-1"), "Utf-8");
%>
Garbled processing Mode II (when submitting data by post)
<%
Set the character encoding of the read request information to UTF-8
Request.setcharacterencoding ("Utf-8");
Read user name and password
String name = Request.getparameter ("name");
String pwd = request.getparameter ("pwd");
%>
Garbled processing method Three (when data is submitted by get)
<!--set the character set in the Tomcat directory structure \conf\server.xml--
<connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000"redirectport= "8443" uriencoding= "UTF-8"/>
Introduction to Cookies
A cookie is a series of text messages that a Web server holds on the client
The role of Cookies
Tracking for a specific object
Statistics page views
Simplified Login
Security Performance: Easy Information disclosure
Using cookies in your JSP
Create a Cookie Object
Cookie Newcookie = new Cookie (String key,object value);
Write Cookie
Response.addcookie (Newcookie);
Read cookies
cookie[] cookies = request.getcookies ();
Common methods:
void setmaxage (int expiry) sets the lifetime of the cookie, in seconds
void SetValue (String value) when a cookie is created, assigns a value to the cookie
String GetName () Gets the name of the cookie
String GetValue () Gets the value of the cookie
String Getmaxage () Gets the effective time of the cookie, in seconds
The difference and connection between session and Cookie
Cookies are the user's data written in the user's local browser, other sites can also scan the use of your cookie, easy to disclose the privacy of their website users, and the General browser to a single site site has a limit on the number and size of cookies.
Session is to write the user's data in the user's exclusive session, stored on the server, usually only the session ID stored in the cookie. However, the cost of storing data on the server is high.
The session is created by the server and the developer can get the session through the GetSession method of the Request object on the server
In general, important information, such as login information, is stored in the session and other information is stored in a cookie.
The implementation principle of Session
The server will create a session object for each user accessing the server, and store the ID of the session object on the local cookie, so long as the user accesses the server again, with the session ID, the server will match the user's session on the server. Restore the user's last browsing status or provide other humanized services based on the data in the session.
The comparison between the cookie and the session
Session
Save user information on server side
Object type is saved in session
Destroys the data it stores with the end of the session
Save Important information
Cookies
Saving user information on the client
The cookie holds a string type
Cookies can be stored for a long time on the client
Save non-critical user information
---------------------------------------------------------------JSP End----------------------------------------------------- ------
Javaweb--jsp Summary