MSc-IT Study Material
June 2010 Edition

Computer Science Department, University of Cape Town

Response status

The Cookie-based counter example had various exception handlers which should have reported an error message and stopped the execution of the doGet() method. However, it is not clear how an error message might be reported. For instance, when writing output to the response object, it itself may throw an IOException which needs to be reported in some way. The way to do this over HTTP is to use the response status. This is a number which is returned the the Web browser from the server to indicate the status of the HTTP request.

Some commonly used response statuses are:

When a page is successfully returned to the browser, HTTP also supplies the browser with the value 200 to indicate the the request was successful. If a request is made to a page which the user is not allowed to view, the response status 403 is given to the browser; 404 is returned when the requested page was not found. 500 is returned when there was an error processing a page. In all of these cases actual content to be displayed can also be returned. For instance, a special message indicating that the requested page was not found could be returned with the appropriate response.

Many of the response status codes are automatically sent by the Servlet based on context. For instance, if the doGet() or doPost() methods return successfully, and they have not explicitly requested a different status code, 200 will automatically be sent. For sending status codes in general, the response.setStatus(int) method can be used. The HttpServletResponse object also contains many public static final member variables that contain the appropriate values. For instance, the four codes presented above are contained in the SC_OK, SC_NOT_FOUND, SC_FORBIDDEN, SC_INTERNAL_SERVER_ERROR variables.

If you want to both return HTML and set a status other than 200, be sure to set the status before you sending any output.

Errors can now be checked for and reported in our Web applications. For example, in the above counter code we could have done the following:

	    try  {
		writer = new PrintWriter(new FileWriter(file_name));
		writer.println(count);
		writer.close();
		writer = null;
	    }
	    catch (IOException e) {
		response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		return;
	    }
	    finally {
		if (writer != null)
		    writer.close();
	    }
    

Which would have stopped execution of doGet() and notified an error. This is still not as useful as it could be: the user is not given any indication of what failed, or why. The sendError() method allows for this:

	    try  {
		writer = new PrintWriter(new FileWriter(file_name));
		writer.println(count);
		writer.close();
		writer = null;
	    }
	    catch (IOException e) {
		try {
		response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 
      	}
		catch (IOException e2)
		{
		response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			return;
		}
		return;
	    }
	    finally {
		if (writer != null)
		    writer.close();
	    }
    

Notice that sendError can itself throw an IOException; if it does, you should fall back to only setting the status.