Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29951024
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2008-04-24 09:06:59

如果用户直接输入了地址,不也可以直接访问吗?理论上是,我们可以加入session进行跟踪,以杜绝此类型事件发生,我们是不是要把每次对session的判断依次拷到每个页里呢,之后下次需要验证的SESSION换了,我们再换?太浪费了,我的做法是做了一个自定义标签,来解决这个问题。

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class CheckTag extends TagSupport
{
    private static final long serialVersionUID = 879137944441282895L;
    private String check = "";//用来验证的变量
    private String url = "index.jsp";//出现错误要去的页面
    private String msg = "";//错误的提示
    private String scope = "";//要严整变量的范围
    private String to = "go";
//如果验证失败,是将页面后退,还是定位到哪里?

    public String getTo()
    {
        return to;
    }

    public void setTo( String to )
    {
        this.to = to;
    }

    public String getMsg()
    {
        return msg;
    }

    public void setMsg( String msg )
    {
        this.msg = msg;
    }

    public String getScope()
    {
        return scope;
    }

    public void setScope( String scope )
    {
        this.scope = scope;
    }

    public String getCheck()
    {
        return check;
    }

    public void setCheck( String check )
    {
        this.check = check;
    }

    public String getUrl()
    {
        return url;
    }

    public void setUrl( String url )
    {
        this.url = url;
    }

    public int doStsrtTag() throws JspException
    {
        return SKIP_BODY;
    }

    public int doEndTag() throws JspException
    {
        boolean valid = false;//先设为不可用
        if ( scope.equalsIgnoreCase( "request" ) )//如果要检查request范围
        {
            valid = CheckUtil.checkRequestAttribute( pageContext.getRequest(),
                    check );
        }
        else if ( scope.equalsIgnoreCase( "session" ) )
        {
            valid = CheckUtil.checkSession( pageContext.getSession(), check );
        }
        else if ( scope.equalsIgnoreCase( "parameter" ) )
        {
            valid = CheckUtil.checkParameter( pageContext.getRequest(), check );
        }
        else if ( scope.equalsIgnoreCase( "application" ) )
        {
            valid = CheckUtil.checkApp( pageContext.getServletContext(), check );
        }
        if ( valid ) return EVAL_PAGE;//如果可用就继续执行此页的其余部分
        else
        {//否则,哈哈
            try
            {
                if ( to.equalsIgnoreCase( "go" ) ) //现在失败了,就看怎么回到你该到的地方
                    HtmlUtil.callParentGo(
                        pageContext.getOut(), msg, url );//将浏览器定位到URL 
                else
                    HtmlUtil.callBack( pageContext.getOut(), msg );//后退一下页面来阻止
                return SKIP_PAGE;//跳过页面的其余部分,不执行
            }
            catch ( Exception e )
            {
                throw new JspException( e.toString() );
            }
        }
    }

    public void release()
    {
        super.release();
        check = "";
        url = "";
        msg = "";
        scope = "";
    }
}


下面是用到的htmlUtil部分:

public static void callParentGo( Writer out, String msg, String url )
            throws IOException
    {
        out.write( "" );
    }
public static void callBack( Writer out, String msg ) throws IOException
    {
        out.write( "" );
    }


写个check.tld部署吧,



 1.0
 1.1
 
  check
  com.boya.subject.util.CheckTag
  
   check
   true
  

  
   url
   false
  

  
   msg
   true
  

  
   scope
   true
  

  
   to
   false
  

 




只要在每个页面里写下这个就可以判定用户是否登陆了

<%@ taglib prefix="boya" uri="/WEB-INF/check.tld" %>


如果没有登陆那么,会自动提示到首页登陆

阅读(2133) | 评论(0) | 转发(0) |
0

上一篇:struts异常

下一篇:java 计算时间差

给主人留下些什么吧!~~