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");
}