我正在开发一个涉及几个servlet和一些jsp页面的小型MVC项目。在项目的早期,我添加了这个ErrorHandler-servlet:
package org.ics.ejb.lab;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/ErrorHandler")
public class ErrorHandler extends HttpServlet {
private static final long serialVersionUID = 1L;
public ErrorHandler() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processError(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processError(request, response);
}
private void processError(HttpServletRequest request, HttpServletResponse response) throws IOException {
Throwable throwable = (Throwable) request
.getAttribute("javax.servlet.error.exception");
Integer statusCode = (Integer) request
.getAttribute("javax.servlet.error.status_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) {
requestUri = "Unknown";
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.write("<html><head><title>Exception/Error Details</title></head><body>");
if(statusCode != 500){
out.write("<h3>Error Details</h3>");
out.write("<strong>Status Code</strong>:"+statusCode+"<br>");
out.write("<strong>Requested URI</strong>:"+requestUri);
}else{
out.write("<h3>Exception Details</h3>");
out.write("<ul><li>Servlet Name:"+servletName+"</li>");
out.write("<li>Exception Name:"+throwable.getClass().getName()+"</li>");
out.write("<li>Requested URI:"+requestUri+"</li>");
out.write("<li>Exception Message:"+throwable.getMessage()+"</li>");
out.write("</ul>");
}
out.write("<p>Show Mats Svensson this error message. Then go back and try again.</p>");
//out.write("<a href="index.html">Go back</a>");
out.write("<br><br>");
out.write("</body></html>");
}
}
然后我将其添加到动态Web项目的web.xml文件中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>EmpClientProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/ErrorHandler</location>
</error-page>
<servlet>
<servlet-name>JUnitEETestServlet</servlet-name>
<servlet-class>org.junitee.servlet.JUnitEEServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JUnitEETestServlet</servlet-name>
<url-pattern>/TestServlet/*</url-pattern>
</servlet-mapping>
</web-app>
它很早就开始了,但是当我添加更多内容,html页面,图像等时,ErrorHandler-servlet突然停止工作,我还没有弄清楚为什么。我想不出任何原因。
这是代表nullreferenceexception的代码,假设员工在数据库中不存在。用于捕获这个但不再存在的servlet:
package org.ics.ejb.lab;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import example.ejb.ics.Department;
import example.ejb.ics.Employee;
import examples.facade.ics.FacadeLocal;
@WebServlet("/EmpServlet")
public class EmpServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB
FacadeLocal facade;
public EmpServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.print("Error: Does not exist in database, try again");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String url = null;
String operation = request.getParameter("operation");
if(operation.equals("showemployee")) {
String socialsn = request.getParameter("socialsn");
Long pnr = Long.parseLong(socialsn);
Employee e = facade.findBySocialSN(pnr);
request.setAttribute("employee", e);
url="/ShowEmployee.jsp";
} else if(operation.equals("searchemployee")){
url="/SearchEmployee.jsp";
} else if(operation.equals("updatedept")){
String depnr = request.getParameter("newdeptid");
int intdepnr = Integer.parseInt(depnr);
Department d = facade.findByDeptId(intdepnr);
String socialsn = request.getParameter("employeeSN");
Long pnr = Long.parseLong(socialsn);
Employee e = facade.findBySocialSN(pnr);
e.setDepartment(d);
Employee updatedEmployee = facade.updateEmployee(e);
request.setAttribute("employee", updatedEmployee);
request.setAttribute("department", d);
url="/ShowEmployee.jsp";
}
else{
url="/SearchEmployee.jsp";
}
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}