1. 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 instance of the httpservletrequest class.
Serial number method description
1 object getattribute (string name) returns the attribute value of the specified attribute
2 enumeration getattributenames () returns the 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 () Get the MIME type of the Request body
6. servletinputstream getinputstream () obtains the binary stream of a row in the Request body.
7 string getparameter (string name) returns the parameter value of the specified name parameter.
8 enumeration getparameternames () returns the enumeration of available parameter names
9 string [] getparametervalues (string name) returns an array containing all values of the parameter name
10 string getprotocol () returns the protocol type and version number of the request.
11 string getscheme () returns the scheduler name used for the request, such as HTTP. HTTPS and FTP.
12 string getservername () returns the server host name that receives the request
13 int getserverport () returns the port number used by the server to accept the request.
14 bufferedreader getreader () returns the decoded Request body
15 string getremoteaddr () returns the IP address of the client sending the request.
16 string getremotehost () returns the client host name that sent this request
17 void setattribute (string key, object OBJ) set the attribute Attribute Value
18 string getrealpath (string path) returns the actual path of a virtual path.
19
20
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% Request. setcharacterencoding ("gb2312"); %>
<HTML>
<Head>
<Title> request object_example 1 </title>
</Head>
<Body bgcolor = "# fffff0">
<Form action = "" method = "Post">
<Input type = "text" name = "qwe">
<Input type = "Submit" value = "Submit">
</Form>
Request Method: <% = request. getmethod () %> <br>
Requested Resource: <% = request. getrequesturi () %> <br>
Request Protocol: <% = request. getprotocol () %> <br>
Request file name: <% = request. getservletpath () %> <br>
IP address of the requested server: <% = request. getservername () %> <br>
Request server port: <% = request. getserverport () %> <br>
Client IP address: <% = request. getremoteaddr () %> <br>
Client Host Name: <% = request. getremotehost () %> <br>
Value for form submission: <% = request. getparameter ("qwe") %> <br>
</Body>
</Html>
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% Request. setcharacterencoding ("gb2312"); %>
<% @ Page import = "Java. util. enumeration" %>
<HTML>
<Head>
<Title> request object_example 2 </title>
</Head>
<Body bgcolor = "# fffff0">
<Form action = "" method = "Post">
Username: <input type = "text" name = "username"> & nbsp;
Password: <input type = "text" name = "userpass"> & nbsp;
<Input type = "Submit" value = "enter">
</Form>
<%
String STR = "";
If (request. getparameter ("username ")! = NULL & request. getparameter ("userpass ")! = NULL ){
Enumeration enumt = request. getparameternames ();
While (enumt. hasmoreelements ()){
STR = enumt. nextelement (). tostring ();
Out. println (STR + ":" + request. getparameter (STR) + "<br> ");
}
}
%>
</Body>
</Html>
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% Request. setcharacterencoding ("gb2312"); %>
<HTML>
<Head>
<Title> request object_example 3 </title>
</Head>
<Body bgcolor = "# fffff0">
<Form action = "" method = "Post">
Good at: <input type = "checkbox" name = "CB" value = "on1"> VC ++ & nbsp;
<Input type = "checkbox" name = "CB" value = "on2"> JAVA & nbsp;
<Input type = "checkbox" name = "CB" value = "on3"> Delphi & nbsp;
<Input type = "checkbox" name = "CB" value = "on4"> VB & nbsp;
<Br>
<Input type = "Submit" value = "enter" name = "qwe">
</Form>
<%
If (request. getparameter ("qwe ")! = NULL ){
For (INT I = 0; I <request. getparametervalues ("CB"). length; I ++ ){
Out. println ("CB" + I + ":" + request. getparametervalues ("CB") [I] + "<br> ");
}
Out. println (request. getparameter ("qwe "));
}
%>
</Body>
</Html>
2. Response object
The response object contains information about the response to the customer request, but it is rarely used directly in JSP. It is an instance of the httpservletresponse class.
Serial number method description
1 string getcharacterencoding () indicates the character encoding used in the response.
2 servletoutputstream getoutputstream () returns a binary output stream of the response
3 printwriter getwriter () returns an object that can output characters to the client.
4 void setcontentlength (INT Len) set the Response Header Length
5 void setcontenttype (string type) set the MIME type of the response
6 sendredirect (Java. Lang. string location): redirect client requests
7
8
3. Session Object
A session object refers to a session between the client and the server, starting from a webapplication that the client connects to the server until the client and the server are disconnected. It is an instance of the httpsession class.
Serial number method description
1 long getcreationtime () returns the session Creation Time
2 Public String GETID () returns the unique ID number set by the JSP Engine during session creation.
3 long getlastaccessedtime () returns the last client request time in this session
4 int getmaxinactiveinterval () returns the interval between two requests this session is canceled (MS)
5 string [] getvaluenames () returns an array containing all the available attributes of this session.
6 void invalidate () cancels the session and makes the session unavailable
7 Boolean isnew () returns a session created by the server, whether the client has been added
8 void removevalue (string name) deletes the attribute specified in the session.
9 void setmaxinactiveinterval () sets the interval between two requests this session is canceled (MS)
10
11
12
13
14
15
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. util. *" %>
<HTML>
<Head> <title> Session object _ Example 1 </title> <Body> <br>
Session Creation Time: <% = session. getcreationtime () %> & nbsp; <% = new date (Session. getcreationtime () %> <br>
Session ID: <% = session. GETID () %> <br>
Last client request time: <% = session. getlastaccessedtime () %> & nbsp; <% = new Java. SQL. time (Session. getlastaccessedtime () %> <br>
Interval between two requests this session is canceled (MS): <% = session. getmaxinactiveinterval () %> <br>
Is it a newly created session: <% = session. isnew ()? "Yes": "no" %> <br>
<%
Session. putvalue ("name", "Linyuan programming ");
Session. putvalue ("nmber", "147369 ");
%>
<%
For (INT I = 0; I <session. getvaluenames (). length; I ++)
Out. println (session. getvaluenames () [I] + "=" + session. getvalue (session. getvaluenames () [I]);
%>
<! -- Returns the number of milliseconds from 0:00:00, January 1, January 01, 1970, Greenwich Mean Time (GMT). -->
</Body>
</Html>
4. Out object
An out object is an instance of the jspwriter class and is a common object for outputting content to the client.
Serial number method description
1 void clear () clears the buffer content
2 void clearbuffer () clears the current content of the buffer
3 void flush () clears the stream
4 int getbuffersize () returns the buffer size in bytes. If no buffer is set, the value is 0.
5 Int getremaining () returns how much available the buffer is.
6 Boolean isautoflush () indicates whether to automatically clear or throw an exception when the returned buffer is full.
7 void close () Close the output stream
8
9
10
11
12
13
14
15
<% @ Page contenttype = "text/html; charset = gb2312" %>
<HTML> <% @ Page buffer = "1kb" %>
<Body>
<%
For (INT I = 0; I <2000; I ++)
Out. println (I + "{" + out. getremaining () + "}");
%> <Br>
Cache size: <% = out. getbuffersize () %> <br>
Remaining cache size: <% = out. getremaining () %> <br>
Automatic Refresh: <% = out. isautoflush () %> <br>
<% -- Out. clearbuffer (); -- %>
<% -- Out. Clear (); -- %>
<! -- By default, the content that the server will output to the client is written to an output buffer instead of the client. the buffer content is output to the client only in the following three cases:
1. the JSP page has completed the output of information
2. The output buffer is full.
3. jsp calls out. Flush () or response. flushbuffer ()
-->
</Body>
</Html>
5. 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.
Serial number method description
1 class getclass returns the class of 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 the specified object.
5. Clone () this object.
6 string tostring () converts the object to a string object.
7 void y () Wake up a waiting thread
8 void yyall () Wake up all waiting threads
9 void wait (INT timeout) causes a thread to wait until the timeout ends or is awakened
10 void wait () enables a thread to wait until it is awakened
11 void entermonitor () locks the object
12 Void exitmonitor () locks the object
13
14
15
6. Application Object
The Application Object shares data between users and stores global variables. It starts from the start of the server until the server is closed. During this period, this object will always exist. In this way, the object can be connected between users or between users, you can perform operations on the same attribute of this object. Operations on this object attribute anywhere will affect access by other users. The startup and shutdown of the server determine the life of the Application object. It is an instance of the servletcontext class.
Serial number method description
1 object getattribute (string name) returns the attribute value to the name
2 enumeration getattributenames () returns the enumeration of all available attribute names
3 void setattribute (string name, object OBJ)
4 void removeattribute (string name) delete a property and its attribute values
5 string getserverinfo () returns the JSP (servlet) engine name and version number.
6 string getrealpath (string path) returns the actual path of a virtual path.
7 servletcontext getcontext (string uripath) returns the Application Object of the specified webapplication.
8 int getmajorversion () returns the maximum servlet API version supported by the server.
9 int getminorversion () returns the maximum servlet API version supported by the server.
10 string getmimetype (string file) returns the MIME type of the specified file
11 URL getresource (string path) returns the URL path of the specified resource (file and directory)
12 inputstream getresourceasstream (string path) returns the input stream of the specified resource
13 requestdispatcher getrequestdispatcher (string uripath) returns the requestdispatcher object of the specified resource
14 servlet getservlet (string name) returns the servlet with the specified name
15 enumeration getservlets () returns the enumeration of all servlets
16 enumeration getservletnames () returns the enumeration of all servlet names
17 void log (string MSG) writes the specified message to the servlet Log File
18 void log (exception, string MSG) writes the stack track and error message of the specified exception to the servlet Log File
19 void log (string MSG, throwable) writes stack tracing and throwable exception descriptions to the servlet log file.
20
<% @ Page contenttype = "text/html; charset = gb2312" %>
<HTML>
<Head> <title> application object _ Example 1 </title> <Body> <br>
JSP (servlet) engine name and version: <% = application. getserverinfo () %> <br>
Return the true path of the/application1.jsp virtual path: <% = application. getrealpath ("/application1.jsp") %> <br>
Server-supported servlet API major version: <% = application. getmajorversion () %> <br>
Minor version of servlet API supported by the server: <% = application. getminorversion () %> <br>
URL path of the specified resource (file and directory): <% = application. getresource ("/application1.jsp") %> <br> <! -- Replace application1.jsp with a directory -->
<Br>
<%
Application. setattribute ("name", "Linyuan Computer Programming Technology Training School ");
Out. println (application. getattribute ("name "));
Application. removeattribute ("name ");
Out. println (application. getattribute ("name "));
%>
</Body>
</Html>
<% @ Page contenttype = "text/html; charset = gb2312" %>
<HTML>
<Head> <title> application object _ Example 2 </title> <Body> <br>
<! -- Because the application always exists on the server side, you can use this feature to count webpages -->
<%
If (application. getattribute ("count") = NULL)
Application. setattribute ("count", "1 ");
Else
Application. setattribute ("count", integer. tostring (integer. valueof (application. getattribute ("count"). tostring (). intvalue () + 1 ));
%>
You are the <% = application. getattribute ("count") %> visitor
</Body>
<! -- Because the getattribute () method returns an object type object, use the getstring () method to convert it to the string type -->
<! -- Use the valueof () method of the integer class to convert the string to an integer object. Use the intvalue () method to get the int type, add 1, and finally use integer to calculate the result. the tostring () method is converted to the string type required by the setattribute () method -->
</Html>
<% @ Page contenttype = "text/html; charset = gb2312" %>
<HTML>
<Head> <title> application object _ Example 3 </title> <Body> <br>
<! -- Because the application always exists on the server side, you can use this feature to count webpages -->
<%
String STR = application. getattribute ("count"). tostring (); // getattribute ("count") returns the object type
Int I = 0;
If (STR = NULL)
Application. setattribute ("count", "1 ");
Else
I = integer. parseint (STR); // out. println (I );
Application. setattribute ("count", ++ I + "");
%>
You are the <% = application. getattribute ("count") %> visitor
</Body>
</Html>
7. Exception object
An exception object is an exception object. When an exception occurs during a page running, this object is generated. To apply this object to a JSP page, you must set iserrorpage to true; otherwise, compilation fails. It is actually a java. Lang. throwable object.
Serial number method description
1 string getmessage () returns a message describing an 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 track of an exception
5
8. pagecontext object
The pagecontext object provides access to all the objects and namespaces on the JSP page, that is, it can access the session on this page, or take a property value of the application on this page, it is equivalent to the aggregator of all functions on the page. Its Class Name is also called pagecontext.
Serial number method description
1 jspwriter getout () returns 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 exception getexception () returns the exception object (exception) of the current page)
7 servletconfig getservletconfig () returns the servletconfig object (config) of the current page)
8 servletcontext getservletcontext () returns the servletcontext object (Application) of the current page)
9 void setattribute (string name, object attribute) set attributes and attribute values
10 void setattribute (string name, object OBJ, int scope) set attributes and attribute values within the specified range
11 public object getattribute (string name): Get the attribute value
12 object getattribute (string name, int scope) takes the attribute value within the specified range
13. Search for a public object findattribute (string name) Attribute and return the starting attribute value or null.
14 void removeattribute (string name) delete an attribute
15 void removeattribute (string name, int scope) deletes an attribute within a specified range.
16 int getattributescope (string name) returns the range of an attribute.
17 enumeration getattributenamesinscope (INT scope) return available attribute name enumeration within the specified range
18 void release () releases resources occupied by pagecontext
19 void forward (string relativeurlpath) causes the current page to be re-exported to another page
20 void include (string relativeurlpath) contains another file at the current position
21
<% @ Page contenttype = "text/html; charset = gb2312" %>
<HTML> <Body> <br>
<%
Request. setattribute ("name", "Linyuan programming ");
Session. setattribute ("name", "Linyuan Computer Programming Technology Training ");
// Session. putvalue ("name", "computer programming ");
Application. setattribute ("name", "training ");
%>
Value set for request: <% = pagecontext. getrequest (). getattribute ("name") %> <br>
Session value: <% = pagecontext. getsession (). getattribute ("name") %> <br>
Value set by application: <% = pagecontext. getservletcontext (). getattribute ("name") %> <br>
Value in range 1: <% = pagecontext. getattribute ("name", 1) %> <br>
Value in range 2: <% = pagecontext. getattribute ("name", 2) %> <br>
Value in the range of 3: <% = pagecontext. getattribute ("name", 3) %> <br>
Value in range 4: <% = pagecontext. getattribute ("name", 4) %> <br>
<! -- Start with the smallest range page, followed by reques, session, and application -->
<% Pagecontext. removeattribute ("name", 3); %>
The modified value of pagecontext: <% = session. getvalue ("name") %> <br>
<% Pagecontext. setattribute ("name", "Application Technology Training", 4); %>
The value set by the application after pagecontext is modified: <% = pagecontext. getservletcontext (). getattribute ("name") %> <br>
Value search: <% = pagecontext. findattribute ("name") %> <br>
Attribute name range: <% = pagecontext. getattributesscope ("name") %> <br>
</Body>
9. 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 (consisting of attribute names and attribute values) and server-related information (by passing a servletcontext object)
Serial number method description
1 servletcontext getservletcontext () returns the servletcontext object containing server-related information
2 string getinitparameter (string name) returns the value of the initialization parameter.
3 enumeration getinitparameternames () returns the enumeration of all parameters required for servlet initialization.
4
5