最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
jsp中过滤器选择过滤器的写法详解
时间:2017-04-20 编辑:简简单单 来源:一聚教程网
代码如下 | 复制代码 |
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跳转到欢迎页面所用的跳转是客户端跳转。
-
上一个: java实现合并图片的方法示例
-
下一个: Java GC 机制与内存分配策略详解
相关文章
- SpringBoot测试配置属性与web启动环境解析 10-24
- vue中将el-switch值true、false改为number类型的1和0解析 10-24
- Vue中的路由配置项meta使用解读 10-24
- SpringBoot自定义bean绑定解析 10-24
- SpringBoot常用计量与bean属性校验和进制数据转换规则解析 10-24
- 工厂方法在Spring框架中的运用介绍 10-24