通过HttpServletResponse 将异常信息显示在网页上

 通过HttpServletResponse 的sendError(sc, msg); 可将异常信息在网页上显示

 代码:

package www.csdn.net.day35.servlet;

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;

public class DemoServlet1 extends HttpServlet {

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

		try{
		int i=1/0;
		}catch(Exception e){
			//设置错误界面及内容
			response.sendError(response.SC_NOT_FOUND, e.getMessage());
		}
		
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
   
		this.doGet(request, response);
	}

}


效果:

通过HttpServletResponse 将异常信息显示在网页上_第1张图片

你可能感兴趣的:(java,exception,servlet,异常)