(1) Instantiate an object directly in the Web contain.
Built-in objects |
Type |
Scope |
PageContext |
Javax.servlet.jsp.pageContext |
Page |
Request |
Javax.servlet.http.HttpServletRequest |
Request |
Response |
Javax.servlet.http.HttpServletResponse |
Page |
Session |
Javax.servlet.http.HttpSession |
Session |
Application |
Javax.servlet.ServletContext |
Application |
Config |
Javax.servlet.ServletConfig |
Page |
Out |
Java.servlet.jsp.JspWriter |
Page |
Page |
Java.lang.Object |
Page |
exception |
Java.lang.Throwable |
Page |
(2) JSP built-in object classification
Servlet-related hidden objects: page,config
Hidden objects related to Input/output: Out,request,response
Implied object of the context associated with JSP execution: Session,application,pagecontext
Implied object related to error: exception
(3) Setting and acquisition of attributes
Setting properties: public void SetAttribute (String name,object.value)
Get attribute: public void getattribute (String name)
(4) Four kinds of properties to save the range
Within a page range: page
In the context of a single request to the server: request
In a single session range: Session
Within an application Server range: Application
(5) Page range
The page range is set by the PageContext object, and the attribute is saved within a single request, and must be used server-side jump <jsp:forward/>
(6) Session range
Keep only one user's information
(7) Application range
This information is available to all users and is retained on the entire server. Application the property range value, as long as it is set once, all the page windows can get the data.
Application,session,request need to span multiple pages, and the memory overhead is large when attributes are saved. If you can use the request does not apply to the session, you can use the session do not use application.
(8) PageContext by default represents a page's save scope
public void SetAttribute (String name,object value,int scope)
public static final int application_scope:application range
public static final int session_scope:session range
public static final int Request_scope:request range
public static final int Page_scope:page range
The four attribute ranges are based on PageContext, but in development, the Session,request range is often used
Session Object
(1) Session function
The session object is mainly used to save the user's various information, until its life cycle exceeds or is artificially released, you can use the session object to determine whether this user is a legitimate user.
The session object is an instantiated object of the Javax.Servlet.http.HttpSession interface, and the session belongs to an object within the scope of the HTTP protocol, so only the unique HTTP packet has this interface, there is no inheritance relationship, the user is assigned a sess when connected to the server. Ion to the user.
(2) Main methods
Session.getid (): On the server through the session to separate users, SessionID: Any link to the user on the server, the server will be assigned to it a unique not duplicate SessionID. Unified management by the server, human cannot control.
Session.getid (). Length (): ID is 32 bits long
Session.isnew (): Determine if the user is new
Session.invalidate (): Invalidates the session
Session.getcreationtime (): Gets the session creation time, returns a long type, gets the time by date
Session.getlastaccessedtime (): Gets the last operation time, returns a long type
Instance:
<% @page contenttype= "text/html;charset=gb2312"%>
<% @page import= "java.util.*"%>
<%
Long L = session.getcreationtime ();
Long L2 = Session.getlastaccessedtime ();
%>
If you need to ask for a user's online time, use the following formula: Last operation time-creation time
(3) Setting of Session properties
Session.setattribute (String name,object value)
Session.getattribute (String name)
Session.removeattribute (String name)
(4) The difference between two ways of writing
if (Name.equals ("Mldn") &&pass.equals ("password")): If the name and pass are not initialized, then the nullpointexception will appear when the sentence is executed.
if ("Mldn". Equals (name) && "password". Equals (pass)): No error occurs if the write rule is used.
(5) Session usage: Verify that the user is logged in.
(6) Session and Cookie
Session and cookies are used to share data across Web pages. Session record unique personal information, in different pages, the use of Setattribute,getattribute method; Cookies are stored in the client, for the browser and the Web server to Exchange data in plain text files, When IE executes, it generates a cookie on the computer. Session is more secure than cookie, session is more resource-intensive than cookie, session uses cookie mechanism, and if cookie is disabled, session cannot be used.
Application and Config objects
(1) Application Introduction
The main function of application object is to save public information, belonging to javax. Servlet. ServletContext, the path to this working directory is not fixed, it is possible that the working directory is on the C drive or the real path of the working directory obtained in the D drive. Application is an object that maintains only one instance of the entire server.
(2) attribute operation: SetAttribute (), getattribute (), RemoveAttribute ()
(3) True path: Application.getrealpath (string path), where string path is "/". In practical use, the Getservletcontext () method is used instead of application. The format is as follows:
Getservletcontext (). Getrealpath ("/")
This.getServletContext.getRealPath ("/")
(4) JSP Security Config Object
(a) In the Java EE security embodied in the working directory of the Web-inf folder, the page is saved in this folder, outside the folder can not be found in the file, if you want to make the file in this folder accessible to users, Modify the Web. xml file that exists under the Web-inf folder, which belongs to the configuration file for the entire website.
<......mapping start>
<servlet>
<servlet-name></servlet-name>
<jsp-file></jsp-file>
<init-param>
<param-name></param-name>
<param-value></param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name></servlet-name>
<url-pattern>/the address entered in the browser </url-pattern>
</servlet-mapping>
<......mapping end>
Restart the server after modifying Web. Xml.
(b) Config object
The Config object is generally used to get the initialization configuration parameters of the server, and to use this object should be configured in Web-inf/web.xml. The method by which the Config object obtains initialization parameters in Javax.Servlet.servletConfig:
public string Getinitparameter (string name)
With this function, you can write some information about the database link in the configuration file, similar to the following two methods, is to obtain the full configuration parameters:
Public enumeration Getinitparameternames ()
Request.getparameternames ()
"Turn" compares the use range of page, request, session, application