Servlet------(声明式)异常处理
2016-03-30 16:22
441 查看
Test.java
其他方法不变,重写
web.xml
其他代码不变,加入下面代码
errorPage.jsp
其他方法不变,重写
protected void service()方法
public void init(ServletConfig config) throws ServletException { // Put your code here super.init(config); } @Override protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // TODO Auto-generated method stub //super.service(arg0, arg1); String method=req.getMethod(); if(method.equals("POST")){ doPost(req,res); } else if(method.equals("GET")){ doGet(req,res); } res.setContentType("text/html;charset=gb2312"); PrintWriter out=res.getWriter(); Integer status_code=(Integer) req.getAttribute("javax.servlet.error.status_code"); out.print("<html><head><title>错误处理页面</title></head>"); out.print("<body>"); switch(status_code){ case 401: break; case 404: out.print("<h2>HTTP转态代码: "+status_code+"</h2><br>"); out.print("您正在搜索的页面已删除<>br"); break; default: break; } out.print("</body>"); out.print("</html>"); out.close(); }
web.xml
其他代码不变,加入下面代码
<web-app> ........ <error-page> <error-code>401</error-code> <location>/errorPage.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/errorPage.jsp</location> </error-page> </web-app>
errorPage.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'welcome.jsp' starting page</title> </head> <body> 401,405错误处理页面 </body> </html>
相关文章推荐
- O2O商业模式剖析
- POJ 3034 DP
- 55. Jump Game
- centos 7 中文输入法的安装和启用
- 剑指offer-面试题18:树的子结构
- ionic云开发系列一
- Linux Shell常用技巧(十) 管道组合
- ajax 跨域解决 网上资料
- Lua环境安装
- linux 下检查java jar包 程序是否正常 shell
- linux 下检查java jar包 程序是否正常 shell
- 转载:学习Entity Framework 中的Code First
- 字符倒序
- andorid:listSelector失效问题。
- java中的缓存技术该如何实现
- 使用java 和freemarker实现多级菜单(分类)
- 2016年3月7日作业
- Linux Shell常用技巧(九) 系统运行进程
- jquery实现简单的banner轮播效果【实例】
- 阶乘之和