1----------------------------
登录过滤类loginFilter
------------------------------------------
package spell;
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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class loginFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig;
//Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
//Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) {
try {
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse rsp=(HttpServletResponse)response;
HttpSession session=req.getSession(true);
String userName=(String)session.getAttribute("userName");
if(userName!=null)
{//验证通过
filterChain.doFilter(request, response);
}
else
{//验证不通过,取得设置的跳转页面参数
String noLogin=filterConfig.getInitParameter("noLogin");
rsp.sendRedirect(noLogin);
}
} catch (Exception sx) {
filterConfig.getServletContext().log(sx.getMessage());
}
}
//Clean up resources
public void destroy() {
}
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(arg0, arg1);
}
protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(arg0, arg1);
}
}
web.xml配置
----------------------------
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>spell.loginFilter</filter-class> <!--loginFilter对应的类名-->
<init-param>
<param-name>noLogin</param-name>
<param-value>../strutsHtml.jsp</param-value> <!--没有登录跳转的页面-->
</init-param>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name> <!--选择loginFilter来验证登录-->
<url-pattern>/member/*</url-pattern> <!--配置member下的文件要求权限判断-->
</filter-mapping>
2-----------------------------
servlet登陆拦截器
2012-07-26 11:25:03| 分类: 默认分类 |举报|字号 订阅
实现Servlet.Filter接口
public class AdminLoginFilter implements Filter
新建一个实现filter接口的类,实现dofilter方法,在dofilter方法里面写一个判读,比如如果用户名密码为空,则返回到登录页面
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) arg1;
HttpServletRequest request=(HttpServletRequest)arg0;
HttpSession session = request.getSession(true);
String username = (String) session.getAttribute("username");//
String url=request.getRequestURI();
if(username==null || username.equals(""))
{
//判断获取的路径不为空且不是访问登录页面或执行登录操作时跳转
if(url!=null && !url.equals("") && ( url.indexOf("Login")<0 && url.indexOf("login")<0 ))
{
response.sendRedirect("login.jsp");
return ;
}
}
//已通过验证,用户访问继续
arg2.doFilter(arg0, arg1);
}
web.xml配置过滤器
<filter>
<filter-name>AdminLoginFilter</filter-name>
<filter-class>com.vnb.servlet.AdminLoginFilter</filter-class>
</filter>
<!-- 配置多个映射,哪些请求执行过滤器 -->
<filter-mapping>
<filter-name>AdminLoginFilter</filter-name>
<url-pattern>/web-inf/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>AdminLoginFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
3----------------------------------
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) arg1;
HttpServletRequest request=(HttpServletRequest)arg0;
HttpSession session = request.getSession(true);
String username = (String) session.getAttribute("username");//
String url=request.getRequestURI();
if(username==null || username.equals(""))
{
//判断获取的路径不为空且不是访问登录页面或执行登录操作时跳转
if(url!=null && !url.equals("") && ( url.indexOf("Login")<0 && url.indexOf("login")<0 ))
{
response.sendRedirect("login.jsp");
return ;
}
}
//已通过验证,用户访问继续
arg2.doFilter(arg0, arg1);
}
web.xml配置过滤器
<filter>
<filter-name>AdminLoginFilter</filter-name>
<filter-class>com.vnb.servlet.AdminLoginFilter</filter-class>
</filter>
<!-- 配置多个映射,哪些请求执行过滤器 -->
<filter-mapping>
<filter-name>AdminLoginFilter</filter-name>
<url-pattern>/web-inf/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>AdminLoginFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
4---------------------------
用filter结合session
例如:
public class CheckStuLoginFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse resp = (HttpServletResponse) response;
HttpServletRequest req = (HttpServletRequest) request;
HttpSession session = req.getSession();
Stuinfo stuinfo = (Stuinfo) session.getAttribute("stuinfo");
String requestURI = req.getRequestURI().substring(
req.getRequestURI().indexOf("/", 1),
req.getRequestURI().length());
if (!requestURI.equals("/index.jsp")) {
if (stuinfo == null) {
resp.sendRedirect(req.getContextPath() + "/index.jsp");
return;
}
}
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}