Files
wahyd4.github.com/source/_posts/java-web-define-error-page.md
Hermes Bot 232ecd7c22 Rebuild blog source from GitHub Pages artifact
- 308 posts reverse-extracted from wahyd4.github.com (Hexo 6.3.0 + NexT 8.25)
- Hexo project with NexT theme, reading-experience custom styles
- publish-post.sh: write post -> hexo build -> PR (master untouched)
2026-08-04 09:44:07 +10:00

58 lines
1.8 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "java web.xml定义配置错误页面及使用"
date: 2012-08-16 00:00:00
tags: [java, web, web.xml]
---
我们在开发java web 的程序时候,经常会需要给用户提示信息,或者页面。最常见的,比如用户输入了一个不存在的网址,默认情况下Tomcat会给用户返回一个默认的页面。如下所示:
[![](/images/2012/08/22.jpg "22")](/images/2012/08/22.jpg)
这个页面千篇一律。我们完全可以给用户展示一个自定义的页面。
其实java web程序中web.xml 本身就支持自定义错误页面,我们只需要在web.xml添加如下信息:
```xml
404
/errorpage/404.html
401
/errorpage/401.html
500
/errorpage/500.html
```
当然“error-code”,可以自己选择在程序中出现得最多的错误码。我个人认为通常我们拦截:
401:未授权
404:没有找到页面
500:服务器内部错误
这三个即可。在“location”中配置你自己的html,当然页面样式,内容这些这些都可以自己定义。这个就看你自己啦。
如果我们不做其他操作,当系统出现这些错误情况时,服务器就会自动调用我们配置的网页。当然我们可以自行在恰当的时候使用它。
在servlet或者其他可能访问到HttpServletResponse对象的地方,使用:
```java
response.sendError(404); //404为你需要的错误代码
//也可以向前台网页传输错误语句,不过前台网页需要相应支持。这里没有实现
response.sendError(404,"这里写想要输出到前台的提示信息");
```
在java中还可以使用:
```java
response.setStatus(404);  //设置状态码
```
设置状态码来实现,不过这并不能调用配置的自定义页面。只是在前台显示错误,并无输出。