Various built-in objects in JSP

Source: Internet
Author: User

Various built-in objects in JSP

 

The basic unit of web interaction is HTTP requests. Each user's process from entering the website to leaving the website is called an http session. When a server is running, multiple users access it, that is, multiple HTTP sessions. The scope is explained as follows:

1. application: the time from server start to stop

2. Session: the time from the beginning to the end of HTTP sending

3. Request: the time from the beginning to the end of the HTTP request

4. Page: The time from opening to closing the current page

I. Application Scope

(1) Get a copy of the application object

1. directly use application in JSP to represent the Application Object

2. Use the servletconfig Interface

The servletconfig interface can be used to indirectly obtain the application object, but the name of this application object cannot be the same as that of application. Because application is a copy of the default application object created by the JSP engine, it cannot be overwritten. You can call the getservletcontext () method to return an object of the servletcontext interface, that is, a copy of the application object.

3. Use the pagecontext object to obtain the application object

Getservletcontext () of the pagecontext object also returns a copy of the application object.

(2)

1. Object getattribute (string name): get information from application.

2. Void setattribute (string name, object Value): enter information to the application.

3. Enumeration getattributenames (): returns the names of all parameters or Java objects bound to the current application object.

4. Void removeattribute (string name): Through this method, we can unbind a parameter or Java object from the application object.

5. getserverinfo (): This method can obtain the details of the current server. The formats of the information returned by different servers are different.

6. Log (string MSG): This method is often used to write information to the server's log file, so which log file is determined by the server. Using this method, we can record visitor information in the log file, which is very useful for collecting access traffic sources, and also helps the administrator monitor the website operation. In fact, there is also a log () method with the same name but different parameters. With the latter, we can include all the error events during JSP program execution into the log file.

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "ISO-8859-1" %> <br/> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <body> <br/> <% <br/> string username1 = "George "; <br/> string username2 = "Robin"; <br/> string password1 = "George"; <br/> string password2 = "Robin "; </P> <p> out. println ("Set Application attribute <br>"); <br/> application. setattribute ("username1", username1); <br/> application. setattribute ("username2", username2); <br/> application. setattribute ("password1", password1); <br/> application. setattribute ("password2", password2); </P> <p> out. println ("list application attribute: <br>"); <br/> enumeration enum1 = application. getattributenames (); <br/> while (enum1.hasmoreelements () <br/>{< br/> string attrname = (string) enum1.nextelement (); <br/> If (attrname. equals ("username1") | attrname. equals ("username2") | attrname. equals ("password1") | attrname. equals ("password2") {<br/> out. println (attrname + "----->"); <br/> string attrvalue = application. getattribute (attrname ). tostring (); <br/> out. println (attrvalue + "<br>"); <br/>}</P> <p> username1 = "Robin "; <br/> password1 = "Robin"; </P> <p> out. println ("Change Application attribute: <br>"); <br/> application. setattribute ("username1", username1); <br/> application. setattribute ("password1", password1); </P> <p> out. println ("username1 ----->" + application. getattribute ("username1") + "<br>"); <br/> out. println ("password1 ----->" + application. getattribute ("password1") + "<br>"); </P> <p> out. println ("Remove application attribute: <br>"); <br/> application. removeattribute ("username1"); <br/> application. removeattribute ("password1"); </P> <p> out. println ("relist application attribute: <br>"); <br/> enumeration enum2 = application. getattributenames (); <br/> while (enum2.hasmoreelements () <br/>{< br/> string attrname = (string) enum2.nextelement (); <br/> If (attrname. equals ("username1") | attrname. equals ("username2") | attrname. equals ("password1") | attrname. equals ("password2") {<br/> out. println (attrname + "----->"); <br/> string attrvalue = application. getattribute (attrname ). tostring (); <br/> out. println (attrvalue + "<br> "); <br/>}< br/>%> <br/> </body> <br/> </ptml> 
 

Effect:

Ii. Session Scope

1. Object httpsession. getattribute (string name): obtains information from the session.

2. Void httpsession. setattribute (string name, object Value): Enter the information in the session.

3. httpsession httpservletrequest. getsession (): gets the object of the session in which the current request is located.

Note:

It is better to judge the beginning of the session. The first HTTP request sent by the browser indicates that the session starts. However, it is difficult to judge the end time because the server will not be notified when the browser is closed. Generally, if the client does not respond within a period of time, the session ends. The default value of Tomcat is 120 minutes, but this value can also be set through the setmaxinactiveinterval () method of httpsession:

1. Void setmaxinactiveinterval (INT interval)

If you want to end the session, you can use the invalidate () method of httpsession to force the session to end:

2. Void invalidate ():

Iii. Request Scope

The processing of an HTTP request may require the cooperation of multiple Servlets, which can transmit information in some way, but the message is invalid after the request ends.

Servlet Information sharing is implemented through the httpservletrequest interface.

1. Void setattribute (string name, object Value): saves the object value with name in the request scope.

2. Object getattribute (string NAMR): retrieves the information of the specified name from the request scope.

After setting, you must use the forward () method of the requestdispatcher interface to forward requests to other servlets.

1. requestdispatcher servletcontext. getrequestdispatcher (string path): gets the dispatcher for forwarding. path is the target servlet for forwarding.

2. Void requestdispatcher. Forward (servletrequest request, servletresponse response): forward requests and response.

Iv. Page Scope

The scope of the page object is limited to the current page requested by the user. Reference to the page object will be released after the response is returned to the client, or released after being forwarded to other places. References to page objects are usually stored in pagecontext objects.

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.