JSP 服务器响应 菜鸟教程,JSP服务器响应

JSP - Server Response

When a Web server responds to a HTTP request to the browser, the response typically consists of a status line, some response headers, a blank line, and the document. A typical response looks like this:

HTTP/1.1200OKContent-Type:text/htmlHeader2:......HeaderN:...(BlankLine)..>

......

The status line consists of the HTTP version (HTTP/1.1 in the example), a status code (200 in the example), and a very short message corresponding to the status code (OK in the example).

Following is a summary of the most useful HTTP 1.1 response headers which go back to the browser from web server side and you would use them very frequently in web programming:

HeaderDescription

AllowThis header specifies the request methods (GET, POST, etc.) that the server supports.

Cache-ControlThis header specifies the circumstances in which the response document can safely be cached. It can have values public, private or no-cache etc. Public means document is cacheable, Private means document is for a single user and can only be stored in private (nonshared) caches and no-cache means document should never be cached.

ConnectionThis header instructs the browser whether to use persistent in HTTP connections or not. A value of close instructs the browser not to use persistent HTTP connections and keep-alive means using persistent connections.

Content-DispositionThis header lets you request that the browser ask the user to save the response to disk in a file of the given name.

Content-EncodingThis header specifies the way in which the page was encoded during transmission.

Content-LanguageThis header signifies the language in which the document is written. For example en, en-us, ru, etc.

Content-LengthThis header indicates the number of bytes in the response. This information is needed only if the browser is using a persistent (keep-alive) HTTP connection.

Content-TypeThis header gives the MIME (Multipurpose Internet Mail Extension) type of the response document.

ExpiresThis header specifies the time at which the content should be considered out-of-date and thus no longer be cached.

Last-Modified此标题指示文档何时上次更改。然后,客户端可以缓存文档,并在稍后的请求中通过If-Modified-Since请求头提供日期。

位置这个标题应该包含在300s中的状态代码的所有响应中。这通知浏览器文档地址。浏览器自动重新连接到此位置并检索新文档。

刷新这个标题指定浏览器应该要求更新页面的时间。您可以指定时间,之后刷新页面的秒数。

重试该标题可以与503(服务不可用)响应一起使用,以便告诉客户端可以多久重复其请求。

Set-Cookie此标头指定与该页面相关联的cookie。

HttpServletResponse对象:

响应对象是javax.servlet.http.HttpServletResponse对象的一个​​实例。就像服务器创建请求对象一样,它也创建一个对象来表示对客户端的响应。

响应对象还定义了处理创建新HTTP头的接口。通过此对象,JSP程序员可以添加新的Cookie或日期戳,HTTP状态代码等。

有以下方法可用于在您的servlet程序中设置HTTP响应头。这些方法可用于表示服务器响应的HttpServletResponse对象。

SN方法和说明

1String encodeRedirectURL(String url)

对指定的URL进行编码,以在sendRedirect方法中使用,或者如果不需要编码,则不会更改URL。

2String encodeURL(String url)

通过在其中包含会话ID来对指定的URL进行编码,或者如果不需要编码,则返回URL。

3boolean containsHeader(String name)

返回一个布尔值,指示是否已经设置了命名的响应头。

4boolean isCommitted()

返回一个布尔值,指示响应是否已提交。

5void addCookie(Cookie 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()

清除响应中底层缓冲区的内容,而不清除标题或状态代码。

12void sendError(int sc)

使用指定的状态代码向客户端发送错误响应并清除缓冲区。

13void sendError(int sc,String msg)

使用指定的状态向客户端发送错误响应。

14void sendRedirect(String location)

使用指定的重定向位置URL向客户端发送临时重定向响应。

15void setBufferSize(int size)

设置响应主体的首选缓冲区大小。

16void setCharacterEncoding(String charset)

设置发送给客户端的响应的字符编码(MIME字符集),例如UTF-8。

17void setContentLength(int len)

设置响应中内容主体的长度在HTTP servlet中,此方法设置HTTP Content-Length头。

18void setContentType(String type)

如果还没有提交响应,请设置要发送给客户端的响应的内容类型。

19void setDateHeader(String name,long date)

设置具有给定名称和日期值的响应头。

20void setHeader(String name,String value)

设置具有给定名称和值的响应头。

21void setIntHeader(String name,int value)

设置具有给定名称和整数值的响应头。

22void setLocale(Locale loc)

如果响应尚未提交,则设置响应的区域设置。

23void setStatus(int sc)

设置此响应的状态代码。

HTTP头响应示例:

以下示例将使用setIntHeader()方法设置刷新头来模拟数字时钟:

Auto Refresh Header Example

Auto Refresh Header Example

");%>

现在把上面的代码放在main.jsp中,尝试访问它。这将在每5秒钟之后显示当前系统时间,如下所示。只需运行JSP并等待查看结果:

Auto Refresh Header Example

Current Time is: 9:44:50 PM

为了使其他方法变得更加舒适,您可以以同样的方式尝试更多上述列出的方法。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值