What are the built-in jsp objects?

Source: Internet
Author: User

1. request object

This object encapsulates the information submitted by the user. by calling the response method of this object, you can obtain the information submitted by the user.

When the request object obtains the Chinese characters submitted by the user, garbled characters are generated, which can be solved by the following methods:

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

Common request methods:



1. request. getParameter ("name"); obtain the information submitted by the form.

2. request. getPRotocol (); obtain the protocol used by the client

3. request. getServletPath (); page for obtaining information submitted by the customer

4. request. getMethod (); method for obtaining information submitted by the customer

5. request. getHeader (); obtain the attribute values in the http message header, such as accept, accept-encoding, and host.

6. request. getRemoteAddr (); get the customer's IP address

7. request. getRemoteHost (); get the client name

8. request. getServerName (); get the server name

9. request. getServerPort (); get the server port number

10. request. getParameterNames (); obtain the names of all parameters submitted by the client.

How to Use the response object

Responds dynamically to customer requests and sends data to the client

1. Dynamic Response contentType attribute

When a user accesses a jsp page, if the page uses the page command to set the page's contentType attribute text/html, the JSP Engine will respond according to this attribute value. If you want to dynamically change the value to respond to the customer, you need to use the setContentType (String s) method of the Response object to change the value of the contentType.
Format: response. setContentType (String s );
The parameters s can be text/html, application/x-msExcel, and application/msWord.
2. Response redirection
In some cases, when responding to a customer, you need to redirect the customer to another page. You can use the sendRedirect (URL) method of Response to redirect the customer. For example:
Response. sendRedirect ("index. jsp ");

3. session Object

What is a session object?

It 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, closing the browser and leaving the server

Is called a session. When a customer accesses a server, it may need to switch between several pages on the server. The server needs to know that this is a customer.

Session object.

When a customer visits a JSP page on the server for the first time, the JSP engine generates a Session object and assigns a String-type idnumber. the JSP engine also sends this idnumber to the client, store the Session object in the Cookie so that the Session object will not be canceled on the server until the client closes the browser and the Session relationship with the client disappears. When the client re-opens the browser and connects to the server, the server creates a new Session object for the client.

Session object usage:

● 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 specifies an index keyword for the added Object.
● Public Object getAttribute (String key): obtains the Object containing the keyword in the Session Object.
● Public Boolean isNew (): determines whether it is a new customer.


4. Application Object

The Application object is generated after the server is started. When the client browses the pages of the website that the client accesses again, the Application object is the same until the server is closed. However, when it is different from the Session object, all clients share the same Application object.
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): obtains the object containing keywords in the Application object.



5. out object

An output stream when an Out object is used to output data to the client. The Out object is used to output various types of data. The common method is as follows.
● Out. print (): outputs various types of data.
● Out. newLine (): outputs a linefeed.
● Out. close (): close the stream.


6. pageContext object

Set the webpage attributes here.

Page Context object
Jsp introduces a class named PageContext, through which many attributes of the page can be accessed.
The PageContext class has methods such as getRequest, getResponse, getOut, and getSession.
The pageContext variable stores the values of the PageContext object 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, and out. (Both methods can achieve the same purpose)

7. page Object

Generally, we use the page command to replace this object.

8. config object

9. exception object: used to handle exceptions

The following is a column for this exception object;
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 abnormal, that is, I marked it in green. The place I wrote in red indicates that as long as there is an exception on this page, it will automatically jump to page2.jsp.
Now this page2.jsp is skipped when a page1.jsp error occurs. Therefore, we must specify page2.jsp as an error page, that is, a red place,
In this way, I can write <% = exception. toString () %> on the page and display the exception information on page1.jsp,
Code of page2.jsp
<% @ Page contentType = "text/html; charset = GBK" isErrorPage = "true" %>
<Html>
<Head>
</Head>
<Body bgcolor = "# ffffff">
<H1> program error <% = Exception. toString () %>
</Body>
</Html>
Source: xuewang (www.xue5.com), original address: http://www.xue5.com/itedu/200802/99006_2.html



This article from the 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.