跳到登陆后当前的页面

登录后跳转到原页面(servlet)

描述 :有些web项目中的网页需要用户登录后才可以访问,当用户未登录时访问该页面则跳转到登录页面,等用户输入用户名,密码登录成功后再跳转到之前用户需要访问的页面。

这里使用servlet实现访问控制。

第一步:.当未登录用户访问某个需要登录的功能页面时,首先会跳转到servlet,在方法前放置案例代码如下

//需要登录设置
        String userId=(String)session.getAttribute("userid");
        if(userId==null)
        {
            if (null!=request.getQueryString())
            {
                session.setAttribute("redirectUrl", 

request.getRequestURL().append("?").append

(request.getQueryString()).toString());
            }
            else 
            {
                session.setAttribute("redirectUrl", 

request.getRequestURL().toString());
            }
            response.sendRedirect

("user/cy/login.jsp");
        }
        
        //再次获得userId,判断是否成功登录
        userId=(String)session.getAttribute("userid");
      if(userId!=null){ ...  }

第二步:登录跳转到的servlet方法里需要放下如下的类似代码
//登录成功后跳回原页面
String redirectUrl=(String)
request.getSession().getAttribute( "redirectUrl");
                if (redirectUrl!=null){//判断是否有跳回的操作
                    //清除session中需要调转到的页面
                    System.out.println("返回");
                    session.removeAttribute( "redirectUrl");
                    response.sendRedirect(redirectUrl);//跳回到redirectUrl
                }
                else
                {
                    //不需要跳回就默认跳到首页
                    response.sendRedirect("user/cy/home.jsp");
                }














应用场景:一般网页游客和登录用户看到的内容是有区别的,如果一个未登录的用户在看到登录提示后跳转到登录界面登录,那么登录成功后怎么返回到该页面呢?

写这篇博客是因为我自己把解决问题的方向想偏了,本来实现是很简单的....为了避免再走弯路吧

假设用户在 www.example.com/a.html 看到登录提示,然后点击登录跳转到 www.example.com/login.html,登录界面使用ajax验证用户登录信息,当返回信息为成功时,在回调函数里要做这些逻辑的处理:

1.判断document.referrer是否为空,若为空本页面就不是从其它页面跳转过来的,就将页面跳转至网站首页

2.若document.referrer不为空,则需要判断前一个页面是否是本站点的页面,以免跳到其它站点去了,如果是其它站点则跳转至首页;

3.若document.referrer不为空且为本站点页面,则需要跳转至该页面

我想偏的地方就是第三步,一开始我用的 window.history.back(); 回到上一个页面,这样的操作相当于点击了浏览器的后退按键,原来的页面是没有得到刷新的,页面的内容完全来自于浏览器缓存,然后我想到用cookie,设置document.cookie =  'reload=true',然后在一个每个页面都包含的js里去读cookie,判断cookie中是否有reload=true,如果有则删除该属性,并使用location.reload(); 重新加载页面,看上去没什么问题,可是百度了各种代码去删除cookie中的reload字段的值都没有成功,也试过敲了一遍《JavaScript高级程序设计(第3版)》的cookie删除代码,仍然不见效果,想来这操作cookies也是件蛋疼的事。

删除cookie始终搞不定让我不得不想想是否还有其它方法来实现这个效果,然后想location.href是否可以实现重新加载呢,试了一下location.href果然会重新加载新URL所指向的页面。《JavaScript高级程序设计(第3版)》中有一句"每次修改location的属性(hash除外),页面都会以新URL重新加载”....另外使用location.href = URL  的效果与使用location.assign(URL) 的效果是完全一样的,因为location.href = URL的方式最终以URL值调用assign()方法!

location.assign() 方法加载新的文档。

window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面。

因此在登录成功的回调函数里执行下列操作就可以实现登录跳回刷新页面了

[html]  view plain  copy
  1. var prevLink = document.referrer;  
  2. if($.trim(prevLink)==''){  
  3.     location.href = 'www.example.com/index.html';  
  4. }else{  
  5.     if(prevLink.indexOf('www.example.com')==-1){    //来自其它站点  
  6.         location.href = 'www.example.com/index.html';  
  7.     }  
  8.     if(prevLink.indexOf('register.html')!=-1){      //来自注册页面  
  9.         location.href = 'www.example.com/index.html';  
  10.     }  
  11.     location.href = prevLink;  
























思路 

get

get enurl()带过去参数,再登录页面hidden表单提交,登录成功后得到的post值再跳转


post

enurl()hidden表单提交,登录成功后得到的post值再跳转

[php]  view plain  copy
  1. //渲染登录页  
  2.     public function login_y()  
  3.     {  
  4.         //$this->display('User/pczzc');  
  5.         if(is_mobile()){  
  6.             header("Location:http://wap.xxxx.com/login_page.html");  
  7.         }  
  8.         $refererUrl = urldecode(I('get.refererUrl',U('Index/index')));  
  9.         if (empty($refererUrl)) {  
  10.             $refererUrl = $_SERVER['HTTP_REFERER'];  
  11.         }  
  12.         if (strpos($refererUrl'login') || strpos($refererUrl'regist')) {  
  13.             $refererUrl = "http://" . $_SERVER['HTTP_HOST'];  
  14.         }  
  15.         $this->assign('refererUrl'$refererUrl);  
  16.         $this->display('User/login');  
  17.     }  
  18.   
  19.   
  20.   
  21.     /** 
  22.      * 用户登录 
  23.      */  
  24.     public function login()  
  25.     {  
  26.         if (IS_GET) {  
  27.             $this->display();  
  28.         }  
  29.         if ($_POST) {  
  30.             $account = I('post.account');  
  31.             $password = I('post.password');  
  32.             $auto = I('post.auto');  
  33.             $refererUrl = I('post.refererUrl');  
  34.         }  
  35.         if(!$account || !$password){  
  36.             $this->error('请输入账号或密码');  
  37.         }  
  38.         $object = D('User');  
  39.         $user_info = $object->getLoginUserInfo($account$password);  
  40.         $user_info = $user_info[0];  
  41.         if (empty($user_info)) {  
  42.             $this->error('用户名错误');  
  43. //              echo "<script>alert('用户名错误');location.href='login_y';</script>";  
  44.         }  
  45.         $verify_password = encode_password($password$user_info['salt']);  
  46.         // echo $verify_password;die;  
  47.         if ($verify_password != $user_info['password']) {  
  48.             $this->error('密码错误');  
  49. //              echo "<script>alert('用户名错误');location.href='login_y';</script>";  
  50.         }  
  51.   
  52.         $avatar = !empty($user_info['largeAvatar']) ? get_avatar_path($user_info['largeAvatar']) : 'http://v.xxx.com/assets/img/default/avatar026827c4.png';  
  53.         //成功页  
  54.         $uid = encrypt($user_info[id]);  
  55.         if ($user_info['nickname']) {  
  56.             $account = encrypt($user_info['nickname']);  
  57.         }  
  58.         // 兼容ie  
  59.         $_COOKIE["uid"] = $uid;  
  60.         $_COOKIE["account"] = $account;  
  61.         $_COOKIE["avatar"] = $avatar;  
  62.         // cookie('uid', $uid);  
  63.         // cookie('account', $account);  
  64.         // cookie('avatar', $avatar);  
  65.   
  66.         // if ($auto == 'on') {  
  67.             cookie('uid'$uid, 30 * 24 * 3600);  
  68.             cookie('account'$account, 30 * 24 * 3600);  
  69.             cookie('avatar'$avatar, 30 * 24 * 3600);  
  70.         // }  
  71.         /** 
  72.          * 登录成功更新时间操作 
  73.          */  
  74.         $data = [  
  75.             'id' => decrypt($uid),  
  76.             'loginTime' => time(),  
  77.             'loginIp' => GetIP()  
  78.         ];  
  79.         M('user')->data($data)->save();  
  80.             header("Location: $refererUrl");  
  81.     }  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值