Presentation is loading. Please wait.

Presentation is loading. Please wait.

Generating the Server Response: HTTP Status Codes

Similar presentations


Presentation on theme: "Generating the Server Response: HTTP Status Codes"— Presentation transcript:

1 Generating the Server Response: HTTP Status Codes

2 HTTP Request/Response
GET /servlet/SomeName HTTP/1.1 Host: ... Header2: ... ... HeaderN:   (Blank Line) Response HTTP/ OK Content-Type: text/html Header2: ... ... HeaderN: ...   (Blank Line) <!DOCTYPE ...> <HTML> <HEAD>...</HEAD> <BODY> </BODY></HTML>

3 When a web server responds to a request, the response consists of a status line, some response headers, a blank line and the document. The status line consists of the HTTP version (HTTP/1.1 in the preceding example), a status code (an integer; 200 in the example), and a very short message corresponding to the status code (OK in the example).

4 Methods to set HTTP Status Codes
The following methods can be used to set HTTP Status Code in your servlet program. These methods are available with HttpServletResponse object. public void setStatus( int statusCode ) This method sets an arbitrary status code. The setStatus method takes an int (the status code) as an argument. Use a constant for the code, not an explicit int. These constants are defined in HttpServletResponse. Names derived from standard message. E.g., SC_OK, SC_NOT_FOUND, etc.

5 Set status codes before sending any document content to the client.
public void sendRedirect(String url) This method generates a 302 response along with a Location header giving the URL of the new document. The 302 status code directs the browser to connect to a new location. public void sendError(int code, String message) This method sends a status code (usually 404) along with a short message that is automatically formatted inside an HTML document and sent to the client. The 404 status code is used when no document is found on the server.

6 HTTP 1.1 Status Codes There are five general categories of specific status codes available in HTTP 1.1 : 100 – 199 Informational codes Client should respond with some other action 200 – 299 Signify that the request was successful 300 – 399 Used for files that have moved Includes a Location header indicating the new address 400 – 499 Error by the client 500 – 599 Signify an error by the server

7 Common HTTP 1.1 Status Codes (contd.)
100 (Continue) Only a part of the request has been received by the server, but as long as it has not been rejected, the client should continue with the request 200 (OK) Everything is fine; document follows for GET and POST. This is default for servlets. 202 (Accepted) The request is accepted for processing, but the processing is not complete. 204 (No Content) Browser should keep displaying previous document.

8 Common HTTP 1.1 Status Codes (contd.)
301 (Moved Permanently) Requested document permanently moved elsewhere (document URL is indicated in Location header). Browsers go to new location automatically. 302 (Found) Requested document temporarily moved elsewhere (indicated in Location header). Servlets should use sendRedirect, not setStatus, when setting this header.

9 Common HTTP 1.1 Status Codes (contd.)
400 (Bad Request) The server did not understand the request 401 (Unauthorized) Browser tried to access password-protected page without proper Authorization header. 404 (Not Found) No such page. Servlets should use sendError to set this. 405 (Method Not Allowed) The method specified in the request is not allowed. 415 ( Unsupported Media Type) The server will not accept the request, because the media type is not supported.

10 Common HTTP 1.1 Status Codes (contd.)
500 (Internal Server Error) The request was not completed. The server met an unexpected condition 501 (Not Implemented) The request was not completed. The server did not support the functionality required. 503 (Service Unavailable) The request was not completed. The server is temporarily overloading or down. 505 (HTTP Version Not supported) The server does not support the "http protocol" version.

11 A Servlet That Redirects Users to Browser-Specific Pages
public class WrongDestination extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String userAgent = request.getHeader("User-Agent"); if ((userAgent != null) && (userAgent.indexOf("MSIE") != -1)) response.sendRedirect(" } else { response.sendRedirect(" } } }

12 A Servlet That Redirects Users to Browser-Specific Pages

13 A Front End to Various Search Engines
public class SearchEngines extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String searchString = request.getParameter("searchString"); if ((searchString == null) || (searchString.length() == 0)) { reportProblem(response, "Missing search string"); return; } searchString = URLEncoder.encode(searchString); String searchEngineName = request.getParameter("searchEngine"); if ((searchEngineName == null) || (searchEngineName.length() == 0)) { reportProblem(response, "Missing search engine name"); return; }

14 A Front End to Various Search Engines (Continued)
String searchURL = SearchUtilities.makeURL(searchEngineName, searchString); if (searchURL != null) { response.sendRedirect(searchURL); } else { reportProblem(response, “Unrecognized search engine"); private void reportProblem(HttpServletResponse response, String message) throws IOException { response.sendError(response.SC_NOT_FOUND, message); } }

15 A Front End to Various Search Engines (Continued)
public class SearchSpec { /** Builds a URL for the results page by * simply concatenating the base URL * ( with the * URL-encoded search string (jsp+training). */ public String makeURL(String searchString) { return(baseURL + searchString); } …

16 Front End to Search Engines: HTML Form

17 Front End to Search Engines: Result for Valid Data

18 Front End to Search Engines: Result for Invalid Data


Download ppt "Generating the Server Response: HTTP Status Codes"

Similar presentations


Ads by Google