01: /*
02: * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: */
07: package winstone;
08:
09: import java.io.IOException;
10: import java.io.PrintWriter;
11: import java.io.StringWriter;
12: import java.io.Writer;
13: import java.util.Date;
14:
15: import javax.servlet.ServletException;
16: import javax.servlet.ServletRequest;
17: import javax.servlet.ServletResponse;
18: import javax.servlet.http.HttpServlet;
19:
20: /**
21: * A simple servlet that writes out the body of the error
22: *
23: * @author <a href="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
24: * @version $Id: ErrorServlet.java,v 1.3 2006/02/28 07:32:47 rickknowles Exp $
25: */
26: public class ErrorServlet extends HttpServlet {
27:
28: public void service(ServletRequest request, ServletResponse response)
29: throws ServletException, IOException {
30:
31: Integer sc = (Integer) request
32: .getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
33: String msg = (String) request
34: .getAttribute(RequestDispatcher.ERROR_MESSAGE);
35: Throwable err = (Throwable) request
36: .getAttribute(RequestDispatcher.ERROR_EXCEPTION);
37:
38: StringWriter sw = new StringWriter();
39: PrintWriter pw = new PrintWriter(sw);
40: if (err != null) {
41: err.printStackTrace(pw);
42: } else {
43: pw.println("(none)");
44: }
45: pw.flush();
46:
47: // If we are here there was no error servlet, so show the default error page
48: String output = Launcher.RESOURCES.getString(
49: "WinstoneResponse.ErrorPage", new String[] { sc + "",
50: (msg == null ? "" : msg), sw.toString(),
51: Launcher.RESOURCES.getString("ServerVersion"),
52: "" + new Date() });
53: response.setContentLength(output.getBytes(response
54: .getCharacterEncoding()).length);
55: Writer out = response.getWriter();
56: out.write(output);
57: out.flush();
58: }
59: }
|