What are the built-in objects for a JSP

Source: Internet
Author: User
Tags tostring port number

1.request Object





This object encapsulates the information submitted by the user, and can obtain information submitted by the user by invoking the response of the object.





when the request object gets the characters submitted by the user, it will produce garbled text, which can be solved by the following methods:





Sting s2 = new String (s1.getbytes ("iso8859-1"), "GB2312") for conversion.





request commonly used methods:











1.request.getparameter ("name"); Get information for form submission





2.request.getprotocol (); Get the protocol used by the client





3.request.getservletpath (); Page
to get customer submission information




4.request.getmethod (); How to obtain customer submission information





5.request.getheader (); Gets the value of the property in the HTTP message header, such as: Accept, accept-encoding, host





6.request.getremoteaddr (); Get the customer's IP address





7.request.getremotehost (); Gets the name of the client





8.request.getservername (); Gets the name of the server





9.request.getserverport (); Gets the port number of the server





10.request.getparameternames (); Gets the name of all parameters submitted by the client




How to use
response objects





responds dynamically to customer requests and sends data to the client





1. Dynamic Response ContentType Properties





when a user accesses a JSP page, the JSP engine responds to this property value if the page is text/html with the page instruction setting the ContentType property.  If you want to dynamically change this property value to respond to the customer, you need to use the Response object's setContentType (String s) method to change the ContentType property value.


format: Response.setcontenttype (String s);


parameter S is preferable Text/html,application/x-msexcel,application/msword etc.


2.Response redirect


in some cases, when responding to a customer, you need to reboot the customer to another page, and you can use the response Sendredirect (URL) method to achieve customer redirection. For example:


response.sendredirect ("index.jsp");





3.session Object





what is a Session object?





it automatically creates when the first JSP page is loaded, completes session management, opens the browser from one customer and connects to the server, closes the browser and leaves the server for





, known as a session, when a client accesses a server, may want to switch between several pages on the server, the server to know that this is a customer, this requires





Session object.





when a customer first accesses a JSP page on a server, the JSP engine produces a session object and assigns a string ID number, which the JSP engine sends to the client at the same time, stored in a cookie, so that the session object, The client's session object is canceled on the server side until the client closes the browser, and the conversation relationship with the customer disappears. When the client reopened the browser and then connected to the server, the server creates a new session object for the client.




Use of the
Session object:





public String getId (): Gets the Session object number.


public void setattribute (String key,object obj): Adds the object obj specified by the parameter object to the session object and assigns an index keyword to the added object.


public Object GetAttribute (String key): Gets the object that contains the keyword in the session object.


public Boolean isnew (): Determine if it is a new customer.








4.Application Object




This Application object is generated when the
server is started, and the Application object is the same until the server shuts down when browsing through the pages of the Web site that the customer accesses.  However, when different from the session object, all customers have the same application object, that is, all customers share the built-in Application object.

Common methods of
application objects


setattribute (String key,object obj): Adds the object obj specified by the parameter object to the Application object and assigns an index keyword to the object that is added.


GetAttribute (String key): Gets the object that contains the keyword in the Application object.











5.out Object





an Out object is an output stream that is used to output data to the client. The Out object is used for the output of various data.  The usual methods are as follows.


out.print (): Output various types of data.


out.newline (): Output a newline character.


Out.close (): Closes the stream.








6.pageContext Object




The properties of the
Web page are set here.





Page Context Object

The
JSP introduces a fame PageContext class that can access many of the page's properties.


PageContext class has getrequest,getresponse,getout,getsession and other methods.

The
PageContext variable stores the value of the PageContext object that is associated with the current page.


Supplement:


if the method needs to access multiple page-related objects,


passing PageContext is easier than passing independent references such as Request,response,out. (although both approaches can achieve the same goal)





7.page Object





generally we use page directives instead of using this object.





8.config Object





9.exception objects: Used to handle exceptions





Here for this exception object to give an example;


I have two pages page1.jsp and page2.jsp


page1.jsp Code


<%@ page contenttype= "text/html; CHARSET=GBK "errorpage=" page2.jsp "%>


<html>


<head>


<title>


Page1


</title>


</head>


<body >


<h1>


<%= (3/0)%>


</body>


</html>


Obviously this page is an anomaly is the place I marked with green, then I write in red is to say that as long as the page has an exception to automatically jump to the page2.jsp.


now this page2.jsp is just page1.jsp error when the jump over, so my page2.jsp must be designated as the wrong page, that is, the red place,


so I can write <%=exception.tostring ()%&gt on the page, and he displays the exception information on the page1.jsp,


page2.jsp Code


<%@ page contenttype= "text/html; CHARSET=GBK "iserrorpage=" true "%>


<html>


<head>


</head>


<body bgcolor= "#ffffff" >


<h1> program has been wrong </h1>


<%=exception.tostring ()%>


</body>


</html>


Source: Learning Network (www.xue5.com), original address: http://www.xue5.com/itedu/200802/99006_2.html











This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/lookthatgirl/archive/2009/12/29/5102057.aspx

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.