Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Error in getting the servlet response

843842Jun 5 2009 — edited Jun 11 2009
Below is my ErrorPage Servlet. Whenever a 404 page not found error is encountered, the error page servlet reads the 404.html, and writes the content to the response. But I am not able to see the content in IE, but works fine in mozilla.
 

public class ErrorPageServlet extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {



        String errorPath = request.getAttribute("javax.servlet.error.request_uri").toString();
        int statusCode = Integer.parseInt(request.getAttribute("javax.servlet.error.status_code")
                .toString());
        String[] errorPathSegment = errorPath.split("/");
        String site = errorPathSegment[2];


        switch (statusCode) {
        case HttpServletResponse.SC_NOT_FOUND:
            writeOutFile(request, response, "/" + site + "/error/404.html");
            break;


        case HttpServletResponse.SC_INTERNAL_SERVER_ERROR:
        case HttpServletResponse.SC_NOT_IMPLEMENTED:
        case HttpServletResponse.SC_BAD_GATEWAY:
        case HttpServletResponse.SC_SERVICE_UNAVAILABLE:
        case HttpServletResponse.SC_GATEWAY_TIMEOUT:
        case HttpServletResponse.SC_HTTP_VERSION_NOT_SUPPORTED:
            writeOutFile(request, response, "/" + site + "/error/500.html");
            break;
        }


    }


    public void writeOutFile(HttpServletRequest request, HttpServletResponse response,
            String fileName) throws IOException {


        ServletContext context = request.getSession().getServletContext();
        response.setContentType("text/html");

        PrintWriter writer = response.getWriter();
        InputStream resourceStream = context.getResourceAsStream(fileName);
        if (resourceStream != null) {
            InputStreamReader streamReader = new InputStreamReader(resourceStream);
            BufferedReader reader = new BufferedReader(streamReader);
            StringBuilder builder = new StringBuilder();
            for (String line = reader.readLine(); line != null; line = reader.readLine()) {
                builder.append(line);
                builder.append('\n');
            }
            writer.write(builder.toString());
            writer.flush();
        } else {
            log.info("Problem occurred with " + fileName);
            response.sendRedirect("/");
        }
    }
Also attached is my 404.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd]">
<html xmlns="[http://www.w3.org/1999/xhtml]">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="refresh" content="10;URL=/" >
</head>
<body>
<h1>Error</h1>
<p>A technical error has occurred. You will redirected in 10 seconds</p>
</body>
</html>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 9 2009
Added on Jun 5 2009
8 comments
487 views