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
}
}