JSP nine large built-in objects and servlet learning notes [go]

Source: Internet
Author: User
Tags sessions

We often say JSP has nine large built-in objects: request, response, session, out, PageContext, page, exception, application, CONFIG.

We know that the JVM only recognizes Java classes and does not recognize JSP code, so how are these objects generated? This will refer to the servlet.

The nature of JSP is servlet, Servlet is the predecessor of JSP, JSP is the extension of servlet, when JSP is first accessed, the Web container compiles JSP code into Java class that the JVM can recognize, and the JSP is compiled into a servlet. (JSP and servlet contact)

The built-in objects in the JSP are obtained by HttpServletRequest objects, HttpServletResponse objects, and HttpServlet objects.

JSP is embedded Java code in HTML, focusing on page display, while servlet is HTML code separate from Java code, focus on logical control. (JSP differs from servlet)

1. Request object (corresponds to Javax.servlet.http.HttpServletRequest object after translation)

The Request object represents the requested information of the client and is primarily used to accept data that is transmitted to the server via the HTTP protocol (including header information, System Information, request method, request parameters, etc.).

The life cycle of the Request object: one request.

The scope of the Request object: Valid within the JSP page.

Common methods of Request:

1 Object getattribute (String name)----Returns the property value of the specified property
2 enumeration Getattributenames ()----Returns an enumeration of all available property names
3 String getcharacterencoding ()----Returns the character encoding method
4 int getcontentlength ()----Returns the length of the request body (in bytes)
5 String getContentType ()----Gets the MIME type of the request body (multi-purpose Internet Mail extension type)
6 ServletInputStream getInputStream ()----get a binary stream of one row in the request body
7 string GetParameter (string name)----returns the parameter value of name specified parameter
8 enumeration Getparameternames ()----Returns an enumeration of the available parameter names
9 string[] Getparametervalues (String name) returns an array of all values that contain the parameter name
Ten String Getprotocol ()----Returns the protocol type and version number of the request
String getscheme ()----Returns the plan name of the request, such as: Http.https and FTP, etc.
String getservername ()----Returns the server host name that accepts the request
int Getserverport ()----Returns the port number used by the server to accept this request
BufferedReader getreader ()----Returns the decoded request body
The String getremoteaddr ()----Returns the IP address of the client that sent this request
String getremotehost ()----Returns the host name of the client that sent this request
$ void SetAttribute (String key,object obj)---Setting property values for a property
Stringgetrealpath (String path)----Returns the true path of a virtual path
String Request.getcontextpath ()----Returns the context path

2. Response object (corresponding Javax.servlet.http.HttpServletResponse object after translation)

The response object represents the server-side response to the client. It is mainly used for setting header information, jumping, cookies and so on.

Response life cycle: a single response.

Response scope: Valid only within the JSP page.

Response Common methods:

public void  setheader (java.lang.String  name, java.lang.String  value)     ----Setting the name and contents of header information
public void Sendredirect (java.lang.String location) throws Java.io.IOException  ----jump, from one page to another page
public void Addcookie (Cookie  cookie)                                      ,         &NB Sp              ----Add cookies to the client
public void setContentType (java.lang.String type)    ----Set the return type of the content

After understanding the use of request and response, there is an important issue to be clear, the difference between request transfer and request redirection:

Request redirection: Response.sendredirect (), client behavior, essentially equivalent to two requests, the previous request object will not be persisted, and the Address bar URL will change.

Request Forwarding: Request.getrequsetdispatcher (). Forward (Requset,response); server behavior, is a request, after forwarding the request object will be saved, the Address bar URL address will not change. (Intra-server forwarding, all clients do not see the change in the Address bar).

3. Session object (corresponds to Javax.servlet.http.HttpSession object after translation)

Session objects are used to store the information required for a particular user session.

Session object Life cycle: Starts when data is deposited and expires after 30 minutes of inactivity.

Session Object scope: valid within sessions. ( starting with a client opening the browser and connecting to the server, the client closes the browser and leaves the end of the server, which is called a session ; Because HTTP is stateless, the session needs to use a cookie as the identification mark.) The cookie is automatically generated by the server, and its MaxAge property is typically-1, which means that only the current browser is valid, and the browser windows are not shared, and the browser is disabled. )

Session Common methods:

Public String getId () ----Gets the session object number.
public void SetAttribute (String key,object obj)----Add the object obj specified by the parameter object to the Session object and specify an index keyword for the added object.
public Object GetAttribute (String key)----Gets the object that contains the keyword in the session object.
Public Boolean isnew ()----Determine if it is a new session

4. Application object (corresponding Javax.servlet.ServletContext object after translation)

The Application object is used to store and access variables from any page, similar to the Session object. The difference is that all users share a Application object, similar to the "global variable", and the Session object and the user's relationship is one by one corresponding.

Application Object life cycle: When the server starts sending the first request, it produces the Application object until the server shuts down.

Common methods for application objects:

SetAttribute (String key,object obj)----Adds the object specified by the parameter object, obj, to the Application object, and assigns an index keyword to the added object.
GetAttribute (String key)----Gets the object that contains the word in the Application object.

5. Out object (corresponds to Javax.servlet.jsp.jspWriter object after translation)

The Out object is used to output information within a Web browser and to manage the output buffers on the application server. (Note to close the output stream in a timely manner)

The main methods are as follows:

Clear ()----clears the data in the buffer and, if the buffer is already empty, generates a IOException exception;
Clearbuffer ()----Clears the buffer's data and, if the buffer is empty, does not produce an IO exception;
Flush ()-----Directly the data output currently staged in the buffer;
GetBufferSize ()----Returns the size of the buffer;
Getremaining ()----Returns the remaining space size of the buffer;
Isautoflush ()----Returns a Boolean value indicating whether the data of the buffer is automatically output;
Some ways to output data:
NewLine ()----output line-wrapping;
Print (datatype data)----output different data types;
println (datatype data)----output different data types, and automatically wrap;

6. PageContext object (corresponding Javax.servlet.jsp.PageContext object after translation)

PageContext objects can access other hidden objects, such as request, reponse, session, application, and so on. ( in fact, the PageContext object provides access to all the objects and namespaces of the JSP page.) )

Pagecontex Common methods:

void SetAttribute (String name, object value, int scope)----PageContext object to access other suppressed object properties, you need to specify a range of parameters: The range parameter has four, Represents four different ranges: Page_scope, Request_scope, Session_scope, Application_scope

The PageContext object obtains other hidden objects by means of:

Exception getexception ()----The exception for the postback page
JspWriter getout ()----The output stream of the backhaul Web page
Object getpage ()----The servlet entity for the postback Web page
ServletRequest getrequest ()----request to return a webpage
Servletresponse getResponse ()----response to the callback page
ServletConfig getservletconfig ()----Callback ServletConfig object for this page
ServletContext Getservletcontext ()----callback the execution environment for this page
HttpSession getsession ()----callbacks and web-related sessions
3.PageContext objects provide a way to get properties
Object GetAttribute (String name, int scope)----Callback Name property, Scope Property object, callback type is Object
Enumeration Getattributenamesinscope (int scope)----Callbacks all property names with scope, and the callback type is enumeration
int Getattributesscope (String name)----Property range with name of the callback property
void RemoveAttribute (String name)----Remove Property object named Name
void RemoveAttribute (String name, int scope)----Remove Property object named Name, scope
void SetAttribute (String name, object value, int scope)----Specifies the name of the Property object, the value of which is scope
Object Findattribute (String name)----Looking for Property object with name in all scopes

7. config object (corresponds to Javax.servlet.ServletConfig object after translation)

The main purpose of the Config object is to obtain the server configuration information.

Common methods for Config objects:

Getservletcontext ()----Returns a ServletContext object that contains information about the server.
Getintparameter (String name)----Returns the value of the initialization parameter.
The Getintparameternames ()----Returns all parameters that are required to initialize the servlet, and the return type is enumerated

8. Page object (corresponds to this after translation)

The Page object represents the JSP itself and is only legal within the JSP page. The Page object is somewhat similar to the this pointer in Java programming, meaning the current JSP page itself.

9. Exception object (corresponding Java.lang.Throwable object after translation)

The purpose of the exception object is to display exception information, which must be set in the page directive <%@ page iserrorpage= "true"%> to be used, which will not compile the JSP file in a generic JSP page.

Common methods for exception objects:

GetMessage ()----The method returns an error message.
Printstacktrace ()----The method outputs a stack of errors and errors in the form of a standard error.
ToString () ----The method returns a description of the exception in the form of a string.

JSP nine large built-in objects and servlet learning notes [go]

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.