利用servlet写的用户注册登陆
源代码在线查看: sessionfilter.java
package com.tarena.filter;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class SessionFilter extends HttpFilter {
@Override
public void doFilter(HttpServletRequest request,
HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpSession session = request.getSession(true);
String path = request.getServletPath();
path = path.substring(0, path.indexOf("."));
if (session.getAttribute("userInfo") != null && !path.equals("/login")
&& !path.equals("/register")) {
response.sendRedirect("/serv-guides/login.html");
return;
} else {
chain.doFilter(request, response);
}
}
}