Servlet中web.xml的配置

引言:这是一个采用原生Servlet开发的项目的一个简要配置,在这里记录一下,以便以后用到了

  可以直接copy,如又侵权,请联系本博主。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>netctoss</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>main</servlet-name>
<servlet-class>web.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!-- 声明发生哪种异常时转发到哪个错误的页面 -->
<!-- 注意,此处错误的页面的路径应该写绝对路径,而tomcat会主动帮我们加上项目名,
所以此处的绝对就不要再写项目名,避免重复 -->
<!-- 1.按照异常类型声明 -->
<!-- <error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/WEB-INF/error.jsp</location>
</error-page> --> <!-- 2.按照异常的编号声明 -->
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/error.jsp</location>
</error-page>
<error-page>
<error-code>405</error-code>
<location>/WEB-INF/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/error.jsp</location>
</error-page> <!-- 登录检查过滤器 -->
<filter>
<filter-name>login</filter-name>
<filter-class>web.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>login</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>

以下是一个过滤器:

package web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; public class LoginFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
/*
* 1.Filter由tomcat自动调用,tomcat会在接收到请求后,调用Servlet前调用Filter。
* 2.tomcat在调用Filter前就会创建好request和response。
* 3.tomcat实例化的是接口的实现类:
* RequestFacade implements HttpServletRequest
* HttpServletRequest extends ServletRequest
*/
HttpServletRequest request=(HttpServletRequest) req;
HttpServletResponse response=(HttpServletResponse) res;
//排除掉3个不需要检查的路径,即发现是这3个路径,直接让请求继续执行
String[] paths=new String[]{"/toLogin.do","/login.do","/createImg.do"};
String p=request.getServletPath();//获取当前路径
for(String path : paths){
if(p.equals(path)){
chain.doFilter(request, response);
return;
}
}
//从session获取账号
HttpSession session=request.getSession();
String adminCode=(String)session.getAttribute("adminCode");
//根据账号判断用户是否登录
if(adminCode ==null){
//没登录,重定向到登录页
response.sendRedirect("/netctoss/toLogin.do");
}else{
//已登录,请求继续执行
chain.doFilter(request, response);
}
} public void init(FilterConfig cfg) throws ServletException { } }
上一篇:JavaWeb工程中web.xml基本配置


下一篇:HTTP详解(1)-工作原理【转】