JSP server response


The Response object mainly transmits the results processed by the JSP container back to the client. You can set the HTTP status and send data to the client through the response variable, such as cookies, HTTP file header information, etc.

A typical response looks like the following:

HTTP/1.1 200 OK
Content-Type: text/html
Header2: ...
...
HeaderN: ...
  (空行)
<!doctype ...>
<html>
<head>...</head>
<body>
...
</body>
</html>

The status line contains HTTP version information, such as HTTP/1.1, a status code, such as 200, and a very short message Corresponds to the status code, such as OK.

The following table summarizes the most useful parts of the HTTP1.1 response headers, which you will often see in network programming:

## Allow             Specify the request methods supported by the server (GET, POST, etc.)                 Cache-Control             Specifies the circumstances under which response documents can be safely cached. Usually the value is               Connection             Instructs the browser whether to use persistent HTTP connections.               Content-Disposition               Let the browser ask the user to store the response on disk under the given name               Content-Encoding               Specify the encoding rules of the page during transmission             Content-Language               The language used to express the document, such as en, en-us,, ru, etc.               Content-Length             Indicates the number of bytes in the response. Only useful if the browser uses a persistent (keep-alive) HTTP connection             Content-Type               Indicates the MIME type used by the document             Expires               Specify when to expire and remove from cache             Last-Modified             Indicates when the document was last modified.The client can cache the document and provide an If-Modified-Sincerequest header
Response headerDescription
public, private or no-cache etc. Public means that the document is cacheable, and Private means that the document is only served to a single user and can only use private cache. No-cache means the document is not cached.
closeValue instructs the browser not to use persistent HTTP connections, while keep-alive means to use persistent connections.
in subsequent requests. Location             Within 300 seconds, including all response addresses with a status code, the browser will automatically reconnect and retrieve new documents
              Refresh               Indicates how often the browser requests updates to the page.
              Retry-After             Used with 503 (Service Unavailable) to tell the user how long it will take for the request to be responded to
              Set-Cookie             Indicate the cookie corresponding to the current page

HttpServletResponse Class

The response object is an instance of the javax.servlet.http.HttpServletResponse class. Just as the server creates the request object, it also creates a client response.

The response object defines the interface for processing the creation of HTTP information headers. By using this object, developers can add new cookies or timestamps, HTTP status codes, and more.

The following table lists the methods used to set HTTP response headers. These methods are provided by the HttpServletResponse class:

#               1String encodeRedirectURL(String url)             2String encodeURL(String url)               3boolean containsHeader(String name)             4boolean isCommitted()             5void addCookie(Cookie cookie)             6void addDateHeader(String name, long date)             7void addHeader(String name, String value)             8void addIntHeader(String name, int value)             9void flushBuffer()             10void reset()               11void resetBuffer()             21             22               23

HTTP response header program example

The following example uses the setIntHeader() method and the setRefreshHeader() method to simulate a digital clock:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<h2>自动刷新实例</h2>
<%
   // 设置每隔5秒自动刷新
   response.setIntHeader("Refresh", 5);
   // 获取当前时间
   Calendar calendar = new GregorianCalendar();
   String am_pm;
   int hour = calendar.get(Calendar.HOUR);
   int minute = calendar.get(Calendar.MINUTE);
   int second = calendar.get(Calendar.SECOND);
   if(calendar.get(Calendar.AM_PM) == 0)
      am_pm = "AM";
   else
      am_pm = "PM";
   String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
   out.println("当前时间: " + CT + "\n");
%>
</body>
</html>

Save the above code as main.jsp, and then browse server to access it. It will display the current system time every 5 seconds.

We can take a look at the following Gif demonstration:

jsp1.gif

You can also modify the above code yourself and try other methods to get deeper results. experience.

##S.N.Method & Description

Encode the URL used by the sendRedirect() method

Encode the URL and return the URL containing the Session ID

Returns whether the specified response header exists

Return whether the response has been submitted to the client

Add the specified cookie to the response

Add the response header and date value of the specified name

Add the response header and value of the specified name

Add the response header and int value of the specified name

Write any cached content to the client

Clear any data in any cache, including status codes and various response headers

Clear basic cache data, excluding response headers and status codes

              12void sendError(int sc)


Send an error response to the client with the specified status code, then clear the cache
              13void sendError(int sc, String msg)


Send an error response to the client using the specified status code and message
              14void sendRedirect(String location)


Send a temporary indirect response to the client using the specified URL
            15void setBufferSize(int size)


Set the cache size of the response body
            16void setCharacterEncoding(String charset)


Specify the response encoding set (MIME character set), such as UTF-8
              17void setContentLength(int len)


Specify the length of the response content in HTTP servlets. This method is used to set the HTTP Content-Length header
                18void setContentType(String type)


Set the content type of the response, if the response has not yet been submitted
              19void setDateHeader(String name, long date)


Set the name and content of the response header using the specified name and value
            20void setHeader(String name, String value)


## Set the name and content of the response header using the specified name and value

void setIntHeader(String name, int value)


Set the name and content of the response header using the specified name and value

void setLocale(Locale loc)


Set the locale of the response, if the response has not been submitted

void setStatus(int sc)


Set the response status code