<span id="Label3"></p><p><p>Some objects can be used in the Java Patch and Expression section of a JSP page without declaring it, which is the built-in object of the Jsp.<br>JSP common built-in objects have Request,response,session,application,out.<br>of these, two important request and response objects provide control of the server and browser communication methods.<br><br></p></p>1.request Object (request Object)<p><p>Role: encapsulating user-submitted information, obtaining encapsulation information, etc.</p></p>1.1. Get user Submission information<p><p>Request.getparameter (String s);//get Submission Information<br>If the method gets a nullpointerexception exception that is getting an empty object, the exception is handled<br>String S=request.getparameter ("s");<br>If (s==null) {s= "";}</p></p>1.2. Processing Chinese character information<p><p>(1) Re-encode The information<br>Request Re-encodes The information obtained, encodes it with iso-8859-1, stores the encoding in a byte array, and converts the array into a string.<br>String Str=request.getparameter ("s");<br>BYTE B[]=str.getbytes ("iso-8859-1");<br>Str=new String (b);<br>String Str=new string (request.getparameter ("s"). getBytes ("iso-8859-1"), "utf-8");<br>(2) Request Setup Code<br>Request uses the Setcharacterencoding () method to set its own encoding to utf-8 before obtaining information<br>Request.setcharacterencoding ("utf-8");</p></p>1.3 Common methods<p><p>(1) Getprotocol () Gets the communication protocol used by the user to submit information to the server, such as http/1.1<br>(2) Getservletpath () Gets the directory of the JSP paging file requested by the user<br>(3) Getcontextpath () Gets the current Web service directory requested by the user<br>(4) getcontentlength () Gets the length of the entire message submitted by the user<br>(5) GetMethod () How to obtain user-submitted information, such as post or get<br>(6) GetHeader (String S) Gets the value of the header name specified by the parameter S in the HTTP headers file. s desirable value: Accept,accept-language,content-type,accept-encoding,user-agent,host,content-length,connection,cookie and so On.<br>(7) Getheadernames () Gets an enumeration of header names<br>(8) getheaders (String S) gets all the names of the specified headers in the header file that are worth an enumeration<br>(9) getremoteaddr () Gets the IP address of the user<br>(ten) getservertname () Gets the name of the server<br>(one) getremotehost () Gets the name of the User's computer (gets the IP address if it cannot get it)<br>() getserverpost () Gets the port number of the server<br>() Getparmaeternames () Gets an enumeration of the value of the name parameter in the User-submitted Information body section</p></p>1.4 Managing properties within scopes<p><p>When the request is forwarded, some data is taken to the forwarded page Processing. At this point, you can use the SetAttribute () method of the request object to set the data to be accessed within the request Scope. In the request scope, the format of the method used to forward the data is as Follows: Request.setattribute ("key", value);<br>The parameter key is a String-type key Name. The data is retrieved from the forwarded page by this key name, and the parameter value is a key value of type Object that represents the data that needs to be saved in the request Scope. In the Requet scope, the method used to get the forwarded data is in the following format:<br>Object Object=request.getattribute ("name");</p></p>1.5 Getting cookies<p><p>The GetCookies () method in the Request object gets the data in the cookie,<br>Format: cookie[] cookie= request.getcookies ();<br>The method return value is an array of cookies.</p></p>2.response Object (response Object)<p><p>Function: to respond dynamically to a User's request and send data to the client</p></p>2.1 Dynamic Response ContentType Properties<p><p>Use setContentType (String s) to change the properties of ContentType</p></p>2.2 Page Redirection<p><p>In some cases, when responding to a user, the user needs to be rebooted to another page, which can be implemented using the Sendredirect (url url) method.<br>JSP pages can also use the Senderror () method in the response object to indicate an error state that accepts an error and an optional error message. This information is returned to the customer on the content Body.<br>For example: response.senderror (500, "the Request page has errors");<br>The last two methods abort the current request and Response.<br>The following methods are used for redirection in the response object:<br>Senderror (int number): sends an error response to the customer using the specified status Code.<br>Senderror (int number,string msg): sends an error response to the customer using the specified status code and descriptive Message.<br>sendredirect: Specifies the redirect location URL and sends a redirect response to the customer, which can use a relative url.<br><br>Response Status Code<br>1xx: mainly experimental in nature<br>2xx: used to indicate successful request<br>3xx: used to indicate that further action should be taken prior to the request<br>4xx: returns the status code when the browser gives a request that is not satisfied<br>5xx: used to indicate a problem with the server</p></p>2.3 Buffer Output<p><p>Buffering can effectively transfer content between the server and the Customer.<br>The HttpServletResponse object has a buffer configuration enabled to support JspWriter Objects. The response object can set the buffer capacity for the JSP page and detect the state of the Buffer.<br>The following methods are used in the response object for the corresponding buffering:<br>Flushbuffer (): forces the contents of the buffer to be sent to the Customer.<br>GetBufferSize (): returns the actual buffer size used by the response and returns 0 if no buffer is Used.<br>Setbuffersize (int size): Sets the preferred buffer size for the body of the Response.<br>Boolean iscommitted (): Indicates whether the response has been committed and the submitted response has been written to the status Code.<br>Reset (): clears any data that exists in the buffer and clears the status Code.</p></p>3.session Object (session Object)<p><p>HTTP is a stateless protocol in which a user initiates a request to the server and the server returns a response that retains information about the connection at the server end.<br>Role: log information about the connection</p></p>3.1 Creating and getting a client session<p><p>Session.setattribute (String key, Object obj)<br>Adds an object named obj to the session object, and if you add an object with the same keyword, the first object you add is Cleared.<br>Session.getattibue (String Key)<br>Gets the object that the keyword is key. Since any object can be added to the session object, it should be forced to convert to the original type when retrieving the Object.<br>Java.util.Enumeration Getattributenames ()<br>Gets the name of all objects in the session, which returns an object of an enumerated type</p></p>3.2 Remove Delete Session object<p><p>RemoveAttribute () removes the appropriate object removal with the following syntax:<br>void RemoveAttribute (java.lang.String Name)<br>The parameter name is a String-type value that represents the name of the object being Removed.<br>The Invalidate () method deletes all objects that have been saved to the Session. The syntax for the invalidate () method is as Follows:<br>void Invalidate ()</p></p>3.3 Timeout Management<p><p>Session.getlastaccessedtime () Gets the Client's last-accessed server-side save time<br>Session.getmaxinactiveinterval () Gets the client to stop accessing the Server-side save time<br>Session.setmaxinactiveinterval (int Value) Sets the Session's save time on the server side when the client stops</p></p>3.4 Other methods<p><p>Session.getid () Gets the number of the session Object.<br>The creation time of the Session.getcreationtime () Session.<br>Session.getlastaccessedtime () The time of the most recent Request.<br>Session.getmaxinactiveinterval () Two request interval how long this session object is Canceled.<br>Session.isnew () is the newly created session Object.</p></p>4.application Objects (multi-client Shared objects)<p><p>Function: save public data in all applications, after the Web server starts and automatically creates the application object, the Appliaction object persists as long as the server is not shut down, and all users can share the Appliaction object.</p></p>4.1 Common methods<p><p>SetAttribute (String key, Object Obj) adds object obj to the application object specified by object and assigns an index to the added object<br>GetAttribute (String Name) Gets the specified property Value.<br>RemoveAttribute (String Name) Deletes the value of the specified Application.<br>Getattributenames () Gets an enumeration that contains all the available property Names. GetContext (String Uripath) Gets the Application object for the specified resource<br>GetResource (String path) Gets the URL path for the specified resource (file and Directory)<br>getResourceAsStream (String Path) Gets the input stream for the specified resource<br>Getservlet (String Name) returns the specified servlet.<br>Log (String Msg) writes the specified message to the Servlet's log file.<br>Getrealpath (".") Returns the current path of the File.<br>Getrealpath ("/") returns the Server's home Directory.</p></p>5.out Objects (output Objects)<p><p>Function: output the contents of various data types to the Web browser, and manage the output buffers on the application server, the default value of the buffer is 8KB.</p></p>5.1 Common methods<p><p>Print ()/println () outputs various types of Data.<br>Clearbuffer () clears the Buffer's data and writes the data to the Client.<br>Clear () clears the current contents of the buffer, but does not write data to the Client.<br>Flush () The data in the output buffer.<br>NewLine () outputs a line break Symbol.<br>Close () closes the output stream.</p></p>6.page Objects (page objects) 6.1 common methods<p><p>GetClass () gets the class of the page Object.<br>Hashcode () Gets the hash code of the page Object.<br>equals (object Obj) Determines whether the page object is equal to obj in the parameter<br>Copy (object obj) copies this page object into the specified object<br>Clone () clones the current Page object<br>ToString () Converts the Page object to an object of type string</p></p>7.pageContext Objects (page Context Objects)<p><p>Function: obtain any range of parameters, through the PageContext object can get the JSP page out, request, response, session, application and other objects, or can redirect customer requests and so On.</p></p>7.1 Common methods<p><p>SetAttribute (String name, Object attribute): Sets the default page range or named objects within a specific object Range.<br>GetAttribute (String name[,int scope]): Gets the property of the name object, and the optional parameter scope represents a specific range.<br>RemoveAttribute (String name [, int scope]): Deletes an attribute in the specified range.<br>Forward (String relativeurlpath): redirects to a different page.<br>Include (String relativeurlpath): contains another file at the current Location.<br>Release (): frees the resources occupied by the PageContext Object.<br>Getservletcontext (): Gets the ServletContext object for the current Page.<br>GetException (): Gets the exception object for the current Page.</p></p>8.config Objects (configuration Objects)<p><p>Function: Gets the configuration information of the Server. The Config object is encapsulated as an javax.servletconfig interface, and a config object can be obtained by means of the Pagecontext.getservletconfig () method.</p></p>8.1 Common methods<p><p>Getservletcontext () Gets the current servlet context.<br>Getinitparameter (String Name) Gets the value of the specified initial parameter.<br>Getinitparameternames () Gets the value of all initial Parameters.<br>Getservletname () Gets the current servlet name.</p></p>8.2 Getting initialization parameters<p><p>1), Configure the initialization parameters in the Web. XML file:</p></p><pre> <span style="color: #0000ff;"><span style="color: #0000ff;"><?</span></span><span style="color: #ff00ff;"><span style="color: #ff00ff;">XML version= "1.0" encoding= "UTF-8"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">?></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Web-app</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">servlet</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Servlet-name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>Sanyang<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Servlet-name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Jsp-file</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>/index.jsp<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Jsp-file</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Init-param</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #008000;"><span style="color: #008000;"><!--</span></span><span style="color: #008000;"><span style="color: #008000;">Setting Initialization Parameters</span></span><span style="color: #008000;"><span style="color: #008000;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Param-name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>Email<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Param-name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Param-value</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>[email protected]<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Param-value</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Init-param</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">servlet</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">servlet-mapping</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Servlet-name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>Sanyang<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Servlet-name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Url-pattern</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>/index.jsp<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Url-pattern</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">servlet-mapping</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Web-app</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p>2), in the JSP page through the Config object in the Getinitparameter () method to get the parameters initialized in the Web. XML file, The program code is as Follows:</p></p><pre> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">%@ Page</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">ContentType</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "text/html; charset=gb2312"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">%</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Body</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Center</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #000000;"><span style="color: #000000;">E-mail address:</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">%=config.getinitparameter</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">("email")%</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Center</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Body</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre>9.exception Object (exception Object) 9.1 error mechanism<p><p>The exception exception object refers to a problem that the Web application can identify and Handle. In the Java language, with the key of "try/catch" to handle the exception information situation, if there is no catch exception information in the JSP page, then the system will automatically generate the exception object, and transfer this object to the page directive element set in the error page, The corresponding exception object is then processed in the error prompt Page.<br>Exexception objects can be used only on error pages, and there are iserrorpage=true pages in the page instruction Element.</p></p>9.2 Common methods<p><p>GetMessage () Get the current error message<br>Getlocalizedmessage () Exception Errors for localized languages<br>Printstacktrace () output an incorrect and incorrect heap trace in the form of a standard error<br>Fillinstacktrace () overriding the execution stack trajectory of an exception<br>ToString () Simple Information Description of the exception error</p></p>9.3 Setting Error specified page<p><p>first, Create a custom error page "error.jsp", that is, add code to the <%@ page%> identifier at the beginning of the Page: <%@ page iserrorpage= "true"%><br>then, point the other pages to the error page. Add code in <% @page%> identifier: <%@ page errorpage= "error.jsp"%><br><br></p></p><p><p>JSP Learning notes (3)-jsp built-in objects</p></p></span>
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.