如何使用filter实现未登录用户自动跳转到登录页面篇1
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{HttpServletRequestreq=(HttpServletRequest)request;if(req.getSession().getAttribute("user")==null){req.setAttribute("error","请你先登录");//非法请求才会进到这里面,在这里保存请求的url地址,在成功登录后再进行跳转req.getSession().setAttribute("goUrl",req.getRequestURL()+"?"+req.getQueryString());request.getRequestDispatcher("/ulogin.jsp").forward(request,response);}else{chain.doFilter(request,response);}}上面的代码是过滤器中的代码下面的是servlet中的代码if(request.getSession().getAttribute("goUrl")!=null){Stringurl=(String)request.getSession().getAttribute("goUrl");response.sendRedirect(url);}else{response.sendRedirect("/webshopping/index.jsp");}。
java过滤器怎么写用的是SSH框架主要目的是防止有些比如登录不填篇2
在web.xml中配置过滤器
<filter>
<filter-name>encoding</filter-name>
<filter-class>;此处是自己已实现filter类的目录</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<url-pattern>;此处是要过滤的路径</url-pattern>
<filter-name>encoding</filter-name>
</filter-mapping>
怎么使用filter实现未登录用户自动跳转到登录页面篇3
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{
HttpServletRequestreq=(HttpServletRequest)request;
if(req.getSession().getAttribute("user")==null){
req.setAttribute("error","请你先登录");
//非法请求才会进到这里面,在这里保存请求的url地址,在成功登录后再进行跳转
req.getSession().setAttribute("goUrl",req.getRequestURL()+"?"+req.getQueryString());
request.getRequestDispatcher("/ulogin.jsp").forward(request,response);
}
else{
chain.doFilter(request,response);
}
}
上面的代码是过滤器中的代码
下面的是servlet中的代码
if(request.getSession().getAttribute("goUrl")!=null){
Stringurl=(String)request.getSession().getAttribute("goUrl");
response.sendRedirect(url);
}
else{
response.sendRedirect("/webshopping/index.jsp");
}
如何用Filter实现对注册用户是否登录的过滤篇4
下面是我从前写的项目里用的用户过滤。供你参考。你可以去上看看,那儿有过滤器的视频教程。
packagefilter;
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.;charset=GBK");
request.setCharacterEncoding("GBK");
PrintWriterout=response.getWriter();
HttpServletRequestreq=(HttpServletRequest)request;
HttpSessionsession=req.getSession();
//System.out.println(session.getAttribute("user"));
//user是用户登录后session.setAtrribute("user",***)中的user
if(session.getAttribute("user")!=null){
chain.doFilter(request,response);
}
else{//未登录,后退
out.println("<scriptlanguage='javascript'>alert('你还未登录');");
out.println("history.go(-1);</script>");
}
}
//过滤器销毁
publicvoiddestroy(){}
}
怎么使用filter实现未登录用户自动跳转到登录页面篇5
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{HttpServletRequestreq=(HttpServletRequest)request;if(req.getSession().getAttribute("user")==null){req.setAttribute("error","请你先登录");//非法请求才会进到这里面,在这里保存请求的url地址,在成功登录后再进行跳转req.getSession().setAttribute("goUrl",req.getRequestURL()+"?"+req.getQueryString());request.getRequestDispatcher("/ulogin.jsp").forward(request,response);}else{chain.doFilter(request,response);}}上面的代码是过滤器中的代码下面的是servlet中的代码if(request.getSession().getAttribute("goUrl")!=null){Stringurl=(String)request.getSession().getAttribute("goUrl");response.sendRedirect(url);}else{response.sendRedirect("/webshopping/index.jsp");}。