错误页面未显示在servlet中

时间:2016-11-04 11:49:15

标签: java servlets

我正在尝试学习servlet的错误处理我的目的是在错误代码为404或500时重定向到错误页面。因为这已写入error.java并创建了error.jsp页面,但它不是错误页面。请帮我解释为什么会发生这种情况?FYI我故意投掷错误

我的web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <filter>
        <filter-name>MyFilter</filter-name>
        <filter-class>com.MyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>MyFilter</filter-name>
        <servlet-name>Login</servlet-name>
    </filter-mapping>

    <servlet>
        <servlet-name>ErrorHandler</servlet-name>
        <servlet-class>com.Error</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>Login</servlet-name>
        <servlet-class>com.Login</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ErrorHandler</servlet-name>
        <url-pattern>/Error</url-pattern>
    </servlet-mapping>



    <servlet-mapping>
        <servlet-name>Login</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>
    <error-page>
        <error-code>500</error-code>
        <location>/Error</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/Error</location>
    </error-page>

</web-app>

Error.java

package com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Error
 */
public class Error extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Error() {
        super();

    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession session=request.getSession();

    Throwable throwable=(Throwable) request.getAttribute("javax.servlet.error.exception");
    Integer code=(Integer) request.getAttribute("javax.servlet.error.statud_code");
     String servletName = (String)
              request.getAttribute("javax.servlet.error.servlet_name");
              if (servletName == null){
                 servletName = "Unknown";
              }
              String requestUri = (String)
              request.getAttribute("javax.servlet.error.request_uri");
              if(requestUri==null || "".equalsIgnoreCase(requestUri)){
                  requestUri="unkniwn";
              }
              session.setAttribute("errorMessage", "error");
              session.setAttribute("errorCode", code);
              PrintWriter pw=response.getWriter();
              pw.println("error here");
              System.out.println("error:"+code);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

Login.java

package com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();

    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.getWriter().append("Served at: ").append(request.getContextPath());
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter pw=response.getWriter();
        String user=request.getParameter("user");
        pw.println("Welcome "+user);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request, response);
    }

}

的index.jsp:

<html>
<body>
<h2>Hello World!</h2>
<form method="post" action="first">
    Name:<input type="text" name="user" /><br/>
    Password:<input type="text" name="password" /><br/>
    <input type="submit" value="submit" />
</form>
</body>
</html>

error.jsp文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hi this error</h1>
<form method="post">
<div style="color: #FF0000;"> ${errorMessage} ${errorCode}</div>
</form>
</body>
</html>

编辑1: 我检查了边缘浏览器它正在打印我在Error.java中的打印编写器中写的内容,但是它没有将我重定向到/ error url也没有显示error.jsp

2 个答案:

答案 0 :(得分:0)

您正在使用PrintWriter,这是不正确的,而您需要使用RequestDispatcher将请求转发到JSP页面,如下所示:

RequestDispatcher requestDispatcher= request.getRequestDispatcher("/Error.jsp");
requestDispatcher.forward(request, response);

您可以在servlet上查看here到JSP通信:

答案 1 :(得分:0)

在错误页面标记的web.xml中,您尝试为自己的servlet放置位置。而不是这个,你应该使用直接链接到你自己的.jsp错误页面。您可以通过jstl

从那里获得例外
<error-page>
    <error-code>500</error-code>
    <location>/WEB-INF/jsp/serverError.jsp</location> // Location to your jsp
</error-page>

这就是如何在页面上获取异常消息:
    <c:set var="message" value="${requestScope['javax.servlet.error.message']}"/>