JSP details -- response object

Source: Internet
Author: User

JSP details -- response object
Response object

The response object is used to respond to client requests and output information to the customer. It encapsulates the responses generated by JSP and sends them to the client to respond to client requests.

1. Redirect webpage

You can use the sendRedirect () method of the response object to redirect the webpage to another page. Redirection supports redirecting addresses to different hosts, which is different from forwarding. On the client browser, you will get the address after the jump and resend the request link. You can view the address after the jump from the address bar of the browser. After the redirection operation, all attributes in the request will be invalid and a new request object will be started.

The syntax format of the sendRedirect () method is as follows:

Response. sendRedirect (String path );

Parameter description:

Path: the specified destination address, which can be a relative path or another URL address of different hosts.

Example:

Define index. jsp to redirect to Login. jsp

<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

<% Response. sendRedirect ("Login. jsp"); %>

My JSP 'index. jsp 'starting page

This is my JSP page.

Define Login. jsp

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

My JSP 'login. jsp 'starting page

2. process HTTP header files

You can set an HTTP response Header for a response object. The most common option is to disable caching and set automatic page refresh and timed page Jump.

Disable Cache

By default, the browser caches the content of the webpage displayed. you can disable the cache through the response object.

Example:

Modify Login. jsp

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

<%

Response. setHeader ("Catch-control", "no-store ");

Response. setDateHeader ("Expires", 0 );

%>

My JSP 'login. jsp 'starting page

Set timed page Jump

Example:

Modify index. jsp

<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

<%

Response. setHeader ("refresh", "5; URL = hello. jsp ");

%>

My JSP 'index. jsp 'starting page

This is my JSP page.

Set timed page refresh

<%

Response. setHeader ("refresh", "5 ");

%>

Set output buffer

Normally, the content that the server outputs to the client is not directly written to the client, but to an output buffer first. When one of the following three conditions is met, the buffer content will be written to the client.

A. The output of the JSP page has all been written to the buffer zone.

B. The buffer is full.

C. Call the flushBuffer () method of the response object or the flush () method of the out object on the JSP page.

The response object has the following methods for configuring the buffer:

FlushBuffer (): forces the content in the buffer zone to be output to the client.

GetBufferSize (): Get the actual size of the buffer used. If there is no buffer, 0 is returned.

Reset (): clears the content of the buffer while clearing the status code and header.

IsCommited (): checks whether the server has written data to the client.

Example:

Set the buffer size to 20 kb.

Response. setBufferSize ();

PS: If the buffer size is set to 0, the buffer is not buffered.



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.