Web开发中错误页面的配置
一、创建错误处理页。
1)web.xml里面添加
<error-page> <error-code>404</error-code> <location>/404.html</location> </error-page>
2)在根目录下建立404.html
3)测试:随手输入一个不存在的url地址。可以看到404.html被正确显示。
二、按异常类型指定跳转。
1)新建servlet包的Hello.java
2)在doGet里添加
PrintWriter out=resp.getWriter();//此为servlet的输出对象 Object obj = null; out.println(obj.toString()); //会出现java.lang.NullPointerException异常
另附上完整java代码:
package 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 hello extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
out.println(", using the POST method");
Object obj = null;
out.println(obj.toString());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
3)在根目录下建立error.html
4)在web.xml中添加
<!-- servlet配置 -->
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>servlet.hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/servlet/hello</url-pattern>
</servlet-mapping><!-- error-page配置 -->
<error-page>
<exception-type>java.lang.NullPointerException</exception-type> <!--异常类型-->
<location>/error.html</location>
5)测试:在地址栏中输入http://localhost:8080/ProjectTest/servlet/hello
web.xml配置文件如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>servlet.hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/servlet/hello</url-pattern>
</servlet-mapping>
<!-- error-page配置开始 -->
<error-page>
<exception-type>java.lang.NullPointerException</exception-type> <!--异常类型-->
<location>/error.html</location>
</error-page>
<!-- error-page配置结束 -->
<!-- 404页面配置 开始-->
<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<!-- 404页面配置 结束-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Web开发中错误页面的配置的更多相关文章
- Web开发中,页面渲染方案
转载自:http://www.jianshu.com/p/d1d29e97f6b8 (在该文章中看到一段感兴趣的文字,转载过来) 在Web开发中,有两种主流的页面渲染方案: 服务器端渲染,通过页面渲染 ...
- WEB开发中的页面跳转方法总结
PHP header()函数跳转 PHP的header()函数非常强大,其中在页面url跳转方面也调用简单,使用header()直接跳转到指定url页面,这时页面跳转是302重定向: $url = & ...
- Web开发中运行环境的配置:(Tomcat7.0.59)和开发环境的配置
第一部分:运行环境的配置 1.下载压缩包,解压即可 2.配置系统变量JAVA_HOME为jdk的安装路径 3.如有需要修改端口号,比如8080已被占用的时候,可以将其改为9080等 apache-to ...
- SpringBoot学习(七)-->SpringBoot在web开发中的配置
SpringBoot在web开发中的配置 Web开发的自动配置类:在Maven Dependencies-->spring-boot-1.5.2.RELEASE.jar-->org.spr ...
- (转)Web开发中最致命的小错误
Web开发中最致命的小错误 现在,有越来越多所谓的“教程”来帮助我们提高网站的易用性.本文收集了一些在 Web 开发中容易出错和被忽略的小问题,并且提供了参考的解决方案,以便于帮助 Web 开发者更好 ...
- Web开发中的18个关键性错误
前几年,我有机会能参与一些有趣的项目,并且独立完成开发.升级.重构以及新功能的开发等工作. 本文总结了一些PHP程序员在Web开发中经常 忽略的关键错误,尤其是在处理中大型的项目上问题更为突出.典型的 ...
- Web开发中的主要概念
一.Web开发中的主要概念1.静态资源:一成不变的.html.js.css2.动态资源:JavaWeb.输出或产生静态资源.(用户用浏览器看到的页面永远都是静态资源) 3.JavaEE:十三种技术的集 ...
- [译]如何在Web开发中使用Python
[译]如何在Web开发中使用Python 原文:HOWTO Use Python in the Web 摘要 这篇文档展示了Python如何融入到web中.它介绍了几种Python结合web服务器的方 ...
- 结合jquery的前后端加密解密 适用于WebApi的SQL注入过滤器 Web.config中customErrors异常信息配置 ife2018 零基础学院 day 4 ife2018 零基础学院 day 3 ife 零基础学院 day 2 ife 零基础学院 day 1 - 我为什么想学前端
在一个正常的项目中,登录注册的密码是密文传输到后台服务端的,也就是说,首先前端js对密码做处理,随后再传递到服务端,服务端解密再加密传出到数据库里面.Dotnet已经提供了RSA算法的加解密类库,我们 ...
随机推荐
- mysql乱码的好文
1. http://www.blogjava.net/wldandan/archive/2007/09/04/142669.html 2. http://www.111cn.net/database/ ...
- smarty模板继承
- mysqli_stmt预处理类
<?php $mysqli=new mysqli("localhost", "root", "123456", "xsph ...
- Css 常用属性
1. overflow:hidden和zoom:1 verflow:hidden;的作用 1. 隐藏溢出 :2.消除浮动 <style type="text/css"> ...
- [py]导入模块3种方法
import os <--通过os.system()引用 from os import * <---直接system()引用,不建议使用 from os import argv i ...
- 关闭linux centos各种声音
shell报警 #vi /etc/inputrc ================================ set bell-style none 或 echo "set bell- ...
- WorldWind源码剖析系列:WorldWind实时确定、更新、初始化和渲染地形和纹理数据
WorldWind实时确定.更新.初始化和渲染地形和纹理数据 当用户点击WorldWind中的地球时,首先响应的是WorldWindow.OnPaint()函数,后续程序的调用流程如下图所示. 零散知 ...
- The specified LINQ expression contains references to queries that are associated with different contexts
今天在改写架构的时候,遇到这么个错误.当时单从字面意思,看上去错误是由join的两个不同的表来源不一致引起的. 其中的videoResult和deerpenList均来自与同一个edmx文件,所以两个 ...
- linux实践——字符集
一.ASCII码 首先是看得懂ASCII码表: 二 八 十 十六 缩写/字符 0000 0000 0 0 00 NUL(null) 0000 0001 1 1 01 SOH(start of head ...
- 足球运动训练心得及经验分析-c语言学习调查
在准备预备作业02之前,我参考娄老师的提示,阅读了<[做中学(Learning By Doing)]之乒乓球刻意训练一年总结>一文. 在文章描述的字里行间,给予我的印象是系统.负责,娄老师 ...