45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 编程代码 > 阅读资讯:怎么样在jsp中选择过滤器?

怎么样在jsp中选择过滤器?

2017-06-19 16:09:00 来源:www.45fan.com 【

怎么样在jsp中选择过滤器?

实例如下:

public class LoginCheckFilter implements Filter {


private FilterConfig config=null;
private String webroot=null;
public void destroy() {
// TODO Auto-generated method stub


}


@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest request=(HttpServletRequest)req;
HttpServletResponse response=(HttpServletResponse)resp;
HttpSession session=request.getSession(false); //取得当前会话的session,没有session也不创建session,同request.getSession()一样


String url=request.getRequestURI(); //获取当前输入的 /项目/当前访问的路径 与我们定义的路径是否相同


if(url!=null&&url.equals(webroot+"/login.jsp")&&url.equals(webroot+"/LoginServlet")){

chain.doFilter(req,resp);
}else{

if(session==null){
response.sendRedirect(webroot+"/login.jsp");

}else{

String user2=(String)session.getAttribute("user1");
if(user2==null){

response.sendRedirect(webroot+"/login.jsp");

}else{

chain.doFilter(request, response);

}
}

}

}


@Override
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
this.config=config;
ServletContext ctx=config.getServletContext(); //获取url /项目名
webroot=ctx.getContextPath();  //项目名


}
}

这里需要注意的是LoginServlet跳转到欢迎页面所用的跳转是客户端跳转。

以上这篇jsp中过滤器选择过滤器的写法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持路饭。


本文地址:http://www.45fan.com/bcdm/88907.html
Tags: 选择 JSP 过滤器
编辑:路饭网
推广内容
推荐阅读
热门推荐
推荐文章
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部