登陆过滤器实现

  写了一个简单的聊天室,为实现登陆过滤功能,想了很长时间看书,网上找不到资源利用,花了不少功夫完成的,将过滤的原码贴出来,给  有需要的人一点帮助。。


一般过滤  -------------登陆界面  的表单  提交给自己,,在  过滤器 中判断,完成  跳转。。以下 的代码  实现了此功能。


至于web.xml  中的配置过滤器,,在编译环境中自动生成,,就不贴出来了。。希望对你有用。。   

package Filter;


import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import vo.Customer;


import dao.CustomerDao;


public class filter implements Filter {


public void destroy() {
}


public void doFilter (ServletRequest arg0, ServletResponse arg1,
FilterChain chain )throws IOException, ServletException  {


HttpServletRequest request=(HttpServletRequest)arg0;
HttpServletResponse response=(HttpServletResponse)arg1;
response.setContentType("text/html;charset=GBK");
request.setCharacterEncoding("GBK");
HttpSession session=request.getSession();
PrintWriter out=response.getWriter();



String patht=request.getContextPath();
String path=request.getRequestURI();
String pageName=path.substring(patht.length()+1);
//System.out.println(path); 
//if(访问的是login.jsp并且不是有关注册的页面){
//if(action ==null){
//
// 说明是第一次访问这个页面,让他继续执行。。。chain.dofilter(..,..);
//}else{
//
// 说明是点击“登陆“按钮访问页面的(**login.jsp的action让他访问自己,在过滤器中判断跳转**)
// 获取它 的登录名,密码进行数据库验证,
// if(数据库存在并且密码正确){
//
// 将该用户存放在session中,
// 然后将用户名存放在arrayList中(通过application 获取),
// 在”。。。上线了!“存放在arrayList masg里面;(通过application获取);
//
// }else说明数据库验证失败{
//   在页面上输入失败信息,跳转到登陆页面
// }
// }
// }else if(访问的注册页面){
// 继续执行
//  }else if(访问的是注册页面的servlet){
// chain.doFilter(...,...);继续执行
//}else{
//获取session里面存在customer
//   Customer customer=(Customer)session.getAttribute("customer");在session中获取师傅存在customer;
// if(如果session不等于空){
//         chain.dofileter();继续执行。。
// }else{
// 重定向到注册页面。。
//}
// }
//
//
//
if(pageName.equals("login.jsp")&&!path.equals("/roomchat/logins/log.jsp")&&!path.equals("/roomchat/log")){
String action=request.getParameter("action");

if(action==null){
//System.out.println(action);
chain.doFilter(arg0, arg1);

}else{

String account=request.getParameter("account");
String password=request.getParameter("password");
//System.out.println(account+"  "+password);
CustomerDao cdao=new CustomerDao();
Customer customer=cdao.getCustomerByAccount(account);
//System.out.println("              "+action); 
//System.out.println(customer.getAccount());
if(customer.getAccount()!=null&&customer.getPassword().equals(password) ){
session.setAttribute("customer", customer);
response.sendRedirect("/roomchat/login");

}else{
out.println("用户名不存在!<a href='/roomchat/login.jsp'>点击会登陆页面</a>");

}
}
}else if(path.equals("/roomchat/logins/log.jsp")){

chain.doFilter(request, response);
}else if(path.equals("/roomchat/log")){
chain.doFilter(request,response);
}else{
Customer customer =(Customer)session.getAttribute("customer");
if(customer!=null){

chain.doFilter(request, response);
}else{
response.sendRedirect("/roomchat/login.jsp");
}
}
}

public void init(FilterConfig arg0) throws ServletException {
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值