Filter 相关知识点-----自动登录
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<body>
<c:if test="${empty name}" var="boo"><!-- 没有登录才显示 from表单-->
<form action="<c:url value='/AutoServlet'/>" method="post">
Name:<input type="text" name="name"/><br/>
Pwd:<input type="password" name="pwd"/><br/>
<input type="radio" name="time" vaule="0" />不自动登录
<input type="radio" name="time" vaule="1" />1天
<input type="radio" name="time" vaule="7" />1周<br/>
<input type="submit" value="登录"/>
</form>
</c:if>
<c:if test="!boo">
</c:if>
</body>
总结:
<!-- Filter:优先级高;项目一启动就初始化;只要看到request和 response就会自动
进入Filter;根据其配置的URL看是不是他想拦截过滤的资源;不是就放行;是就进过处理再放行-->
<!-- 第一次请求index.jsp界面:没有cookie(AutoLogin);没有session(name)
先进入filter,是执行chain.doFilter();访问LoginServlet;登录成功后添加 session(name)
cookie(AutoLogin)-->
<!-- 第二次请求index.jsp(没关浏览器)界面:有cookie(AutoLogin);有session(name)
进入filter进入拦截功能,这时Filter会来做登录的操作;再chain.doFilter()-->
<!-- 第三次请求index.jsp(关浏览器)界面:有cookie(AutoLogin);没有session(name)
先进入filter进入拦截功能,这时Filter会来找cookie(AutoLogin)做登录的操作;再chain.doFilter()-->
AutoServlet.java(cn.hucu.servlet包)
public class AutoLogin extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
String stime=request.getParameter("time");
if(name.contains("hncu")){
//这里本来是调用dao层;
request.getSession().setAttribute("name", name);
}
//--------------自动登录----------------
name=URLEncoder.encode(name, "utf-8");
pwd=URLEncoder.encode(pwd, "utf-8");
Cookie c=new Cookie("AutoLogin", name+"@#"+pwd);
c.setPath("/");
int time=60*60*24*(Integer.valueOf(stime));
c.setMaxAge(time);//设置cookie的生命周期
response.addCookie(c);
}
}
AutoLoginFilter.java(cn.hucu.filter包)
public class AutoLoginFilter implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest) request;
if(req.getSession().getAttribute("name")==null){
Cookie cookies[]=req.getCookies();
if(cookies!=null){
for(Cookie c:cookies){
if("AutoLogin".equals(c.getName())){
//来做的登录的事
String vals[]=c.getValue().split("@#");
String name=URLDecoder.decode(vals[0], "utf-8");
String pwd=URLDecoder.decode(vals[1], "utf-8");
if(name.contains("hncu")){
//转到登录后的界面
}
}
}
}
}
chain.doFilter(request, response);
}
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<body>
<c:if test="${empty name}" var="boo"><!-- 没有登录才显示 from表单-->
<form action="<c:url value='/AutoServlet'/>" method="post">
Name:<input type="text" name="name"/><br/>
Pwd:<input type="password" name="pwd"/><br/>
<input type="radio" name="time" vaule="0" />不自动登录
<input type="radio" name="time" vaule="1" />1天
<input type="radio" name="time" vaule="7" />1周<br/>
<input type="submit" value="登录"/>
</form>
</c:if>
<c:if test="!boo">
</c:if>
</body>
总结:
<!-- Filter:优先级高;项目一启动就初始化;只要看到request和 response就会自动
进入Filter;根据其配置的URL看是不是他想拦截过滤的资源;不是就放行;是就进过处理再放行-->
<!-- 第一次请求index.jsp界面:没有cookie(AutoLogin);没有session(name)
先进入filter,是执行chain.doFilter();访问LoginServlet;登录成功后添加 session(name)
cookie(AutoLogin)-->
<!-- 第二次请求index.jsp(没关浏览器)界面:有cookie(AutoLogin);有session(name)
进入filter进入拦截功能,这时Filter会来做登录的操作;再chain.doFilter()-->
<!-- 第三次请求index.jsp(关浏览器)界面:有cookie(AutoLogin);没有session(name)
先进入filter进入拦截功能,这时Filter会来找cookie(AutoLogin)做登录的操作;再chain.doFilter()-->
AutoServlet.java(cn.hucu.servlet包)
public class AutoLogin extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
String stime=request.getParameter("time");
if(name.contains("hncu")){
//这里本来是调用dao层;
request.getSession().setAttribute("name", name);
}
//--------------自动登录----------------
name=URLEncoder.encode(name, "utf-8");
pwd=URLEncoder.encode(pwd, "utf-8");
Cookie c=new Cookie("AutoLogin", name+"@#"+pwd);
c.setPath("/");
int time=60*60*24*(Integer.valueOf(stime));
c.setMaxAge(time);//设置cookie的生命周期
response.addCookie(c);
}
}
AutoLoginFilter.java(cn.hucu.filter包)
public class AutoLoginFilter implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest) request;
if(req.getSession().getAttribute("name")==null){
Cookie cookies[]=req.getCookies();
if(cookies!=null){
for(Cookie c:cookies){
if("AutoLogin".equals(c.getName())){
//来做的登录的事
String vals[]=c.getValue().split("@#");
String name=URLDecoder.decode(vals[0], "utf-8");
String pwd=URLDecoder.decode(vals[1], "utf-8");
if(name.contains("hncu")){
//转到登录后的界面
}
}
}
}
}
chain.doFilter(request, response);
}
}
//注释仅仅代表个人观点