Intercept JSP pages, verify that you have logged in and implement code _java

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.