Five ------ nine built-in Jsp objects.

Source: Internet
Author: User

Five ------ nine built-in Jsp objects.

Nine built-in Jsp objects, the most important of which are the first five objects.


5-1
Out object
An out object is an instance of the JSPWriter class and is a common object for outputting content to the client.
Common Methods for out objects are as follows:
1, out. println (): print the string to the client
2, out. clear (): clears the buffer content. If it is called after flush, an exception is thrown.
3, out. clearBuffer (): clears the buffer content. If it is called after flush, no exception is thrown.
4, out. flush (): outputs the buffer content to the client.
5, int getBufferSize (): returns the size of the number of bytes in the buffer. If no buffer is set, the value is 0.
6, int getRemaining (): returns how much available the buffer is.
7, boolean isAutoFlush (): whether to automatically clear or throw an exception when the returned buffer is full
8, out. close: close the output stream.


5-2
Built-in request object
The request information of the client is encapsulated in the request object, so that the client can understand the customer's requirements and then respond. It is an HttpServletRequest class.


. The request object has a request domain, that is, the object remains valid until the client completes the request. The common method is as follows.
1. password: <% = request. getAttribute ("password") %>
,
2. MIME Type of the request body: <% = request. getContentType () %>

3. protocol type and version: <% = request. getProtocol () %>
,
4. Server Host Name: <% = request. getServerName () %>

5. server port: <% = request. getServerPort () %>

6. Length of the request file: <% = request. getContentLength () %>

7. IP address of the request client: <% = request. getRemoteAddr () %>

8. Actual request path: <% = request. getRealPath (path) %>

9. request context path: <% = request. getContextPath () %>

10. Set the authorization code: request. setCharacterEncoding ("UTF-8 ");
11. Set the attribute value (for example, set the password): request. setAttribute ("password", "123456 ");
12. Obtain a single property value (taking the user name as an example): <% = request. getParameter ("username") %>

13. Obtain multiple attribute values (take the user-submitted hobby as an example ):
<%
If (request. getParameterValues ("favorite ")! = Null ){
String [] favorites = request. getParameterValues ("favorite ");
For (String s: favorites ){
Out. println (s + "& nbsp ;");
}
}
%>


5-3
Response object
The response object contains information about the response to the customer request, but is rarely used directly in JSP. It is an instance of the HttpServletResponse class.


The response object has a page scope. when accessing a page, the response object in the page can only be valid for this access.


The following are common methods to disable an object for the current page.
1, response. setContentType ("text/html; charset = UTF-8"); set the MIME type of the response
2, String getCharacterEncoding (); returns the encoding type of the response.
3. PrintWriter getWriter () returns an object that can output characters to the client.
4, response. sendRedirect ("reg. jsp"); // redirect requests to jump to the specified page.
Note: The difference between getWriter and out Built-in objects in PrintWriter:
The content output by getWriter always comes before the output content
The solution is to force out. flush before the code output by outer.


The following is an instance of the response page.
<% @ Page import = "java. io. PrintWriter" %>
<% @ Page language = "java" import = "java. util. *" contentType = "text/html; charset = UTF-8" %>
<%
Response. setContentType ("text/html; charset = UTF-8"); // you can specify the MIME type of the response.

Out. println ("response built-in object ");
Out. println ("");
Out. flush (); // clear the buffer before the output outer.

PrintWriter outer = response. getWriter (); // gets the output stream object
Outer. println ("the output content of outer always comes before the output content of out.
The solution is to force


Out. flush ");
// Response. sendRedirect ("reg. jsp"); // redirect requests to jump to the specified page.
%>


5-4
Session built-in object
1. What is session?
Session indicates a session between the client and the server.
The session in the web refers to the period from when a user browses a website to when the user closes the website, that is, when the user browses the website


Time spent.
From the above definition, we can see that session is actually a specific concept of time.


2. session Object
The session object is a JSP built-in object.
The session object is automatically created when the first JSP page is loaded to complete session Period Management.
A session starts when a client opens a browser and connects to the server, and ends when the client closes the browser and leaves the server.
When a customer accesses a server, it may switch between several pages of the server. The server should know in some way that this is a customer.


Session object.
The session object is an instance of the HttpSession class.


3. Common Methods for session objects are as follows:
1) long getCreationTime (): returns the session creation time.
2) public String getId (): return the unique ID number set by the JSP Engine when the session is created.
3) public Object setAttribute (String name, Object value): bind the Object with the specified name to this session.
4) public Object getAttribute (String name): returns the Object bound with the specified name in this session. If no Object is bound to this


The return value is null.
5) String [] getValueNames (): returns an array containing all the available attributes of this session.
6) int getMaxInactiveInterval (): the interval between two requests returned. The session is canceled (in seconds)


4. session Lifecycle
1) Create:
When the client accesses a JSP or Servlet for the first time, the server creates a SessionId for the current session, and each time the client sends a request to the server


The SessionId will be carried in the past, and the server will verify the SessionId.


2) Activities:
A new page opened through a hyperlink in a session belongs to the same session.
As long as the current session page is not closed, re-open a new browser window to access resources of the same project belongs to the same session.
Unless all the pages of this session are closed and then re-access a JSP or Servelt will create a new session.
Note: When a new session is created, the original session still exists, and the old SessionId still exists on the server, but no client will carry it


Submit it to the server for verification.


3) Destruction
Only three methods are available for session destruction.
A. The session. invalidate () method is called.
B. We recommend that you destroy the expired Session (timeout.
C. Restart the server


Supplement
The default session timeout time for Tomcat is 30 minutes.
You can set session timeout in either of the following ways:
1, session. setMaxInactiveInterval (time); // unit: seconds
2. Configure in web. xml

Time

// The unit is minute.




5-5
1. application Object
The application Object shares data between users and stores global variables.
Different from the session object, the application object of all customers is the same, that is, all customers share the application object.


The application object can act as a global variable.
The application starts from the start of the server and ends with the shutdown of the server.
You can perform operations on the same attribute of the application object in the user's frontend and backend links or connections between different users.
Any operation on application object attributes affects access by other users.
The startup and shutdown of the server determine the life of the application object.
The application object is an instance of the ServletContext class.


2. Common Methods for application objects are as follows:
1) public void setAttribute (String name, Object value) binds the Object to this session with the specified name.
2) public Object getAttribute (String name) returns the Object bound with the specified name in this session. If no Object is bound to this


The return value is null.
3) Enumeration getAttributeNames () returns the Enumeration of all available attribute names.
4) String getServerInfo (): returns the JSP (Servlet) engine name and version number.


5-6
Page Object
The page Object points to the current JSP page itself, a bit like the this pointer in the class. It is an instance of the java. lang. Object Class. The common method is as follows:
1, class getClass (): class that returns this Object
2, int hashCode (): returns the hash code of this Object.
3, boolean equals (Object obj): determines whether the Object is equal to the specified Object.
4. void copy (Object obj): copy the Object to a custom Object.
5. Object clone (): clone the Object:
6. String toString (): Convert the Object to a String Object.
7. void y (): Wake up a waiting thread.
8, void policyall (): wake up all the waiting threads
9, void wait (int timeout): enables a thread to wait until the timeout ends or is awakened.
10, void wait (): enables a thread to wait until it is awakened.


5-7
PageContext object
The pageContext object provides access to all objects and namespaces on the JSP page.
The pageContext object can access the session on this page, or obtain a property value of the application on this page.
The pageContext object is equivalent to all functions of the page.
This class name of the pageContext object is also called pageContext.


The common method is as follows:
1. JSPWriter getOut (): return the JspWriter stream (out) in use in the current client response)
2. HttpSession getSession (): returns the HttpSession object (session) on the current page)
3. Object getPage (): returns the Object (page) of the current page)
4, ServletRequest getRequest (): returns the ServletRequest object (request) of the current page)
5, ServletResponse getResponse (): returns the ServletResponse object (response) of the current page)
6. void setAttribute (String name, Object attribute): sets attributes and attribute values.
7. Object getAttribute (String name, int scope): obtains the attribute value within the specified range.
8, int getAttributeScope (String name): returns the scope of an attribute.
9, void forward (String relativeUrlPath): redirect the current page to another page
10, void include (String relativeUrlPath); contains another file in the current position


5-8
Config object
The config object is used by the JSP Engine to transmit information to a Servlet during initialization. This information includes the parameters used during Servlet initialization (through


Attributes and attribute values) and server-related information (passing a ServletContext object) are commonly used as follows:
1. ServletContext getServletContext (): returns the ServletContext object containing server-related information.
2, String getInitParameter (String name (): return the value of the initialization parameter.
3. Enumeration getInitParameterNames (): returns the Enumeration of all parameters required for Servlet initialization.


5-9
Exception object
The exception object is an exception object. When a page encounters an exception during running, this object is generated. If a JSP page needs to apply this object


IsErrorPage must be set to true. Otherwise, compilation fails. It is actually a java. lang. Throwable object. The common method is as follows:
1, String getMessage (): return the message describing the exception.
2, String toString (): returns a brief description message about the exception.
3. void printStackTrace (): displays exceptions and stack traces.
4. Throwable FillInStackTrace (): rewrite the execution stack trace of an exception.

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.