Document directory
- 1. Common Methods:
- 2. Cookie control:
1. built-in object introduction built-in objects cannot be found in <%! %> Used in!
Built-in object |
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 |
To facilitate document query, you must remember the class to which the file belongs. (These classes are not found in the Java SE document and you need to download the Java EE document );
JSP built-in objects are very important in JSP. These built-in objects are created by web containers, so you do not need to create them yourself.
You can clearly see the creation of these built-in objects in the servlet class generated by JSP, but note that exceptions are generally not created, and they are created only when the JSP page is an error page, that is, the exception object is created only when JSP contains iserrorpage = "true;
The main built-in objects include:
(1) Request: javax. servlet. http. httpservletrequest, indicating the customer's request.
Specific usage: request. getparameter ("name.
(2) response: javax. servlet. http. httpservletresponse, indicating the server response.
(3) pagecontext: javax. servlet. jsp. pagecontext, indicating the JSP page.
(4) Session: javax. servlet. http. httpsession, indicating a session.
(5) Application: javax. servlet. servletcontext, indicating that all users share information.
(6) Out: javax. servlet. jsp. jspwriter writes the page content.
(7) page: indicates an instance of a page.
(8) config: javax. servlet. servletconfig, which indicates the configuration file information.
2. Four attribute ranges
(1) page range (pagecontext): valid for one page. redirection is invalid.
(2) request range: valid for server redirect and invalid for client redirect.
(3) Session range: The jump is valid and the new browser is invalid.
(4) Application Scope: valid for all users and invalid for restarting the server.
The four objects have three methods:
(1) void setattribute (string key, object O); // set attributes
(2) object getattribute (string key); // perform a downward transformation after obtaining the object.
(3) void removeattribute (string key); // delete an attribute
Note: transformation is required after getattribute!
Example:
Pagecontext. setattribute ("name", "xiazdong"); // save only on a single page
String name =(String)Pagecontext. getattribute ("name"); // after a page is changed, it cannot be obtained.
NOTE: If possible, the storage range of attribute settings should be as small as possible. This improves the performance;
Iii. Request object
Note: request. getsession () can obtain the session object;
The request object is the information sent from the receiving client, and the client sends the header information, content, transmission method, and cookie.
Common Methods:
(1) Request. getparameter (string name): obtains the value of the name element. Obtain Source: (1) Address rewriting (2) Form submission
(2) Enumeration enu = request. getparameternames (): Get the names of all parameters and traverse them through the iterator.
Supplement: usage of enumeration:
Enu. hasmoreelements ();
Enu. nextelement ();
(3) string [] STRs = request. getparametervalues (string name): used to obtain multiple choices such as checkbox.
(4) Enumeration enu = request. getheadernames (): Get the name of all header information.
(5) string STR = request. getheader (string name): obtains the header information with the name.
(6) string STR = request. getmethod (): Get is post or get.
(7) request. setcharacterencoding (string): Uniform Request Encoding. For example, the server's JSP page is set to GBK, And the browser is the UTF-8, there will be encoding mismatch problem.
(8) request. isuserinrole (string name); used for user authentication role
(9) Cookie [] C = request. getcookies (): obtain all cookies.
Instance 1: Show All header information
Enumeration enu = request. getheadernames ();
While (enu. hasmoreelements ()){
String name = (string) enu. nextelement ();
String value = request. getheader (name );
// Display the name and value.
}
Instance 2: Obtain the Client IP Address
String IP = request. getremoteaddr ();
There are also some less commonly used methods for request:
(1) Request. getremoteaddr (); return the IP address of the client;
(2) request. getservletpath (); returns the directory of the current file on the server;
(3) request. getcontextpath (); returns the home directory information;
Add new users and new permissions to Tomcat:
(1) In tomcat/CONF/tomcat-users.xml can be set, refer to the admin configuration, and then restart the Tomcat server to complete the loading of new users and permissions.
(2) set in Web. xml so that the user name and password will pop out of the window before you log on to the webpage. See http://blog.csdn.net/xiazdong/article/details/6894889 for details
Iv. Differences between post and get
The most obvious difference is the difference in the address bar. When a post is passed, the address bar does not change, while get is passed, and the address bar displays the transmitted information. Therefore, the information transmitted by get is limited.
5. Response object
The object that responds to the client.
1. Common Methods:
(1) addcookie: Add cookie
(2) setheader (string name, string value): sets the header information.
(3) sendredirect (): Redirection, similar to client jump.
(4) getoutputstream (); output stream
Common usage:
(1) timed Refresh: Response. setheader ("refresh", "2"): refresh once every 2 seconds.
(2) scheduled jump: Response. setheader ("refresh", "2; url = Hello. jsp"); jump to hello. jsp in 2 seconds.Note: This is a client jump;
(3) Client jump: Response. sendredirect ("Hello. jsp ");
Note: The above (2) usage may fail because the user refresh the page within 2 seconds, resulting in refresh failure;
Application:
After logon, you can jump to the "3 seconds later" page. If no jump is made, clickHere";
What is the difference between server jump and client jump:
The server jumps immediately, that is, the subsequent statements are not executed after the page is executed to the jump statement;
The client jumps only after the complete page is executed.
2. Cookie control:
Javax. servlet. http. Cookie;
Saves user information as a cookie stored on the client. Each request sends the cookie to the server.
According to our instructor Gong Xueqing, a person has a piece of paper, which is his cookie. When he has logged on to page a before, it will be recorded on the paper. If he enters page a again, others will know that you have already entered page A and know your identity.
Response. addcookie (cookie E); add cookie.
The cookie is located in javax. servlet. http. Cookie.
Common Methods:
(1) Cookie c = new cookie ("name", "value"): Create a New cookie.
(2)C. setmaxage (INT seconds ):Set the maximum lifetime of a cookie because the original cookie is saved in the browser. If the browser is closed, the cookie is lost.
(3) c. getname (): Get the name.
(4) C. getvalue (): obtain the value.
Cookie [] cs =Request. Getcookies (); get cookies
Vi. Session Object
Common session methods:
(1) string GETID (): obtains the session ID. Session ID is similar to the JSESSIONID of the cookie, which is automatically allocated by the server;
(2) Session. setattribute ("", ""): sets attributes.
(3) Session. getcreationtime (): Obtain the creation time.
(4) Session. getlastaccessedtime (): Get the last access time.
(5) Session. isnew (): determines whether the session ID is new.
(6) Session. invalidate (): clears all session attribute settings.
Login-logout page:
(1) Use session. setattribute () for registration.
(2) Use session. getattribute () to determine whether the application has been registered.
(3) UseSession. invalidate ();Logout;
If you want to save session ID, you need to perform the persistence operation, see: http://blog.csdn.net/xiazdong/article/details/6894945
VII. Application Object
Javax. servlet. servletcontext;
Common Methods:
(1) getrealpath (PATH): Get the real path.
(2) getcontextpath (): Obtain the virtual path.
(3) getinitparameter (string );
We can add initialization parameters in Web. xml:
<context-param><param-name></param-name><param-value></param-value></context-param>
Application = This. getservletcontext ()
8. config object
Javax. servlet. servletconfig;
First from the configuration file, in each web application, there is a WEB-INF folder, this folder is safe, will not be seen by others, because can not directly access, unless accessed through ing;
If you put a hello. jsp file in the WEB-INF, if you can make it accessible.
The answer is to set it in WEB-INF/Web. XML, the content is as follows:
<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 the server is restarted, enter http: // localhost: 8080/test/hello in the address bar.
Common Methods of config:
(1) getinitparameternames ();
(2) getinitparameter (string name );
What are initialization parameters?
The initialization parameters are configured in Web. XML in the following format:
<Servlet>
<Init-param>
<Param-Name> name </param-Name>
<Param-value> value </param-value>
</Init-param>
</Servlet>
9. pagecontext object
JSP context. You can use this instance to obtain 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 ();
That is to say, as long as a pagecontext object is available, all built-in object functions can be completed;
Supplement: pageconext sets attributes of any range (rarely used)
Function used: pageconext. setattribute ("name", "value", intScope);
1. Set the page range
Pageconext. setattribute ("name", "xiazdong", pageconext. page_scope );
2. Set the request range
Pageconext. setattribute ("name", "xiazdong", pageconext. request_scope );
3. Set the session range
Pageconext. setattribute ("name", "xiazdong", pageconext. session_scope );
4. Set the application range
Pageconext. setattribute ("name", "xiazdong", pageconext. application_scope );
Therefore, you only need pagecontext to complete operations on all built-in objects;
10. The exception object can only be used when iserrorpage = "true. Common methods: (1) getmessage (); 11. javax. servlet. JSP. jspwriter is generally used to output a response stream, that is, a text stream. However, if you want to transmit images and other information, you must pass the response stream;
Public void _ jspservice (final httpservletrequest request, final httpservletresponse response) // CREATE request, response throws Java. io. ioexception, servletexception {final 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 an application that is, servletcontext Config = pagecontext. getservletconfig (); // create config session = pagecontext. getsession (); // create session out = pagecontext. getout (); // create out _ jspx_out = out;
Summary: If you want to output data on the JSP page, use out. println (); for output!
Supplement: Disable scripts and El
1. Disable scripts
Configure the following template in Web. xml:
<jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <scripting-invalid>true</scripting-invalid> </jsp-property-group> </jsp-config>
All JSP scripts can be disabled.
Effect: If scriptlet is disabled, "org. Apache. Jasper. jasperexception" is displayed ";
2. Disable el
Configure the following template in Web. xml:
<jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <el-ignored>true</el-ignored> </jsp-property-group> </jsp-config>
Or iselignored = "true" in the JSP page command ";
If the two configurations conflict with each other, the JSP page command takes precedence;
Effect: If El is disabled and El is used, it is treated as plain text. If $ {requestscope. AAA} is used, "$ {requestscope. AAA}" is output }"
Disable El summary table
<El-ignored> |
Iselignored |
Whether to ignore El expressions |
Not Specified |
Not Specified |
Ignore |
False |
Not Specified |
Ignore |
True |
Not Specified |
Ignore |
False |
False |
Ignore |
False |
True |
Ignore |
True |
False |
Ignore |