Interview questions: nine built-in objects of jsp

We often say that JSP has nine built-in objects: request, response, session, out, pagecontext, page, exception, application, and config.

We know that JVM can only recognize java classes, not JSP code, so how are these objects generated? This is to mention Servlet. Servlet is a server-side program written in Java. A narrow servlet refers to an interface implemented by the Java language, and a broad servlet refers to any class that implements the servlet interface.

The essence of JSP is Servlet, Servlet is the predecessor of JSP, and JSP is an extension of Servlet; when JSP is accessed for the first time, the Web container compiles the JSP code into Java classes that can be recognized by JVM, and JSP becomes Servlet after being compiled. . (JSP contacts Servlet)

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

JSP is embedded Java code in HTML, focusing on page display; while Servlet is HTML code separated from Java code, focusing on logic control. (Difference between JSP and Servlet)

1. The request object (corresponding to the javax.servlet.http.HttpServletRequest object after translation)

The request object represents the client's request information, and is mainly used to accept the data (including header information, system information, request method, and request parameters, etc.) transmitted to the server through the HTTP protocol.

The life cycle of the request object: a request.

The scope of the request object: valid within the JSP page.

Common methods of request: 

1 object getAttribute(String name) ----Returns the attribute value of the specified attribute
2 Enumeration getAttributeNames() ----Returns an enumeration of all available attribute 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 (Multipurpose Internet Mail Extension Type)
6 ServletInputStream getInputStream() ----Gets A binary stream of one line in the request body
7 String getParameter(String name) ---- Returns the parameter value of the parameter specified by name
8 Enumeration getParameterNames() ---- Returns an enumeration of available parameter names
9 String[] getParameterValues(String name) Returns an array containing all the values ​​of the parameter name
10 String getProtocol() ----Returns the protocol type and version number used for the request
11 String getScheme() ----Returns the plan name used for the request, such as: http.https and ftp Wait
12 String getServerName() ----Returns the host name of the server that accepts the request
13 int getServerPort() ----Returns the port number used by the server to accept this request
14 BufferedReader getReader() ----Returns the decoded request body
15 String getRemoteAddr() ---- Returns the IP address of the client that sent this request
16 String getRemoteHost() ---- Returns the host name of the client that sent this request
17 void setAttribute(String key, Object obj) --- Set The attribute value of the attribute
18 StringgetRealPath(String path) ----Returns the real path of a virtual path
19 String request.getContextPath() ----Returns the context path

 

 

 

 

2. The response object (corresponding to the javax.servlet.http.HttpServletResponse object after translation)

The response object represents the server's response to the client. Mainly used to set header information, redirects, cookies, etc.

The life cycle of the response: a response.

Scope of response: Only valid within JSP pages.

Common methods of response:

public void setHeader(java.lang.String name, java.lang.String value) ----Set the name and content of the header information
public void sendRedirect(java.lang.String location) throws java.io.IOException ---- Jump, jump from one page to another page
public void addCookie(Cookie cookie) ----Add a cookie 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 issue to be clear, the difference between request transfer and request redirection:

Request redirection: response.sendRedirect(), client behavior, is essentially equivalent to two requests, the previous request object will not be retained, and the URL address in the address bar will change.

Request forwarding: request.getRequsetDispatcher().forward(requset,response); The server behavior is a request. After forwarding, the request object will be saved, and the URL address in the address bar will not change. (The server is internally forwarded, and all clients cannot see the change in the address bar).

3. Session object (corresponding to javax.servlet.http.HttpSession object after translation)

The session object is used to store information required for a specific user session.

Session object life cycle: It starts when data is stored, and expires after 30 minutes of inactivity by default.

session object scope: valid within the session. (It starts when a client opens the browser and connects to the server, and ends when the client closes the browser and leaves the server. It is called a session; because http is stateless, the session needs to use a cookie as an identifier. The cookie is automatically generated by the server. Yes, its maxAge attribute is generally -1, which means that it is only valid in the current browser, and is not shared between browser windows, and it will be invalid when the browser is closed.)

Common methods of session:

public String getId() ---- Get the ID of the Session object.
public void setAttribute(String key, Object obj)----Add the object obj specified by the parameter Object to the Session object, and specify an index key for the added object.
public Object getAttribute(String key) ---- Get the object containing the keyword in the Session object.
public Boolean isNew() ---- determine whether it is a new session

 

 

4. The application object (corresponding to the 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 an Application object, which is similar to "global variables", and the relationship between session objects and users is one-to-one.

Application object life cycle: The Application object is generated when the server starts to send the first request until the server is shut down.

Common methods of application object:

setAttribute(String key, Object obj) ---- Add the object obj specified by the parameter Object to the Application object, and specify an index key for the added object.
getAttribute(String key) ---- Get the object containing the word in the Application object.

5. out object (corresponding to javax.servlet.jsp.jspWriter object after translation)

The out object is used to output information within a web browser and to manage output buffers on the application server. (Be careful to close the output stream in time)

Its main methods are as follows:

clear() ----Clear the data in the buffer, if the buffer is already empty, an IOException will be generated;
clearBuffer() ----Clear the data in the buffer, if the buffer is empty, it will not generate IO exception;
flush() ---- directly output the data currently temporarily stored in the buffer;
getBufferSize() ---- returns the size of the buffer;
getRemaining() ---- returns the remaining size of the buffer ;
isAutoFlush() ----Returns a boolean value indicating whether to automatically output the data
in the buffer; some methods of outputting data:
newLine() ----Output newlines;
print(datatype data) ----Output different data types data;
println(datatype data) ---- output data of different data types, and automatically wrap;

 

 

6. pageContext object (corresponding to javax.servlet.jsp.PageContext object after translation)

The pageContext object can access other implicit objects, such as request, response, session, application and other objects. (Actually, the pageContext object provides access to all the objects and namespaces of the JSP page.)

Common methods of pageContex:

void setAttribute(String name, Object value, int scope)----The method for the pageContext object to access other implicit object attributes. At this time, you need to specify the parameters of the scope: there are four scope parameters, representing four scopes: PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE, APPLICATION_SCOPE

The method of the pageContext object to obtain other implicit objects:

Exception getException( ) ---- Returns the exception of the webpage
JspWriter getOut( ) ---- Returns the output stream of the webpage
Object getPage( ) ---- Returns the Servlet entity of the webpage
ServletRequest getRequest( ) ---- Returns The request of the web page
ServletResponse getResponse( ) ----Returns the response of the web page
ServletConfig getServletConfig( ) ----Returns the ServletConfig object of this web page
ServletContext getServletContext( ) ----Returns the execution environment of this web page
HttpSession getSession( ) ---- Returns the session associated with the web page 3. The
PageContext object provides a method for obtaining attributes.
Object getAttribute(String name, int scope) ---- Returns the name attribute, the scope is the attribute object of the scope, and the return type For Object
Enumeration getAttributeNamesInScope(int scope) ---- Returns all attribute names whose attribute scope is scope, return type is Enumeration
int getAttributesScope(String name) ----Returns the attribute scope with the attribute name name
void removeAttribute(String name) ----Remove the attribute object with the attribute name name
void removeAttribute(String name, int scope) --- -Remove the attribute object whose attribute name is name and scope is scope
void setAttribute(String name, Object value, int scope) ----Specify the name of the attribute object as name, value as value and scope as scope
Object findAttribute(String name ) ---- finds the attribute object whose attribute name is name in all scopes

 

7. config object (corresponding to javax.servlet.ServletConfig object after translation)

The main function of the config object is to obtain the configuration information of the server.

Common methods of config object:

 getServletContext() ---- Returns a ServletContext object containing server-related information.
 getIntParameter(String name) ---- Returns the value of the initialization parameter.
 getIntParameterNames() ----Return contains all parameters required for Servlet initialization, the return type is an enumeration type

8. The page object (corresponding to this after translation)

The page object represents the JSP itself and is only legal within a 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 object (corresponding to java.lang.Throwable object after translation)

The function of the exception object is to display the exception information. It must be set in the page directive < %@ page isErrorPage="true" %> to use it. If this object is used in a general JSP page, the JSP file cannot be compiled.

Common methods of exception object:

getMessage( ) ---- This method returns an error message.
printStackTrace( ) ---- This method prints a stack of errors and errors to standard error.
toString() ---- This method returns a description of the exception in the form of a string.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325386334&siteId=291194637