FileDocCategorySizeDatePackage
ErrorHandlerServlet.javaAPI DocExample2282Sat Jan 24 10:44:40 GMT 2004je3.servlet

ErrorHandlerServlet

public class ErrorHandlerServlet extends HttpServlet
This servlet is the server-side companion to the ErrorHandler.reportThrowable() utility method developed elsewhere in this this book; it responds to the HTTP POST request initiated by that method.

Fields Summary
Constructors Summary
Methods Summary
public voiddoPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

	ObjectInputStream in =
	    new ObjectInputStream(request.getInputStream());
	try {
	    Throwable throwable = (Throwable) in.readObject();
	    
	    // Exercise: save the throwable to a database, along with
	    // the current time, and the IP address from which it was reported.
	    
	    // Our response will be displayed within an HTML document, but
	    // it is not a complete document itself.  Declare it plain text,
	    // but it is okay to include HTML tags in it.
	    response.setContentType("text/plain");
	    PrintWriter out = response.getWriter();
	    out.println("Thanks for reporting your <tt>" +
			throwable.getClass().getName() + "</tt>.<br>" +
			"It has been filed and will be investigated.");
	}
	catch(Exception e) {
	    // Something went wrong deserializing the object; most likely
	    // someone tried to invoke the servlet manually and didn't provide
	    // correct data.  We send an HTTP error because that is the
	    // easiest thing to do.  Note, however that none of the HTTP error
	    // codes really describes this situation adequately.
	    response.sendError(HttpServletResponse.SC_GONE,
			       "Unable to deserialize throwable object");
	}