A good memory is not as bad as a 11-web application HttpServletResponse

Source: Internet
Author: User

HttpServletResponse is a very common and important object in Java-developed Web applications, and HttpServletResponse often appears in pairs with HttpServletRequest.

1 , HttpServletResponse objects

Web The server receives an HTTP request that creates a httpservletrequest and HttpServletResponse object for each request. Send data to the client to find HttpServletResponse, obtain data from the client with HttpServletRequest;

HttpServletResponse objects can send three types of data to the client:

A. response header B. Status code C. Data

Getoutputstream and Getwriter methods are used to get the output binary data, output text data servletouputstream, PrintWriter object.

Getoutputstream and Getwriter The two methods are mutually exclusive, after invoking any one of these methods, you can no longer invoke another method.

The data written to the Servletoutputstream or PrintWriter object by the servlet program is fetched by the servlet engine from the response, and the servlet engine treats the data as the body of the response message. It is then combined with the response status line and each response header to output to the client.

Serlvet After the service method finishes, the Servlet engine checks whether the output stream object returned by the Getwriter or Getoutputstream method has already called the Close method, and if not, The servlet engine calls the Close method to close the output stream object.

2 , set HttpServletResponse response header Control Browser

2.1 , set HTTP response header control browser prevents caching of current document content

Response.setdateheader ("Expries",-1);

Response.setheader ("Cache-control", "No-cache");

Response.setheader ("Pragma", "No-cache");

2.2 , set HTTP response header control browser to refresh Web page periodically (refresh)

response.setheader ("Refresh", "*");// Set Refresh response Header Control browser refreshes every 15 seconds

2.3 , to implement request redirection through HttpServletResponse

request redirection means that when a Web resource receives a client request, it notifies the client to access another Web resource, which is called a request redirection.

Application Scenario: User login, the user first access to the login page, login success, will jump to a page, this process is a request redirection process.

There are two ways of implementing this:

First Kind

Response.sendredirect (String location) that calls the Sendredirect method of the response object to implement the request redirection

Sendredirect Internal implementation principle: Using response to set 302 status code and set location response header to achieve redirection;

The second type:

using response to set the 302 status code and set the location response header to implement Redirect request redirection

Response.setheader ("Location", "/change.jsp");

Response.setstatus (Httpservletresponse.sc_found);// Set 302 status code, equivalent to Response.setstatus (302);

2.4 , the difference between reponse request redirection and request forwarding

Request There are similar functions, mainly to do the request forwarding.

Redirection is usually done after some data processing and verification, positioning a new place to continue to work;

Request forwarding is generally the user's request, forwarded to another place, the middle generally do not do processing.

3 , using HttpServletResponse to redirect the source code

Package com.servlet;

Import java.io.IOException;

Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Import Javax.servlet.http.HttpServletRequest;

Importjavax.servlet.http.HttpServletResponse;

/**

 * REDIRECT via HttpServletResponse

* @author Fan Fangming

*/

public class Responsededirect Extendshttpservlet {

Privatestatic final Long serialversionuid = -7412891131635994698l;

Publicvoid doget (httpservletrequest request, httpservletresponse response)

Throws Servletexception,ioexception {

        // calling the Sendredirect method to implement request redirection

Response.sendredirect ("/change.jsp");

}

public void DoPost (HttpServletRequest request, Httpservletresponseresponse)

Throws Servletexception,ioexception {

Doget (request, response);

}

}

A good memory is not as bad as a 11-web application HttpServletResponse

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.