filter过滤器之自动登录代码

servlet类
package com.csdn.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.csdn.domain.User;
import com.csdn.service.UserService;
import com.csdn.service.UserServiceImpl;

public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String username = request.getParameter("username");
		String password = request.getParameter("password");
		int logintime = Integer.parseInt(request.getParameter("loginTime"));
		System.out.println(username + "--" + password + "--" + logintime);
		UserServiceImpl impl = new UserService();
		User user = impl.find(username, password);
		if (user != null) {
			request.getSession().setAttribute("user", user);

			Cookie cookie1 = new Cookie("username", user.getUsername());
			Cookie cookie2 = new Cookie("password", user.getPassword());

			cookie1.setMaxAge(logintime);
			cookie2.setMaxAge(logintime);

			cookie1.setPath("/filter_test");
			cookie2.setPath("/filter_test");

			response.addCookie(cookie1);
			response.addCookie(cookie2);

			response.sendRedirect("/filter_test/success.jsp");

		} else {
			request.setAttribute("message", "用户名或密码错误");
			request.getRequestDispatcher("/message.jsp");
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);

	}

}

过滤器:

package com.csdn.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.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.csdn.domain.User;
import com.csdn.service.UserService;
import com.csdn.service.UserServiceImpl;

public class FilterTest implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;

		User user = (User) req.getSession().getAttribute("user");
		if (user != null) {
			chain.doFilter(req, resp);
			return;
		}
		String username = null;
		String password = null;
		Cookie[] cookie = req.getCookies();
		for (int i = 0; cookie != null && i < cookie.length; i++) {
			if (cookie[i].getName().equals("username")) {
				username = cookie[i].getValue();
			}
			if (cookie[i].getName().equals("password")) {
				password = cookie[i].getValue();
			}
		}
		if (username == null && password == null) {
               chain.doFilter(req, resp);
               return;
		}
		
		UserServiceImpl impl = new UserService();
		User user1 = impl.find(username);
		String save_password = user1.getPassword();
		if(!password.equals(save_password)){
			chain.doFilter(req, resp);
			return;
		}
		req.getSession().setAttribute("user", user1);
		chain.doFilter(req, resp);
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值