http://blog.csdn.net/xiazdong/article/details/6848382
First, the built-in object describes the built-in objects cannot be <%! Use in%>!
Built-in objects |
Owning class |
PageContext |
Javax.servlet.jsp.PageContext |
Request |
Javax.servlet.http.HttpServletRequest |
Response |
Javax.servlet.http.HttpServletResponse |
Session |
Javax.servlet.http.HttpSession |
Config |
Javax.servlet.ServletConfig |
Application |
Javax.servlet.ServletContext |
Out |
Javax.servlet.jsp.JspWriter |
Page |
Java.lang.Object |
exception |
Java.lang.Throwable |
In order to facilitate document checking, it is important to remember the classes that belong to them; (there are no such classes in the Java SE documentation, you need to download the Java EE documentation);
The built-in objects of JSPs are very important in JSPs, which are created by the Web container, so the user does not have to create them themselves.
The creation of these built-in objects can be clearly seen in JSP-generated servlet classes, but it is important to note that exception is generally not created, only if the JSP page is an error page, that is, only the JSP contains iserrorpage= "true" subset Create exception object;
Several of the main built-in objects are:
(1) Request:javax.servlet.http.HttpServletRequest, indicates customer request.
Specific usage: request.getparameter ("name") and so on.
(2) Response:javax.servlet.http.HttpServletResponse, which indicates the server response.
(3) PageContext:javax.servlet.jsp.pageContext, which represents the JSP page.
(4) Session:javax.servlet.http.HttpSession, which represents a session.
(5) Application:javax.servlet.servletContext, which means all users share information.
(6) Out:javax.servlet.jsp.jspWriter, write the page content.
(7) Page: Represents an instance of a page.
(8) Config:javax.servlet.servletConfig, which represents the profile information.
Two, 4 kinds of attribute range
(1) page range (PageContext): A page is valid, the jump is not valid.
(2) Request scope: The server jump is valid, the client jump is invalid.
(3) Session scope: Jump valid, new open browser invalid.
(4) Application range: All users are valid, restarting the server is not valid.
There are 3 methods for these four objects:
(1) void SetAttribute (String key,object o); Setting properties
(2) Object getattribute (String key); Make a downward transition when you get it.
(3) void RemoveAttribute (String key); Delete Property
Note: Be sure to transform after getattribute!
Example:
Pagecontext.setattribute ("name", "Xiazdong"); Can only be saved in a single page
String name = (string)Pagecontext.getattribute ("name"); You can't get a page after you change it.
Note: If possible, save the property settings as small as possible, as long as the functionality is satisfied. This can improve performance;
Iii. Request Object
Note: request.getsession () can get the session object;
The request object is the message received from the client, and the client sends header information, content, delivery method, and cookie.
Common methods:
(1) request.getparameter (String name): Gets the value of the name element. Source: (1) address rewriting (2) Form submission
(2) Enumeration ENU = Request.getparameternames (): Gets the name of all parameters, traversing through the iterator.
Add: Enumeration Usage:
Enu.hasmoreelements ();
Enu.nextelement ();
(3) string[] STRs = request.getparametervalues (String name): Used to obtain multiple selections such as a checkbox.
(4) Enumeration ENU = Request.getheadernames (): Gets the name of all header information.
(5) String str = Request.getheader (string name): Gets the contents of the header information named name.
(6) String str = Request.getmethod (): Get is post or get.
(7) request.setcharacterencoding (String): The encoding of the unified request. For example, the server JSP page is set to GBK, and the browser is UTF-8, there will be a coding mismatch problem.
(8) Request.isuserinrole (String name); For user authentication roles
(9) Cookie[] C = request.getcookies (): Get all the cookies.
Example 1: Displaying All header information
Enumeration ENU = Request.getheadernames ();
while (Enu.hasmoreelements ()) {
String name = (string) enu.nextelement ();
String value = Request.getheader (name);
Display name and value
}
Example 2: Obtaining a client IP address
String IP = request.getremoteaddr ();
There are some less common methods of request:
(1) request.getremoteaddr (); Returns the IP address of the client;
(2) Request.getservletpath (); Returns the directory of the current file in the server;
(3) Request.getcontextpath (); Returns the home directory information;
Tomcat how to add new users and new permissions:
(1) In Tomcat/conf/tomcat-users.xml can be set, refer to the admin configuration, and then restart the Tomcat server, complete the new user and permissions to load.
(2) Set in Web. XML to enter the user name and password before becoming a login page. For details, see: http://blog.csdn.net/xiazdong/article/details/6894889
Iv. the difference between post and get
The most obvious difference is the Address bar difference, post delivery, the address bar does not change, and get pass, the address bar will display the information passed, so get pass the information is limited.
V. Response objects
Represents an object that responds to a client.
1. Common methods:
(1) Addcookie (cookie): Add a cookie
(2) SetHeader (String name,string value): Sets the header information.
(3) Sendredirect (): Redirect, similar to a client jump.
(4) Getoutputstream (); Output stream
Common usage:
(1) Timed refresh: Response.setheader ("Refresh", "2"): 2 seconds to refresh.
(2) Timed Jump: Response.setheader ("Refresh", "2; Url=hello.jsp "), jump to hello.jsp after 2 seconds. Note: This is customer-side jump;
(3) Client Jump: Response.sendredirect ("hello.jsp");
Note: The above (2) usage may fail because the user refreshed the page within 2 seconds, causing refresh to fail;
Application:
Implementation of the user login after the "3 seconds after the jump, if there is no jump click here ";
The difference between a server jump and a client jump:
Server-side jump is immediately jump, that is, the page executes to the jump statement will not execute the following statement;
Client jumps are performed after the entire page has been completed before jumping.
2. Manipulating Cookies:
Javax.servlet.http.Cookie;
Saves the user's information as a cookie, stored on the client, and sends the cookie to the server each time the request is made.
According to our Gongkong teacher said: A person has a piece of paper, this piece of paper is his cookie, when the previous login page A, then on the paper will be recorded, if you enter page a again, others will know that you have entered the page A and know your identity.
Response.addcookie (cookie e); Add a cookie.
The cookie is located in Javax.servlet.http.cookie.
Common methods:
(1) Cookie C = new Cookie ("name", "value"): Creates a new cookie.
(2) c.setmaxage (int seconds): sets the maximum lifetime of a cookie because the original cookie is saved in the browser and if the browser is closed, the cookie is lost.
(3) C.getname (): Get the name.
(4) C.getvalue (): Gets the value.
Cookie[]cs = request. getcookies (); Get cookies
Vi. Session Object
Session Common methods:
(1) String getId (): Gets the session ID. The session ID is similar to the cookie Jsessionid, which is automatically assigned by the server;
(2) Session.setattribute ("", ""): Sets the property.
(3) Session.getcreationtime (): Gets the creation time.
(4) Session.getlastaccessedtime (): Get last access time.
(5) Session.isnew (): Whether this session is new, that is, to determine the session ID.
(6) Session.invalidate (): Clears the session's property settings.
Login-Logout page:
(1) Register with Session.setattribute ().
(2) Use Session.getattribute () to determine if it has been registered.
(3) using session.invalidate (); Log off
If you want to save the session ID, you need to persist it, see: http://blog.csdn.net/xiazdong/article/details/6894945
Vii. Objects of Application
Javax.servlet.ServletContext;
Common methods:
(1) Getrealpath (path): Get the real path.
(2) Getcontextpath (): Gets the virtual path.
(3) Getinitparameter (String);
We can add initialization parameters in Web. xml:
[HTML]View PlainCopy
- <context-param>
- <param-name></param-name>
- <param-value></param-value>
- </context-param>
Application==this.getservletcontext ()
Viii.. config Object
Javax.servlet.ServletConfig;
First from the configuration file, in each Web application, there is a Web-inf folder, this folder is very secure, not to be seen by others, because it can not be accessed directly, unless accessed through a mapping relationship;
If you put a hello.jsp file in Web-inf, you can make it accessible.
The answer is to set the Web-inf/web.xml in the following:
<servlet>
<servlet-name>he</servlet-name>
<jsp-file>/WEB-INF/hello.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>he</servlet-name>
<url-pattern>hello</url-pattern>
</sevlet-mapping>
After restarting the server, enter Http://localhost:8080/test/hello in the Address bar.
Config Common methods:
(1) getinitparameternames ();
(2) Getinitparameter (String name);
What are initialization parameters?
The initialization parameters are configured in Web. XML in the following form:
<servlet>
<init-param>
<param-name>name</param-name>
<param-value>value</param-value>
</init-param>
</servlet>
Ix. PageContext Objects
Represents the JSP context, which can be obtained through this instance, request\response\session\<jsp:forward> and so on.
(1) Pagecontext.forward (String);
(2) Pagecontext.include (String);
(3) Pagecontext.getservletconfig ();
(4) Pagecontext.getservletcontext ();
(5) Pagecontext.getrequest ();
(6) Pagecontext.geresponse ();
(7) pagecontext.getsession ();
In other words, as long as the PageContext object, you can complete all the functions of built-in objects;
Add: Pageconext set any range of attributes (rarely used)
Use function: Pageconext.setattribute ("name", "value", int SCOPE);
1. Set Page range
Pageconext.setattribute ("name", "Xiazdong", Pageconext.page_scope);
2. Set the request range
Pageconext.setattribute ("name", "Xiazdong", Pageconext.request_scope);
3. Setting the session Range
Pageconext.setattribute ("name", "Xiazdong", Pageconext.session_scope);
4. Setting the Application Range
Pageconext.setattribute ("name", "Xiazdong", Pageconext.application_scope);
Therefore, only PageContext can complete the operation of all built-in objects;
Exception objects can only be used when iserrorpage= "true". Common methods: (1) getMessage (); Xi. out Object Javax.servlet.jsp.JspWriter is generally used for outputting character streams, that is, text flow, but if you need to pass information such as pictures, you need to pass through response;
[Java]View PlainCopy
- Public void _jspservice (final httpservletrequest request, final httpservletresponse response)// Create a request, Response
- throws Java.io.IOException, servletexception {
- final PageContext PageContext;
- HttpSession session = null;
- final ServletContext Application;
- Final ServletConfig config;
- JspWriter out = null;
- Final Object page = this ;
- JspWriter _jspx_out = null;
- PageContext _jspx_page_context = null;
- try {
- Response.setcontenttype ("Text/html;charset=utf-8");
- PageContext = _jspxfactory.getpagecontext (this, request, Response, //create PageContext
- null, True, 8192, true);
- _jspx_page_context = PageContext;
- application = Pagecontext.getservletcontext (); //Create application or ServletContext
- Config = Pagecontext.getservletconfig (); //Create config
- Session = Pagecontext.getsession (); //Create session
- out = Pagecontext.getout (); //Create out
- _jspx_out = out;
Summary: In the JSP page if you want to output, then use OUT.PRINTLN ();
JSP built-in objects