日期:2014-05-18  浏览次数:20571 次

判断是否登录并跳转
在header.jsp里包含以下代码:
              <%
                if(request.getSession(false)==null){
                response.sendRedirect( "/index.jsp ");
                }
%>
在其它页面里Include这个页面,但为什么总是不能实现跳转?
好像没有什么错误吧?
用段也不行:
<%                 String   index   =   "index.jsp ";
                    int   utype   =   Integer.parseInt(session.getAttribute( "utype ").toString());
                   
                    if(utype   ==   0){
                    index   =   "admin.jsp ";
                      }else   if   (utype   ==   1){
                    index   =   "teacher.jsp ";
                      }else   if   (utype   ==   2){
                    index   =   "student.jsp ";
                      }else{
                      response.sendRedirect( "/index.jsp ");
                      }
%>
是跳转有问题还是Session的判断有问题?

------解决方案--------------------
跳转的路径有没有问题?
------解决方案--------------------
if(request.getSession( "utype ")==null){
response.sendRedirect( "/index.jsp ");
}

------解决方案--------------------
==========从网上找到这个==================
request.getSession() 方法介绍
HttpRequest对象有两种形式的getSession方法调用:
一个是getSession(),
另一个是getSession(boolean isNew)

这样的,前者会检测当前时候有session存在,如果不存在则创建一个,如果存在就返回当前的。
getSession()相当于getSession(false),

getSession(true)则不管当前是否存在Session都创建一个。
=====================
所以getSession(false)始终都是存在的。始终不等于null


<% String index = "index.jsp ";
//int utype = Integer.parseInt(session.getAttribute( "utype ").toString());

if(utype == 0){
index = "admin.jsp ";
}else if (utype == 1){
index = "teacher.jsp ";
}else if (utype == 2){
index = "student.jsp ";
}else{
index = "index.jsp ";

response.sendRedirect(index);
}
%>

------解决方案--------------------
这样写:
if(request.getSession().getAttribute( "username ")==null){
response.sendRedirect( "/index.jsp ");
------解决方案--------------------
你换成js的试试 看看跳转有错没??
[code]
if((String)session.getAttribute(false)==null)
location = "index.jsp "