Latest web development tutorials

JSP server response

Response Response is primarily the result of post-processing JSP container to pass back to the client. You can set HTTP response by state variables and transmitting data, such as Cookie, HTTP header information to the client.

A typical response looks like this:

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

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

The following table summarizes a response header HTTP1.1 the most useful part of the Network Programming You will often see them:

Response header description
Allow Specifies that the server supports request methods (GET, POST, etc.)
Cache-Control Specifies the response document can be cached security situation. The value is usually public, private or no-cache, and so on. Public means that documents can be cached, Private means that the document is only a single user service and only use a private cache. No-cache means that the document will not be cached.
Connection Command browser whether to use persistent HTTP connections. close value command browsers do not use persistent HTTP connections, and keep-alive means using persistent connections.
Content-Disposition Let the browser requires the user to respond to a given name stored on disk
Content-Encoding Transmission coding rules specified page
Content-Language Language description document being used, such as en, en-us ,, ru etc.
Content-Length It indicates the number of bytes of the response. Only in your browser to use persistent helpful of if (keep-alive) HTTP connections
Content-Type MIME type of the document shows that the use of
Expires When are specified expired and removed from the cache
Last-Modified Specified in the document was last modified. Clients can cache documents and provide an If-Modified-Since in subsequent requests request header
Location In 300 seconds, including all have a status code in response to the address, the browser will automatically reconnect and then retrieves the new document
Refresh Indicate how often the browser requests a page update.
Retry-After And 503 (Service Unavailable) used together to tell the user how long the request will get a response
Set-Cookie Corresponding cookie indicates the current page

HttpServletResponse class

javax.servlet.http.HttpServletResponse response object is an instance of the class. Like server creates request objects, it will create a client response.

response object defines the process to create the interface HTTP header information. By using this object, developers can add new cookie or timestamp, as well as HTTP status code, and so on.

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

SN Method & description
1 String encodeRedirectURL (String url)

On sendRedirect () URL method used to encode
2 String encodeURL (String url)

The URL encoding, return URL that contains the Session ID
3 boolean containsHeader (String name)

Returns the specified response header if there
4 boolean isCommitted ()

Returns whether the response has been submitted to the client
5 void addCookie (Cookie cookie)

Adds the specified cookie to the response
6 void addDateHeader (String name, long date )

Add response headers and date specified name
7 void addHeader (String name, String value )

Add response headers and values ​​specified name
8 void addIntHeader (String name, int value )

Add response headers and int value with the specified name
9 void flushBuffer ()

Any contents of the cache written to the client
10 void reset ()

Clear any cache any data, including status codes and response headers various
11 void resetBuffer ()

Clear basic data cache, not including the response headers and status codes
12 void sendError (int sc)

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

Using the specified status code and an error message is sent in response to the client
14 void sendRedirect (String location)

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

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

Specifies a response code set (MIME character set), such as UTF-8
17 void setContentLength (int len)

Specifies HTTP servlets in response to the length of the content, the method used to set the HTTP Content-Length header
18 void setContentType (String type)

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

With the specified name and set the value of the response header name and contents
20 void setHeader (String name, String value )

With the specified name and set the value of the response header name and contents
twenty one void setIntHeader (String name, int value )

With the specified name and set the value of the response header name and contents
twenty two void setLocale (Locale loc)

Locale setting response, if the response has not yet been submitted, then
twenty three void setStatus (int sc)

Set the response status code

HTTP response header Program Example

The following example uses setIntHeader () method and 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>本教程(w3big.com)</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 main.jsp, then access it through a browser. It will display every five seconds at the current time.

We can look at the following Gif presentation charts:

You can also modify the above code it yourself, try to use other methods, we will be able to get a deeper understanding.