如何做登录后自动跳转到登录前要访问的页面

http://blog.csdn.net/zrwlc2008/archive/2007/07/19/1698981.aspx

import  java.io.IOException;
import  java.net.URLEncoder;

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

import  com.ww.golf.bo.Player;
import  com.ww.golf.util.ActionUtils;

public   class  LogFilter  implements  Filter {

    
protected  FilterConfig filterConfig;

    
public   void  init(FilterConfig config)  throws  ServletException {
        
this .filterConfig  =  config;
    }

    
public   void  doFilter(ServletRequest srequest, ServletResponse sresponse,
            FilterChain chain) 
throws  IOException, ServletException {
        HttpServletRequest request 
=  (HttpServletRequest) srequest;
        HttpServletResponse response 
=  (HttpServletResponse) sresponse;

        Player player 
=  (Player) request.getSession().getAttribute(
                ActionUtils.PLAYER_OBJ_KEY);
        
if  (player  ==   null ) {
            StringBuffer sUrl 
=   new  StringBuffer( "" );
            
if  (request.getRequestURI()  !=   null ) {
                sUrl
                        .append(URLEncoder.encode(request.getRequestURI(),
                                
" UTF-8 " ));
            }
            
if  (request.getQueryString()  !=   null ) {
                sUrl.append(
" ? " );
                sUrl.append(URLEncoder
                        .encode(request.getQueryString(), 
" UTF-8 " ));
            }

            System.out.println(
" URL: "   +  sUrl);
            response.sendRedirect(
" ../index.jsp? "   +  ActionUtils.GO_URI_KEY
                    
+   " = "   +  sUrl.toString());
        } 
else  {
            chain.doFilter(request, response);
        }

    }

    
public   void  setFilterConfig( final  FilterConfig filterConfig) {
        
this .filterConfig  =  filterConfig;
    }

    
public   void  destroy() {
        
this .filterConfig  =   null ;
    }
}

 

        再于应用的web.xml中配置这个Filter

< filter >
        
< filter-name > logf </ filter-name >
        
< filter-class > com.ww.golf.filter.LogFilter </ filter-class >

    
</ filter >
< filter-mapping >
        
< filter-name > logf </ filter-name >
        
< url-pattern > /player/* </ url-pattern >
    
</ filter-mapping >

   这个代码的功能就是说:当访问 localhost:8080 / test / player / 目录里的页面(假设是登陆后显示的所有页面), 会经过Filter , 然后验证session , 如果未登陆或超时,,就回到登陆页面,,并把要访问的路径(比如 / player /  userinfo.jsp?id=111 ) 发到登陆页面, 登陆页面用一个hidden标签记录下来, 并同用户名和密码一起提交到struts的Action 。验证通过后, Action 就直接跳到访问路径userinfo.jsp 。

     写这个代码的时候,request.getRequestURI 只能获取不带参数的请求,如"userinfo.jsp?id=111"只能得到"userinfo.jsp" ,参数就丢了,等自动跳转了再执行是会报错的。后来查了一些文章才想起 request.getQueryString()就是用来获取请求中参数的。两者得一起用。哎,真是“基础不牢,越学越忘”。有功夫还得好好找点基础资 料来看了。

阅读更多
上一篇登陆后自动跳转到登录前页面
下一篇请教字符串匹配的高效算法
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭