1.新建一个FirstFilter类代码如下:
package com.ask.filter;
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.HttpServletResponse;
/*自定义一个java类实现filter接口*/
public class FirstFilter implements Filter {
//通过file配置文件中修改编码方式;
private FilterConfig config;
public FirstFilter(){
System.out.println("FirstFilter: "+this.hashCode());
}
public void init(FilterConfig arg0) throws ServletException {
config=arg0;
System.out.println("init: "+this.hashCode());
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
String utf8=config.getInitParameter("utf-8");
HttpServletResponse resp=(HttpServletResponse) res;
//resp.setContentType("text/html;charset=utf-8");
resp.setContentType("text/html;charset="+utf8);
//调用下一个filter或者是web资源
chain.doFilter(req, res);
}
public void destroy() {
System.out.println("destroy: "+this.hashCode());
}
}
2.新建一个LoginFilter类代码如下:
package com.ask.filter;
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 doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest) arg0;
HttpServletResponse res=(HttpServletResponse) arg1;
//获取资源路径;
String uri=req.getRequestURI();
//获得jsp文件名;
String action=uri.substring(uri.lastIndexOf("/")+1, uri.indexOf("."));
System.out.println(action);
if ("login".equals(action)&&uri.endsWith(".jsp")) {//判断是否是longin.jsp请求
chain.doFilter(req, res);
}else{//main.jsp longin.do
if ("login".equals(action)) {//如果是login.do请求
HttpSession session=req.getSession();
if (session.getAttribute("name")==null) {
String name=req.getParameter("username");
String pwd=req.getParameter("password");
if ("tom".equals(name)&&"123".equals(pwd)) {
session.setAttribute("name", name);
chain.doFilter(req, res);
}else{
res.sendRedirect(req.getContextPath()+"/login.jsp");
}
}
}else{
HttpSession session=req.getSession(false);
if (session.getAttribute("name")==null) {
res.sendRedirect(req.getContextPath()+"/login.jsp");
}else{
chain.doFilter(req, res);
}
}
}
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
public void destroy() {
// TODO Auto-generated method stub
}
}
3.新建一个SecFilter类,代码如下:
package com.ask.filter;
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;
public class SecFilter implements Filter {
public void destroy() {
System.out.println("sec---destroy");
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
chain.doFilter(req, res);
}
public void init(FilterConfig arg0) throws ServletException {
System.out.println("sec---init");
}
}
4.新建一个showServlet类代码如下:
package com.ask.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class showServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String name=req.getParameter("username");
System.out.println("service");
res.getWriter().write("演示servlet中的filter过滤器");
HttpSession session=req.getSession();
session.setAttribute("name", name);
System.out.println("登录成功");
res.sendRedirect(req.getContextPath()+"/main.jsp");
}
}
5.在WebRoot目录下新建两个jsp文件,分别是login.jsp和main.jsp文件
Login.jsp如下
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/login.do" method="post">
username:<input type="text" name="username">
password:<input type="password" name="password">
<input type="submit" value="longing">
</form>
</body>
</html>
Main.jsp如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
欢迎 ${name}进入主页面
</body>
</html>
6.在web.xml配置文件中配置如下:
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>firstServlet</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>
<!-- 测试LoginFilter -->
<filter>
<filter-name>first</filter-name>
<filter-class>com.ask.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>first</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>show</servlet-name>
<servlet-class>com.ask.servlet.showServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>show</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>