使用java中的会话过滤的逻辑 [英] Logic of Filter using sessions in java

查看:57
本文介绍了使用java中的会话过滤的逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我我的逻辑有什么问题?在这一行之后传递零点的所有方法,

Can anyone tell me whatz wrong with my logic? It all ways passing null point after this line,

System.out.println("Inside the filter.............."  );

和home.jsp(索引页面)没有显示。在这里,我想过滤每个网址,只进行有效的登录用户请求。在这里我的逻辑..



FilterRequest.java



and home.jsp(index page) did not display. here I want to filter every url and proceed only valid login users requests. here my logic..

FilterRequest.java

try{
         HttpServletRequest request = (HttpServletRequest) req;
             HttpServletResponse response = (HttpServletResponse) resp;
             System.out.println("Inside the filter.............."  );
         HttpSession session = request.getSession();
         User u = null;
         if(session.getAttribute("loggedUser")!=null){
             u = (User) session.getAttribute("loggedUser");
         }
         if (u!= null)
         {
             System.out.println("user does exits.." + u.getUname() );
             chain.doFilter(req, resp);
         }else{
             System.out.println("user does exits..");
             String message = "Please Login!";
             req.setAttribute("loginMsg", message);
             //response.sendRedirect("login2.jsp");
         }
     }catch(Exception e){
         e.printStackTrace();
     }





web.xml





web.xml

<filter>
        <filter-name>FilterRequest</filter-name>
        <filter-class>com.mobitel.bankdemo.web.FilterRequest</filter-class>
  </filter>
  <filter-mapping>
        <filter-name>FilterRequest</filter-name>
        <url-pattern>/*</url-pattern>
  </filter-mapping>





谢谢!



Thank you!

推荐答案

问题在这里

The problem is here
HttpSession session = request.getSession();
		  User u = null;
		  if(session.getAttribute("loggedUser")!=null){





您正在查看任何现有会话的请求。所以它第一次会返回null。

你做 null.getAttribute()。这就是你得到例外的原因。


试试这个



you are looking in the request for any existing session. so for the first time it will return null.
And your doing the null.getAttribute(). that is the reason you get the exception.

try this

HttpSession session = request.getSession(true);





首先检查是否有任何会话然后返回,否则创建一个并返回。



It first check if any session exist then return it otherwise create one and return.


这篇关于使用java中的会话过滤的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆