JSP nine built-in objects and servlet learning notes __jsp

Source: Internet
Author: User
Tags sessions

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

We know that the JVM can only recognize Java classes and not recognize JSP code, so how are these objects generated? This will refer to the servlet. A servlet is a server-side program written in Java. A narrow servlet is an interface to the Java language implementation, and a generalized servlet refers to any class that implements this servlet interface.

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

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

JSP is embedded in HTML Java code, focus on the page display, while the servlet is the HTML code and Java code separation, focusing on logical control. (JSP differs from servlet) 1. Request object (corresponding Javax.servlet.http.HttpServletRequest object after translation)

The Request object represents the client's requested information, which is used primarily to accept data sent to the server via the HTTP protocol (including header information, System Information, request methods, request parameters, and so on).

Life cycle of the Request object: one request.

Scope of the Request object: Valid within the JSP page.

Common methods to 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 ()----return character encoding
4 int getcontentlength ()----Returns the length of the requested body (in bytes)
5 String getContentType ()----Get the MIME type of the requested body (Multipurpose Internet Mail Extension type)
6 ServletInputStream getInputStream ()----get the binary stream of one line in the request body
7 string GetParameter (string name)----returns the parameter value of the specified parameter of name
8 enumeration Getparameternames ()----Returns an enumeration of the available parameter names
9 string[] Getparametervalues (String name) returns an array of all values containing the parameter name
A String getprotocol ()----Returns the type and version number of the protocol used for the request
One String getscheme ()----Returns the plan name for the request, such as Http.https and FTP
A String getservername ()----Returns the server host name that accepts the request
int Getserverport ()----Return the port number used by the server to accept this request
BufferedReader getreader ()----Returns the decoded request body
String getremoteaddr ()----Returns the client IP address that sent this request
String getremotehost ()----Returns the client host name that sent the request
$ void setattribute (String key,object obj)---Set property value for property
Stringgetrealpath (String path)----Returns the true path of a virtual path
String Request.getcontextpath ()----Return context Path


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

The response object represents the server-side response to the client. Mainly used to set header information, jump, cookies and so on.

Response life cycle: one response.

Scope of the response: only valid within the JSP page.

Response Common methods:

public void SetHeader (java.lang.String name, java.lang.String value)----The name and contents of the 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)----Add cookies to the client
public void setContentType (java.lang.String type)----Set the return type of the content

After understanding the usage of request and response, there is an important question 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, the Address bar URL address 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. (Forwarding within the server, all clients do not see the change in the Address bar). 3. Session object (corresponding Javax.servlet.http.HttpSession object after translation)

The session object is used to store the information that is required for a particular user conversation.

Session Object lifecycle: From the time the data is stored, the default is idle for 30 minutes after the expiration.

Session Object scope: valid within sessions. (from a customer open the browser and connect to the server to start, to the customer shut down the browser to leave the server end, is called a session; Because HTTP is stateless, sessions need to use cookies as a recognition flag.) This cookie is automatically generated for 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, closing the browser will fail. )

Session Common methods:

The public String getId ()----Gets the session object number.
The 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.
The 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 objects (corresponding Javax.servlet.ServletContext objects 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 "global variables", and the Session object and the user's relationship are one by one corresponding.

Application Object lifecycle: When the server initiates the first request, the application object is generated until the server shuts down.

Common methods for application objects:

SetAttribute (String key,object obj)----Add the object obj specified by the parameter object to the Application object and specify an index keyword for the object that is added.
GetAttribute (String key)----Gets the object containing the word in the Application object.
5. Out objects (corresponding Javax.servlet.jsp.jspWriter objects after translation)

An 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 time)

The main methods are as follows:

Clear ()----clears the data in the buffer and generates a IOException exception if the buffer is already empty;
Clearbuffer ()----Clears the buffer data, if the buffer is empty, does not produce IO exception;
The flush ()-----directly to the data currently temporarily stored in the buffer output;
GetBufferSize ()----Returns the size of the buffer;
Getremaining ()----Returns the remaining space size of the buffer;
The Isautoflush ()----Returns a Boolean value indicating whether the data for the buffer is automatically exported;
Some ways to output data:
NewLine ()----output line wrapping;
Print (datatype data)----output data of different data types;
println (datatype data)----output data of different data types, and automatically wrap lines;

6. PageContext objects (corresponding Javax.servlet.jsp.PageContext objects 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, at which point you need to specify a range of parameters: There are four range parameters, Representing four different ranges: Page_scope, Request_scope, Session_scope, Application_scope

The PageContext object obtains methods for other suppressed objects:

Exception getexception ()----The exception that returned the page
JspWriter getout ()----The output stream of the return page
Object getpage ()----servlet entity to return a Web page
ServletRequest getrequest ()----request to return the Web page
Servletresponse GetResponse ()----response to the return page
ServletConfig getservletconfig ()----Return the ServletConfig object for this page
ServletContext getservletcontext ()----Return the execution environment of this web page
HttpSession getsession ()----return and web-connected sessions
3.PageContext object provides methods to obtain properties
Object GetAttribute (String name, int scope)----Return the Name property, scoped to the Property object of scope, with the return type of object
Enumeration Getattributenamesinscope (int scope)----Return the name of all properties scoped to scope, the return type is enumeration
int Getattributesscope (String name)----Property scope with the name of the postback property
void RemoveAttribute (String name)----Remove a Property object with the property name named
void RemoveAttribute (String name, int scope)----Remove a Property object that has a property name named, scoped to scope
void SetAttribute (String name, Object value, int scope)----Specifies that the name of the Property object is named, values is value, range is scope
Object Findattribute (String name)----Look for property objects with name names in all scopes


7. config object (corresponding Javax.servlet.ServletConfig object after translation)

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

Config object common methods:

Getservletcontext ()----Returns a ServletContext object that contains information about the server.
Getintparameter (String name)----Returns the value of the initialization parameter.
Getintparameternames ()----Returns all the parameters that are required for servlet initialization, and the return type is an enumerated type
8, Page object (after translation corresponds to this)

The Page object represents the JSP itself and is valid only within the JSP page. The Page object is somewhat similar to the this pointer in Java programming, which refers to the current JSP page itself.
9. Exception objects (corresponding java.lang.Throwable objects after translation)

The function of the exception object is to display exception information that must be set in the page directive <%@ page iserrorpage= "true"%> to be used, and the JSP file cannot be compiled using the object 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 as a string.

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.