Block JSP pages and verify that they are logged in
Recently using STRUTS2 to do projects, you need to intercept the JSP to verify that it has been logged, but the STRUTS2 filter chain can only filter action. Later found in the web.xml can directly increase the Javax.servlet.Filter implementation class filter, you can filter JSP. Please look at the code
Filter Implementation Class
<span style= "FONT-SIZE:18PX;"
>package Com.common;
Import java.io.IOException;
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.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Loginfilter extends HttpServlet implements Filter {private static final long serialversionuid = 1L; @Override public void Dofilter (ServletRequest request, servletresponse response, filterchain chain) th
Rows IOException, servletexception {httpservletrequest req = (httpservletrequest) request;
HttpServletResponse res = (httpservletresponse) response;
String path = Req.getcontextpath (); String Indexpath = req.getscheme () + "://" +req.getservername () + ": +req.getserverport () +path+"/pages/login.JSP ";
if (Req.getrequesturi (). EndsWith ("login.jsp")) {Chain.dofilter (request, response);
Return Object Loginuser = Req.getsession (). getattribute ("<strong><span style=" color: #ff0000; "
>loginSession</span></strong> ");
if (Loginuser = = null) {res.sendredirect (Indexpath);
Return
Chain.dofilter (request, response); @Override public void init (Filterconfig arg0) throws Servletexception {//TODO auto-generated method s
Tub}} </span>
Login method:
<span style= "FONT-SIZE:18PX;" > Public String Login ()
{
Login login = new login ();
//....... Verify login information
actioncontext CTX = Actioncontext.getcontext ();
Map<string, object> session = Ctx.getsession ();
Session.put ("<strong><span style=" color: #ff0000; " >loginSession</span></strong> ", login);
Return "Success";
} </span>
Web.xml Configuration
<filter>
<filter-name>setlog</filter-name>
<filter-class> com.common.loginfilter</filter-class>
</filter>
<filter-mapping>
<filter-name >setlog</filter-name>
<url-pattern>/pages/home.jsp</url-pattern>
<url-pattern >/pages/channel/*</url-pattern>
<url-pattern>/pages/content/*</url-pattern>
< Url-pattern>/pages/product/*</url-pattern>
<url-pattern>/pages/vodtask/*</url-pattern >
</filter-mapping>
Note that if a frame is used on a page, it may cause the session timeout to exit to the login page, at which point the login page appears in a child page of the frame, and the solution is to add the following code to the login page.
<span style= "FONT-SIZE:18PX;" > <script language= "JavaScript" >
if (Top!= window)
top.location.href = Window.location.href ;
</script></span>
Thank you for reading, I hope to help you, thank you for your support for this site!