不允许未登陆用户访问的过滤器配置,filter,用户登陆过滤器,session判断用户登陆...

不允许未登陆用户访问的过滤器配置
web.xml

<filter>
    <filter-name>onlineFilter</filter-name>
    <filter-class>filter.OnlineFilter</filter-class>
</filter>
   
<filter-mapping>
    <filter-name>onlineFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
</filter-mapping>

 

Filter

 

package filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
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 OnlineFilter extends HttpServlet implements Filter
{
 private static final long serialVersionUID = 1L;
 public void init(FilterConfig filterConfig) throws ServletException
 {

 }
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException 
 {
  RequestDispatcher dispatcher = request.getRequestDispatcher("KK_BlacklistVehicle_UserLogin.jsp");//这里设置如果没有登陆将要转发到的页面
  HttpServletRequest req = (HttpServletRequest) request;
  HttpServletResponse res = (HttpServletResponse) response;
  HttpSession session = req.getSession(true);

//  System.out.println(((HttpServletRequest) request).getRequestURI());
  // 从session里取的用户名信息
  String username = (String) session.getAttribute("sessionKK_BlacklistVehicle_UserLogin_ID");//这里获取session,为了检查session里有没有保存用户信息,没有的话回转发到登陆页面

  // 判断如果没有取到用户信息,就跳转到登陆页面
  if (username == null || "".equals(username))
  {
   // 跳转到登陆页面
   dispatcher.forward(request,response);
//   System.out.println("用户没有登陆,不允许操作");
   
   res.setHeader("Cache-Control","no-store");   
   res.setDateHeader("Expires",0);
   res.setHeader("Pragma","no-cache");
  }
  else
  {
   // 已经登陆,继续此次请求
   chain.doFilter(request,response);
//   System.out.println("用户已经登陆,允许操作");
  }
 }
 public void destroy()
 {

 }
}

 黑色头发  http://heisetoufa.iteye.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值