This article introduces nine built-in Java objects for your reference. The specific content is as follows
1. Request object <br />This object encapsulates the information submitted by the user. By calling the corresponding method of the object, the encapsulated information can be obtained, that is, the information submitted by the user can be obtained using the object.
When the Request object obtains the Chinese characters submitted by the client, garbled code problems will occur and special treatment must be carried out. First, encode the obtained string with ISO-8859-1, and store the code in a byte array, and then convert the array into a string object. as follows:
String textContent=request.getParameter("boy");byte b[]=textContent.getBytes("ISO-8859-1");textContent=new String(b);Common methods for Request:
1.01 getParameter(String strTextName) Gets the information for the form submission. String strName=request.getParameter("name"); 1.02 getProtocol() Gets the protocol used by the client. String strProtocol=request.getProtocol(); 1.03 getServletPath() Gets the page where the client submits information. String strServlet=request.getServletPath(); 1.04 getMethod() Get the way to submit information by the customer, get|post. String strMethod = request.getMethod(); 1.05 getHeade() Gets the values of accept, accept-encoding and Host in the HTTP header file. String strHeader = request.getHeader("accept"); 1.06 getRermoteAddr() Gets the IP address of the client. String strIP = request.getRemoteAddr(); 1.07 getRemoteHost() Gets the name of the client. String clientName = request.getRemoteHost(); 1.08 getServerName() Gets the server name. String serverName = request.getServerName(); 1.09 getServerPort() Gets the port number of the server. int serverPort = request.getServerPort(); 1.10 getParameterNames() Gets the names of all parameters submitted by the client. Enumeration enum = request.getParameterNames(); while(enum.hasMoreElements()){ String s=(String)enum.nextElement(); out.println(s);} 2. Response object
Make a dynamic response to customer requests and send data to the client.
2.1 Dynamic response to contentType property
When a user accesses a JSP page, if the page uses the page directive to set the contentType property of the page and text/html, the JSP engine will react according to this property value. If you want to dynamically change this property value to respond to the customer, you need to use the setContentType(String s) method of the Response object to change the property value of the contentType.
Format: response.setContentType(String s);
Parameters s can be taken as text/html, application/x-msexcel, application/msword, etc.
2.2 Response Redirect <br />In some cases, when responding to a customer, the customer needs to be redirected to another page. Response's sendRedirect (URL) method can be used to implement the redirection of the customer. For example:
response.sendRedirect("index.jsp");
3. Session object
(1) What is a Session object
The Session object is a built-in JSP object that is automatically created when the first JSP page is loaded to complete session management. Starting from a client opening a browser and connecting to the server, ending with a client closing the browser and leaving the server, it is called a session. When a client accesses a server, it may switch between several pages of the server. The server should know in some way that this is a client and needs a Session object.
(2) The ID of the Session object
When a client accesses a JSP page on the server for the first time, the JSP engine generates a Session object and assigns a String-type ID number. The JSP engine also sends this ID number to the client and stores it in the cookie. In this way, the Session object will not be cancelled until the client closes the browser, and the session correspondence with the client disappears. When the client reopens the browser and connects to the server, the server creates a new Session object for the client.
(3) Common methods of Session objects
● public String getId(): Get 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 containing keywords in the Session object.
● public Boolean isNew(): determines whether it is a new customer.
4. Application object (1) When the Application object <br />The Application object is generated after the server starts. When the client browses between the various pages of the website visited by the client, the Application object is the same until the server closes. However, when different from the Session object, all customers' Application object is the same, that is, all customers share this built-in Application object.
(2) Common methods of 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 added object.
● getAttribute(String key): Gets the object containing keywords in the Application object.
5. Out object
An output stream when Out object is used to output data to the client. Out object is used for output of various data. The commonly used methods are as follows.
● out.print(): Output various types of data.
● out.newLine(): Output a newline character.
● out.close(): close the stream.
6. Cookie Object
(1) What is a Cookie
A cookie is a piece of text saved by a web server on the user's hard disk. Cookies allow a web site to save information on the user's computer and then retrieve it.
For example, a web site may generate a unique ID for each visitor and then save it on each user's machine as a cookie file.
If the user uses IE browser to access the web, the user will see all cookies saved on his or her hard drive. The places they are most often stored are: C:/Windows/Cookies. Cookies save records in the format of "keyword key=value".
(2) Create a Cookie object <br />Create a Cookie object by calling the constructor of the Cookie object. The constructor of the cookie object has two string parameters: the cookie name and the cookie value.
For example: Cookie c = new Cookie("username","john");
(3) Transfer the cookie object to the client
In JSP, if you want to transfer the encapsulated cookie object to the client, you can use the addCookie() method of the Response object.
For example: response.addCookie(c).
(4) Read the cookies saved to the client
Use the getCookie() method of the Request object. When executing, all cookie objects sent from the client are arranged in an array. If you want to take out the cookie objects that meet the needs, you need to loop over and compare the keywords of each object in the array.
For example:
Cookie[] c = request.getCookies(); if(c != null) for(int i = 0;i < c.length;i++){ if("username".equals(c.getName())) out.println(c.getValue()); } (5) Set the valid time of the cookie object
Calling the setMaxAge() method of the cookie object can set the valid time of the cookie object.
For example: Cookie c = new Cookie("username","john");
c.setMaxAge(3600);
(6) Cookie application
Cookie objects are used to count the number of visitors to the website when they are typical of the application. Due to the use of proxy servers, caches, etc., the only way to help the website accurately count the number of visitors is to establish a unique ID for each visitor. Using cookies, the website can do a job.
● Determine how many people have visited.
● Measure how many visitors are new users (i.e., first visit) and how many old users are.
● Determine how often a user visits the website. When a user first visits, the website establishes a new ID in the database and transmits the ID to the user through a cookie. When the user visits again, the website increases the counter corresponding to the user ID by 1 to get the number of visits from the user.
7. Config object [not commonly used]
Configure object page object.
The built-in config object is an instance of the ServletConfig class, which is used by the JSP engine to pass information to it (Servlet) through config when the Servlet is initialized. This information can be a parameter that matches the attribute name/value, or it can be information about the server passed through the ServletContext object. Generally, in JSP development, config built-in objects are rarely used, and they will only be used when writing servlets if you need to overload the init() method of the servlet.
Common methods for config objects
getServletContext(): Returns a ServletContext object containing server-related information.
getIntParameter(String name): Returns the value of the initialization parameter.
getIntParameterNames(): Returns all parameters required for Servlet initialization, and the return type is enumerated.
PageContext object [not commonly used]
The built-in object of pageContext is a special object, which is equivalent to the largest integrator of all other object functions in the page, that is, use it to access all other objects in this page, such as request, response, out and page objects described above, etc. Since objects such as request and response can be used directly in JSP, pageContext objects are rarely used in actual JSP development.
Common methods of pageContext object
getRequest(): Returns the request object in the current page. getResponse(): Returns the response object in the current page. getSession(): Returns the session object in the current page. getServletContext(): Returns the application object in the current page. getPage(): Returns the page object in the current page. getOut(): Returns the out object in the current page. getException(): Returns the exception object in the current page. getServletConfig(): Returns the config object in the current page. setAttribute(String name): Set attribute value for the specified attribute name. getAttribute(String naem): Find the corresponding attribute value based on the attribute name. setAttribute(String name, Object obj, int scope): Set the corresponding attribute value within the given range. getAttribute(String name, int scope): Get the corresponding attribute value within the given range. findAttribute(String name): Find an attribute and return it, and if it cannot be found, return null. removeAttribute(String name): Removes a certain attribute by the attribute name. removeAttribute(String name, int scope): Removes a certain attribute in a specified range. getAttributeScope(String name scope): Returns the scope of a certain attribute. getAttributeNamesInScope(int scope): Returns an enumeration of all attribute names within the specified range. release(): Release all data occupied by pageContext. forward(String relativeURLpath): Use the current page to re-transmit to another page. include(String relativeURLpath): Use another page contained in the current location.
8. Page object [not commonly used]
The page object is somewhat similar to this pointer in Java programming, which refers to the current JSP page itself. page is an object of the java.lang.Object class. Page objects are not often used during actual development.
Common methods of page object
getClass(): Returns the class of the Object at that time.
hashCode(): Returns the hash code of the Object at this time.
toString(): Convert the Object class at this time into a string.
equals(Object ob): Compare whether this object is equal to the specified object.
copy(Object ob): Copy this object into the specified object.
clone(): clone this object.
9. Exception object
The exception implicit object can be accessed directly in the web page that handles exceptions.
Page context object
Jsp introduces a class with a nominal PageContext, through which many properties of the page can be accessed.
The PageContext class has methods such as getRequest, getResponse, getOut, getSession, etc.
The pageContext variable stores the value of the PageContext object associated with the current page.
repair:
If the method needs to access multiple page-related objects,
Passing pageContext is easier than passing independent references of request, response, out, etc. (Although both methods can achieve the same goal)
The above is all about this article, I hope it will be helpful to everyone's study.