Java Server Pages built-in objects

Source: Internet
Author: User
Tags throwable unique id server memory

JSP nine large built-in objects:

A, a JSP built-in object is a set of objects created by the Web container, and a built-in object that "does not use the New keyword" can be used. Example: Out

B, JSP nine large built-in objects:

Five most commonly used objects: Out,request,response,session,application

The other four objects: Page,pagecontext,exception,config

1.out built-in objects:

2.request built-in objects, common methods are as follows:

The Request object is an object of type Javax.servlet.httpServletRequest. This object represents the client's request information.

Primarily used to accept data transmitted to the server via the HTTP protocol. (including header information, System Information, request method, request parameters, etc.).

The request object is scoped to the requested one time.

A. <%request.setcharacterencoding ("Utf-8"); %>//solve the Chinese garbled, but can not solve the URL to pass the Chinese garbled

"<a href=" request.jsp?username= John Doe "> Test URL Pass Parameters </a>"

The solution is: Modify the connector in Server.xml, add:uriencoding= "Utf-8"

B. String getparameter (String xxx) returns the parameter value of xxx specified parameter

C. string[] getparametervalues (String xxx) returns an array of all values that contain the parameter xxx

D. void SetAttribute (String,object); Stores the properties in this request

E. int Getserverport ();//Returns the port number used by the server to accept this request

F. String getcharacterencoding ();//return character encoding

G. Void Setcharacterencodinng ();//Set the character encoding of the request

H. int getcontentlength ();//Long Distance (in bytes) returning the request body

I. String getremoteaddr ();//returns the IP address of the client that sent this request

J. String Getrealpath (string path);//Returns the true path of the virtual path

K. String Request.getcontextpath ();//Return context Path

3.response Built-in objects

Response represents the response to the client, which is an instance of the HttpServletResponse class.

The main object is to transfer the objects processed by the JSP container back to the client. The response object also has scope, which is valid only within the JSP page.

The response object has a page scope, that is, when a page is accessed, the response object within that page is valid only for this access, and the response object on the other page is not valid for the current page.

Common methods are as follows:

A. String getcharacterencoding (); What character encoding is returned for the response

b. void setContentType (String type); Sets the MIME type of the response

C. PrintWriter getwriter (); Returns an object that can output characters to the client

D. Response.sendredirect ("xxx.jsp"); Requesting client redirection: such as directed to the Xxx.jsp page

Note: The PrintWriter object obtained by Response.getwriter () is "ahead" of the built-in out object when printed out.

However, after writing the OUT.PRINTLN (), you can force the buffer contents to be written out by the flush () method, which will display the text in front of you.

4.session built-in objects (present in server memory):

What is an instance of the Session:httpsession class, opens the browser from a client and connects to the server, starting with the client closing the browser and leaving the server end, known as a session.

When a client accesses a server, it may be repeatedly connected between several pages of the server, repeatedly refreshing a page,

The server should know by some means that this is the same customer, which requires a session object.

ID of Session object: When a client accesses a JSP page on the server for the first time, the JSP engine produces a session object and assigns a string ID number.

The JSP engine sends this ID number to the client at the same time, storing it in a cookie so that a one by one corresponding relationship is established between the session object and the client.

When customers re-access other pages that connect to the server, they are no longer assigned to the customer's new session object.

Until the client closes the browser, the server side of the client's session object is canceled, and the conversation correspondence with the customer disappears.

When the customer re-opens the browser and then connects to the server, the server creates a new session object for the customer.

The session object has a certain time expiration problem, so the existence of the name value pairs in the session will be lost after a certain period of time, you can avoid this situation by changing the session effective time.

While programming to avoid storing large amounts of useful information in the session, request is a good alternative.

Session built-in object common methods:

A.long getcreationtime ();//return session creation time

B.public String getId ();//returns the unique ID number that the JSP engine sets for the session when it is created

C.public Object SetAttribute (String name,object value);//Bind the objects to this session with the specified name

D.public Object GetAttribute (String name);//returns the specified name in this session bound together with the objects, or null if no object is bound under that name

E.string[] Getvaulenames ();//Returns an array containing all the properties available in the session

F.int getmaxinactiveinterval ();//Return two request interval how long this session is canceled (in seconds)

Session life cycle: Divided into three steps, one: Create two: Activity III: Destruction

The process of creating a session is actually the process of SessionID described above.

Two. Activities:

① a new page opened through hyperlinks in a session belongs to the same session

② as long as the current session page is not all closed, the reopened browser window accesses the same project resource when it belongs to the same session

③ unless all pages of this session are closed after re-accessing a JSP or the servlet will create a new session

Note: Note that the original session still exists, but the old one still exists on the server, but no more clients will carry it and then hand over to the server check

Three. Destruction: There are only three ways to destroy the session

① call Session.invalidate () method

②session expires (timed out), the Tomcat default setting time is 30 minutes, can be session.setmaxinactiveinterval (time), unit is seconds, to set the duration of the session.

③ Server Restart

Configuring timeout session destruction in the Web. xml file can also

<!--set the session to expire one minute, note that the units here are in minutes--

<session-config>

<session-timeout>1</session-timeout>

</session-config>

5.Application Built-in objects

A, to achieve the sharing of data between users, can store global variables. (similar to static objects)

b, starting at server start, terminating at server shutdown (life cycle)

C, the same property of the Application object can be manipulated in the connection between the user's front and back connections or different users

D. Manipulating Application object properties anywhere will affect access to other users

E, Application object is an instance of the ServletContext class

Common methods for application objects:

a.pblic void SetAttribute (String name,object value); Binds an object to this session with the specified name

B.public Object GetAttribute (String name), returns the objects that are bound to the specified name in this session, and returns null if no object is bound under that name.

C.enumeration getattributenames (); Returns an enumeration of the names of all available properties

D.string getserverinfo (); Returns the JSP (SERVER) engine and version number 6:page built-in objects

The Page object is pointing to the current JSP page itself, a bit like the this pointer in the class, which is an instance of the Java.lang.Object class. Common methods are as follows:

A. Class GetClass ();//returns this object class

b. int hashcode ();//Returns the hash code of this object

C. Boolean equals (Object obj);//Determines whether this object is equal to the specified object

D. void copy (object obj);//Copy this object to the specified Object object

E. Object Clone ();//Clone This object

F. String toString ();//Convert this object to the object of the String class

g. Void notify ();//Wake up a waiting thread

H. void Notyfyall ();//Wake up of all waiting threads

I. void wait (int timeout);//causes a thread to wait until timeout ends or is awakened

J. void Wait ();//causes a thread to wait until it wakes up

7.pageContext Built-in objects

* The PageContext object provides access to all objects and namespaces within the JSP page

* The PageContext object can be accessed to the session where this page is located, or it can be a property value of the application

* The PageContext object is equivalent to the synthesizer of all the functions on the page

* This class name of the PageContext object is also called PageContext

*jspwriter getout (); Returns the JspWriter stream (out) of the current client response being used

*httpsessiongetsession (); Returns the HttpSession object (session) in the current page

*objectgetpage (); Object (object) that returns the current page

*serveletrequestgetrequest (); Returns the ServletRequest object for the current page (request)

*serveletresponse GetResponse (); Returns the Servletresponse object of the current page (response)

*void SetAttribute (String name,object attribute); Setting properties and Property values

*void getattribute (String name,int scope); Takes the value of a property within a specified range

*int Getattributescope (String name); Returns the scope of a property

*void forward (String Relativeurlpath); redirect the current page to another page

*void include (String relativeurlpath); Include another file in the current location

8.config Built-in objects

The Config object is used by the JSP engine to pass information to it when a servlet is initialized.

This information includes parameters to be used when the servlet initializes (through property names and property values) and information about the server (by passing a ServletContext object)

Common methods:

A.servletcontext Getservletcontext (); Returns the ServletContext object that contains information about the server

B.string Getinitparameter (String name); Returns the value of the initialization parameter

C.enumeration getinitparameternames (); Returns an enumeration of all parameters required by the servlet initialization

9.Execption objects

The exception object is an exception object that is generated when a page is in the process of running an exception.

If a JSP page is to be applied to this object, the Iserrorpage must be set to true, otherwise it cannot be compiled.

is actually the object of java.lang.Throwable, the common method is as follows:

A. String getMessage (); Returns a message describing the exception

B. String toString (); Returns a short description message about the exception

c. void Printstacktrace (); Show exceptions and their stack traces

D. Throwable fillinstacktrace (); Rewrite the exception's execution stack trajectory

How to use:

A. In the page directive that may throw an exception, set errorpage= "xxx.jsp" to indicate that an exception will be thrown to the XXX page for processing

B. In the XXX page, to use the exception object, you need to set the Iserrorpage property in the page directive to true.

Java Server Pages built-in objects

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.