1. 1xx(信息性状态码):
- 100 Continue
- 101 Switching Protocols
- 102 Processing
2. 2xx(成功状态码):
- 200 OK
- 201 Created
- 202 Accepted
- 204 No Content
- 206 Partial Content
3. 3xx(重定向状态码):
- 300 Multiple Choices
- 301 Moved Permanently
- 302 Found (常用于临时重定向)
- 303 See Other
- 304 Not Modified
- 307 Temporary Redirect
4. 4xx(客户端错误状态码):
- 400 Bad Request
- 401 Unauthorized
- 403 Forbidden
- 404 Not Found
- 405 Method Not Allowed
- 408 Request Timeout
- 409 Conflict
- 410 Gone
- 413 Payload Too Large
- 414 URI Too Long
- 415 Unsupported Media Type
- 429 Too Many Requests
5. 5xx(服务器错误状态码):
- 500 Internal Server Error
- 501 Not Implemented
- 502 Bad Gateway
- 503 Service Unavailable
- 504 Gateway Timeout
- 505 HTTP Version Not Supported
在 Servlet 中,你可以使用 response.setStatus(int sc) 方法设置 HTTP 状态码。例如:
response.setStatus(HttpServletResponse.SC_OK); // 设置状态码为 200 OK
你还可以使用 response.sendError(int sc, String msg) 方法设置状态码并发送错误消息。例如:
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Resource not found");
在这个例子中,SC_NOT_FOUND 表示 404 Not Found 状态码,并且会显示 "Resource not found" 错误消息。
根据业务逻辑和需要,你可以选择合适的状态码来反映请求的处理结果。
转载请注明出处:http://www.zyzy.cn/article/detail/13642/Servlet