//在登录action中set属性
//获得一个新的session
HttpSession session = request.getSession();
session.setAttribute("seq", seq);//然后再Filter中判断Session中的属性
HttpSession session = request.getSession(false);//打印weblogic.servlet.internal.session.MemorySessionData@e03f50
System.out.println("request.getSession() = "+request.getSession());
//打印weblogic.servlet.internal.session.MemorySessionData@e03f50
System.out.println("request.getSession(false) = "+request.getSession(false));//打印null
System.out.println("session() = "+request.getSession().getAttribute("seq"));
//打印null
System.out.println("session(false) = "+request.getSession(false).getAttribute("seq"));
无语了。突然出现这情况。请教高手~
解决方案 »
- Uploadify与Struts结合时,同步上传多个文件
- how to solve this problem about JNDI?
- 紧急求源!!!
- 我在jbuilderX中是用了struts1.1,并用了资源文件,但是报错Cannot find message resources under key org.apache.struts.action.MESSAGE
- 请对ibatis结构了解的讨论一下动态问题
- struts + log4j的问题
- 求助:ejb 出错
- knight_qmh(辉) 进来,麻烦看一下
- telnet的程序
- 如果不使用web.xml文件,在学习struts2时怎么配置filter?
- struts1.3 多文件上传 用静态ActionForm 怎么实现
- MyBatis 3 如何清理一级缓存
如果session不存在,则2者都没有值。
request.getSession();相当于request.getSession(true);是没有会话对应session的话新建一个session出来,如果是request.getSession(false的话,没有的话返回null);
//然后再Filter中判断Session中的属性
HttpSession session = request.getSession(false);这个 session 看看呢 也许他是null?
如果没有session
request.getSession(); 会新建一个session
System.out.println("request.getSession(false) = "+request.getSession(false));放前面输出看看
你们说的我都知道。seq是从数据库查出来的。有值。刚打印完。下面是代码//登录Action
public ActionForward longinSystem(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
String staff_id = request.getParameter("loginName");//用户名
String staff_password = request.getParameter("loginPassword");//密码
System.out.println("用户名="+staff_id);
System.out.println("密 码="+staff_password); //获得一个新的session
HttpSession session = request.getSession(); if(isExist){
Map<String, String> map = imp.getUserMsg(staff_id, staff_password); String seq = map.get("staff_seq");
String name = map.get("staff_name");
String group_id = map.get("staff_group");
String group_name = map.get("group_name");
//seq不是
System.out.println("seq = "+seq);
session.setAttribute("seq", seq);
session.setAttribute("staff_name", name);
session.setAttribute("group_id", group_id);
session.setAttribute("group_name", group_name);
return mapping.findForward("loginSuccess");
}else{
request.setAttribute("msg", "用户名或密码错误.");
return mapping.findForward("loginFail");
}//Filter
public void doFilter(ServletRequest servRequest, ServletResponse servResponse,
FilterChain chain) throws IOException, ServletException {
boolean isLogin = true;
HttpServletRequest request = (HttpServletRequest) servRequest;
HttpServletResponse response = (HttpServletResponse) servResponse;
//设置统一编码
request.setCharacterEncoding("gb2312");
String uri = request.getRequestURI();
int index = uri.lastIndexOf("/");
String value = uri.substring(index + 1); System.out.println("PermitFilter ------------> mehtod is doFilter "+value);
if(!(value.equals(""))){
if(!(value.equals("index.jsp"))){
if(!(value.equals("top2.jsp"))){
if(!(value.equals("LeftCondition.jsp"))){
if(!(value.equals("control.htm"))){
if(!(value.equals("right.jsp"))){
if(!(value.equals("login.do"))){
HttpSession session = request.getSession(false);
System.out.println("request.getSession() = "+request.getSession());
System.out.println("request.getSession(false) = "+request.getSession(false));
System.out.println("session() = "+request.getSession().getAttribute("seq"));
System.out.println("session(false) = "+request.getSession(false).getAttribute("seq"));
if(session != null){
String seq = (String) session.getAttribute("seq");
if(seq == null){
isLogin = false;
}else{
isLogin = true;
}
}else{
isLogin = false;
}
}
}
}
}
}
}
} if(!(isLogin)){
StringBuffer url = new StringBuffer(124);
url.append("/index.jsp"); response.getWriter().print("<script>parent.window.location.href = 'index.jsp';</script>");
}else{
chain.doFilter(servRequest, response);
}}/*
request.getSession();等同于request.getSession(true);
如果是空则创建一个Session。request.getSession(false);
如果返回的Session是空则是null
*/
而在Filter中取的时候就是空了。并且我也明确指定了。当请求时login.do的时候不做过滤。