In JSP, nine objects are implicitly defined, also called built-in objects.
Page: The current object. pagecontext: the context object of the page, which records the information of the current page. Request: This object encapsulates the information submitted by the user. Response: send data to the client in a dynamic response to the customer's request. Session: Session Object records information within the session range. Application: Application Object records information within the application range; out: output stream object, used to output information to the browser; config: configuration object, record configuration information on the current page; Exception: exception object, record error information on the page where an exception occurs.
Pagecontext object
Set and obtain attributes: void setattribute () object getattribute () void removeattribute () object findattribute () int getattributescope () Enumeration getattributenamesinscope () page forwarding and includes: void forward () <% pagecontext. forward ("Top. JSP "); %> void include () <% pagecontext. include ("Top. JSP ", true); %>
Request object
1. Set and obtain attributes: <% request. setattribute ("username", "David"); out. println (request. getattribute ("username"); Request. removeattribute ("username"); enumeration temp = request. getattributenames (); While (temp. hasmoreelements () out. println (string) temp. nextelement () + "<br>"); %> 2. Set character set encoding: void setcharacterencoding (string arg0) 3. Obtain request parameters: <% string name = request. getparameter ("username"); string Hober [] = request. getpa Rametervalues ("holobby"); enumeration temp = request. getparameternames (); While (temp. hasmoreelements () out. println (string) temp. nextelement () + "<br>"); map = request. getparametermap (); iterator it = map. keyset (). iterator (); While (it. hasnext () out. println (string) it. next (); <% -- this method is used to obtain the input stream object. You can use the input stream object to upload a file -- %> servletinputstream getinputstream (); %> 4. Get the request row information: assume that the request is http: // localhost: 8888/test/hello. JSP? Name = the HTTP request line corresponding to David: Get/test/Hello. jsp? Name = David HTTP/1.1 <% Out. println (request. getmethod (); getout. println (request. getrequesturi ();/test/hello. jspout. println (request. getquerystring (); name = davidout. println (request. getprotocol (); http/1.1out.println (request. getcontextpath ();/testout. println (request. getpathinfo (); nullout. println (request. getpathtranslated (); nullout. println (request. getservletpath (); Hello. JSP %> 5. GET request header information: <% Out. println (request. getheader ("Accept-language"); out. println (request. getintheader ("Content-Length"); enumeration EE = request. getheaders ("Accept-encoding"); While (EE. hasmoreelements () out. println (string) EE. nextelement () + "<br>"); enumeration EE = request. getheadernames (); While (EE. hasmoreelements () out. println (string) EE. nextelement () + "<br>"); %> int getdateheader (string name); string getcontenttype (); int getcontentlength (); cookie [] getcookies (); 6. Obtain the network information: <% Out. println (request. getremoteaddr (); %> string getremotehost (); int getremoteport (); string getlocaladdr (); string getlocalname (); int getlocalport (); string getservername (); int getserverport (); string getscheme (); string getrequesturl (); 7. Obtain the session object: httpsession getsession (Boolean arg0); httpsession getsession (); 8. Obtain the Request dispatch object: requestdispatcher getrequestdispatcher (string arg0 );
Response object
1. Set the status line <% response. setstatus (response. SC _not_found); response. senderror (response. SC _not_found, "this is a test"); %> 2. Set the Response Header Information <% response. setheader ("cache-control", "No-Cache"); response. addheader ("cache-control", "No-Cache"); response. addheader ("cache-control", "No-Cache"); response. setcontenttype ("text/html; charset = GBK"); response. setcharacterencoding ("GBK"); response. setlocal (New Java. util. local ("ZH", "cn "); %> Void setintheader (string name, int value); void addintheader (string name, int value); void setdateheadder (string name, long value ); void adddateheadder (string name, long value); void setcontenttype (string S); void setcontentlength (INT length); setcontenttype (), setcharacterencoding () and setlocal () you can set the character encoding of the output stream. The difference is: setcharacterencoding () can only be used to set the encoding used by the out output stream, but it has the highest priority and can overwrite the settings in the last two methods; setcontenttype () can be set to out The encoding method of the output stream character. You can also set the encoding method used by the browser to decode the received character. The priority is centered. setlocal () it can only be used to set the encoding used by the out output stream. The priority is the lowest and will be overwritten by the first two settings. 3. output the corresponding body servletoutputstream getoutputstream (); printwriter getwriter (); void setbuffersize (INT size); int getbuffersize (); void flushbuffer (); void reset () 4. Redirection <% response. sendredirect ("/JSP/Other. JSP "); %>
Session Object
1. Set attributes and obtain void setattribute (string name, object value); object getattribute (string name); void removeattribute (string name); enumeration getattributenames (); 2. session status related operations string GETID (); long getcreationtime (); void setmaxinactiveinterval (INT interval); int getmaxinactiveinterval (); Boolean isnew (); void invalidate (); long getlastaccessedtime ();
Application Object
1. Set attributes and obtain void setattribute (string name, object value); object getattribute (string name); void removeattribute (string name); enumeration getattributenames (); 2. web program initialization parameter settings <% -- web. XML parameter configuration -- %> <% Out. println (application. getinitparameter ("charset"); enumeration temp = application. getiniparameternames (); While (temp. hasmoreelements () out. println (string) temp. nextelement () + "<br>"); %> 3. Access the resource file (1). A resource object is returned. All subdirectories and file path names, as shown in <% set = application. getresourcepaths ("/WEB-INF"); iterator it = set. iterator (); While (it. hasnext () out. println (string) it. next (); %> (2) return a URL corresponding to the specified resource path. The path parameter starts with "/", for example, <% java.net. URL url = application. getresource ("/Other. JSP "); %> (3) returns the input stream connected to a resource, such as <% inpurstream is = application. getresourceasstream ("upload. JSP "); servletoutputstream OS = response. getoutputstream (); int Len = is. re AD (); While (Len! =-1) {OS. write (LEN); Len = is. read ();} is. close (); OS. close (); %> the above Code transfers the file content through application. getresourceasstream ("upload. JSP ") obtain the resource file upload in the web path. JSP input stream, and then written to the response output stream. (4) return the local file system path mapped to a virtual path, for example, <% servletinputstream SIS = request. getinputstream (); string filepath = application. getrealpath ("/upload.txt"); fileoutputstream Fos = new fileoutputstream (filepath); int Len = sis. read (); While (Len! =-1) {FOS. Write (LEN); Len = sis. Read ();} FOS. Close (); %> the upstream code writes the internal content of the request information to upload.txt under webbench.
Out object
Out is an instance object of the javax. servlet. jsp. jspwriter class. It is used to write data to the client. Void clear (); void clearbuffer (); void close (); int getbuffersize (); int getremainning (); Boolean isautoflush (); void newline (); void print (); void println ();
Config object
The Web. xml configuration information is similar to the "web program initialization parameter settings" of the Application object. You can change the application in the code to config. <% Out. println (config. getinitparameter ("charset"); enumeration temp = config. getiniparameternames (); While (temp. hasmoreelements () out. println (string) temp. nextelement () + "<br>"); %>
Exception object
Get exception information: String getmessage (); Output exception information and tracing to standard error stream: void printstacktrace ();