Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The JSP server responds


May 12, 2021 JSP


Table of contents


The JSP server responds

The Response response object primarily sends the results of the JSP container processing back to the client. You can set the status of HTTP and send data to the client through the response variable, such as cookies, HTTP file header information, etc.

A typical response looks like this:

HTTP/1.1 200 OK
Content-Type: text/html
Header2: ...
...
HeaderN: ...
  (Blank Line)
<!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 information corresponding to the status code, such as OK.

The following table summarizes the most useful parts of the HTTP 1.1 response headers that you will often see in network programming:

The response header Describe
Allow Specify the server-supported request method (GET, POST, etc.)
Cache-Control Specify where the response document can be securely cached. T ypically, values are public, private, or no-cache, and so on. P ublic means that documents can be cached, Private means that documents are only available to single users and can only use private caches. No-cache means that documents are not cached.
Connection Command whether the browser wants to use a persistent HTTP connection. Close Value commands that the browser does not use a persistent HTTP connection, while keep-alive means a persistent connection.
Content-Disposition Have the browser require the user to store the response on disk under a given name
Content-Encoding Specifies the encoding rules for the page at the time of transmission
Content-Language Express the language used in the document, such as en, en-us, ru, etc
Content-Length The number of bytes that indicate the response. It is only useful if the browser uses a persistent (key-alive) HTTP connection
Content-Type Indicates the type of MIME used by the document
Expires Indicates when it expires and is removed from the cache
Last-Modified Indicates when the document was last modified. The client can cache the document and provide an If-Modified-Since request header in subsequent requests
Location Within 300 seconds, with all the response addresses with a status code, the browser automatically re-connects and retrieves the new document
Refresh Indicates how often the browser requests an update to the page.
Retry-After Use with Service Unavailable to tell the user how long the request will be responded to
Set-Cookie Indicates the cookie for the current page

HttpServletResponse class

The response object is an example of the javax.servlet.http://httpsponse class. Just as the server creates a request object, it also creates a client response.

The response object defines the interface that handles the creation of the HTTP header. By using this object, developers can add new cookies or timestamps, http status codes, and so on.

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

S.N. Method . . .
1 String encodeRedirectURL(String url)

Encode the URL used by the sendRedirect() method

2 String encodeURL(String url)

Encode the URL and pass back the URL that contains the Session ID

3 boolean containsHeader(String name)

Returns the existence of the specified response header

4 boolean isCommitted()

Returns whether the response has been committed to the client

5 void addCookie(Cookie cookie)

Add the specified cookie to the response

6 void addDateHeader(String name, long date)

Add the response header and date value for the specified name

7 void addHeader(String name, String value)

Add the response header and value of the specified name

8 void addIntHeader(String name, int value)

Add the response header and int value with the specified name

9 void flushBuffer()

Write content from any cache to the client

10 void reset()

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

11 void resetBuffer()

Clears basic cached data, excluding response heads and status codes

12 void sendError(int sc)

Use the specified status code to send an error response to the client and then clear the cache

13 void sendError(int sc, String msg)

Use the specified status code and message to send an error response to the client

14 void sendRedirect(String location)

Use the specified URL to send a temporary indirect response to the client

15 void setBufferSize(int size)

Sets the cache size of the response body

16 void setCharacterEncoding(String charset)

Specifies the encoding set of the response (MIME character set), such as UTC-8

17 void setContentLength(int len)

Specify the length of the content in the HTTP servlets response, which is used to set the HTTP Content-Length header

18 void setContentType(String type)

Sets the type of content of the response, if the response has not yet been submitted

19 void setDateHeader(String name, long date)

Use the specified name and value to set the name and content of the response header

20 void setHeader(String name, String value)

Use the specified name and value to set the name and content of the response header

21 void setIntHeader(String name, int value)

Use the specified name and value to set the name and content of the response header

22 void setLocale(Locale loc)

Set the locale for the response, if the response has not yet been submitted

23 void setStatus(int sc)

Set the status code for the response


An example of an HTTP response header

The following example simulates a digital clock using the setIntHeader() method and the setRefreshHeader() method:

<%@ page import="java.io.*,java.util.*" %>
<html>
<head>
<title>Auto Refresh Header Example</title>
</head>
<body>
<center>
<h2>Auto Refresh Header Example</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("Current Time is: " + CT + "\n"); %>
</center>
</body>
</html>

Save the above code as a .jsp and access it through your browser. It will show the current time of the system every 5 seconds.

The results are as follows:

Auto Refresh Header Example
Current Time is: 9:44:50 PM

You can also modify the above code yourself, try to use other methods, you will get a deeper experience.