在本章中,我们将在jsp中讨论http状态码。http请求和http响应消息的格式相似,并具有以下结构 -
- 初始状态行+ crlf(回车+换行+新行)
- 零个或多个标头行+ crlf
- 空白行,即 crlf
- 一个可选的消息正文,如文件,查询数据或查询输出。
例如,服务器响应标头如下所示:
http/1.1 200 ok
content-type: text/html
header2: ...
...
headern: ...
(blank line)
<!doctype ...>
<html>
<head>...</head>
<body>
...
</body>
</html> 状态行由http版本(示例中为http/1.1),状态码(示例中为200)和与状态代码相对应的非常短的消息(示例中为“ok”)组成。
下表列出了所有从web服务器返回http状态代码和关联的消息 -
| 100 | continue | 服务器只收到请求的一部分,但只要尚未被拒绝,客户端应继续请求 |
| 101 | switching protocols | 服务器切换协议。 |
| 200 | ok | 请求成功 |
| 201 | created | 请求完成,并创建一个新的资源 |
| 202 | accepted | 请求被接受处理,但处理不完整。 |
| 203 | non-authoritative information | |
| 204 | no content | |
| 205 | reset content | |
| 206 | partial content | |
| 300 | multiple choices | 链接列表; 用户可以选择链接并转到该位置。最多有五个地址。 |
| 301 | moved permanently | 所请求的页面已被移动到新的url。 |
| 302 | found | 请求的页面暂时移动到新的url。 |
| 303 | see other | 请求的页面可以在其它(不同的)url下找到。 |
| 304 | not modified | |
| 305 | use proxy | |
| 306 | unused | 此代码在以前的版本中使用。它不再使用,但代码是保留的。 |
| 307 | temporary redirect | 请求的页面暂时移动到新的url。 |
| 400 | bad request | 服务器不了解的请求。 |
| 401 | unauthorized | 请求的页面需要用户名和密码。 |
| 402 | payment required | 不能使用此代码。 |
| 403 | forbidden | 禁止访问请求的页面 |
| 404 | not found | 服务器找不到请求的页面。 |
| 405 | method not allowed | 请求中指定的方法是不允许的 |
| 406 | not acceptable | 服务器只能生成不被客户端接受的响应。 |
| 407 | proxy authentication required | 可以在提供此请求之前,使用代理服务器进行身份验证。 |
| 408 | request timeout | 请求比服务器准备等待的时间更长,请求超时了。 |
| 409 | conflict | 由于冲突,请求无法完成。 |
| 410 | gone | 请求的页面不再可用。 |
| 411 | length required | “content-length”未定义,服务器不接受请求。 |
| 412 | precondition failed | 请求中给出的前提条件由服务器评估求值结果为false。 |
| 413 | request entity too large | 服务器将不接受请求,因为请求实体太大了。 |
| 414 | request-url too long | 因为网址太长,服务器将不接受该请求。 当将“post”请求转换为具有长查询信息的“get”请求时,可能会发生这种情况。 |
| 415 | unsupported media type | 因为不支持媒体类型,服务器将不接受该请求。 |
| 417 | expectation failed | |
| 500 | internal server error | 请求未完成,服务器遇到意外(错误)情况。 |
| 501 | not implemented | 请求未完成,服务器不支持所需的功能。 |
| 502 | bad gateway | 请求未完成,服务器从上游服务器收到无效响应。 |
| 503 | service unavailable | 请求未完成,服务器暂时超载或关闭。 |
| 504 | gateway timeout | 网关已经超时了 |
| 505 | http version not supported | 服务器不支持“http协议”版本。 |
设置http状态代码的方法
以下方法可用于在servlet程序中设置http状态代码。 这些方法可用于httpservletresponse对象。
| 1 | public void setstatus ( int statuscode ) | 此方法设置任意状态代码。 setstatus()方法将一个int(状态代码)作为参数。如果响应包含特殊的状态代码和文档,请确保在实际返回printwriter的内容之前调用setstatus()方法。 |
| 2 | public void sendredirect(string url) | 此方法将生成302响应以及给出新文档的url的位置标头。 |
| 3 | public void senderror(int code, string message) | 该方法发送状态码(通常为404)以及自动格式化在html文档内并发送给客户端的短消息。 |
http状态代码示例
以下示例显示如何将407错误代码发送到客户端浏览器。之后,浏览器会显示“需要验证!!!”的 信息。
打开eclipse,创建一个动态web项目:httpstatuscode , 其目录结构如下所示 -
文件:index.jsp 的代码如下所示 -
<%@ page language="java" contenttype="text/html; charset=utf-8"
pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>发送http状态码</title>
</head>
<body>
<%
// set error code and reason.
response.senderror(407, "need authentication!!!");
%>
</body>
</html> 部署上面项目,然后打开浏览器访问url: http://localhost:8080/httpstatuscode/index.jsp , 可以看到以下结果 -
为了使用http状态代码更友好,可尝试设置不同的状态代码及其描述说明。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!