Using filter to prevent users from accessing some unauthorized resources, such as a user is not logged in to not allow access to certain pages of the site, and to redirect the page to the user needs to log on the page, the following is a related example:
package com.drp.util.filter;
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.HttpServletRequest;
import Javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AuthFilter implements Filter {
public void Destroy () {
}
public void Dofilter (ServletRequest servletrequest, Servletresponse servletresponse,
Filterchain Filterchain) throws IOException, servletexception {
The first parameter of the/** 1,dofilter method is the ServletRequest object. This object provides the filter with information about the entry, including
Full access to
* form data, cookies, and HTTP request headers. The second parameter is Servletresponse, usually in the simple
This parameter is omitted from the
* filter. The last parameter is Filterchain, which is used to invoke a servlet or JSP page.
*/
httpservletrequest request = (httpservletrequest) servletrequest;
/** If HTTP requests are processed and need to be accessed in servletrequest, such as GetHeader or GetCookies
* The method that cannot be obtained, must construct this request object as HttpServletRequest
*/
httpservletresponse response = (httpservletresponse) servletresponse;
String Currenturl = Request.getrequesturi (); Gets the absolute path of the root directory:
String targeturl = currenturl.substring (Currenturl.indexof ("/", 1),
currenturl.length ()); Intercepts to the current file name for comparison
HttpSession session = Request.getsession (false);
if (!) /login.jsp ". Equals (TargetUrl)) {
//To determine whether the current page is redirected after the login page page, if it is not a session to judge, to prevent the death cycle
If session = NULL | | Session.getattribute ("user") = = null) {
//* User should manually add session after login
System.out.println ("request.getcontextpath () =" + Request.getcontextpath ());
Response.sendredirect (Request.getcontextpath () + "/login.jsp");
///If the session is empty indicates that the user is redirected to the login.jsp page without a login
return;
}
}
//Add filter chain to continue down execution
Filterchain.dofilter (request, response);
/** invokes the Dofilter method of the Filterchain object. The Dofilter method of filter interface takes a Filterchain object as
* is one of its parameters. Activates the next associated filter when calling the Dofilter method of this object. If there is no other
* A filter is associated with a servlet or JSP page, the servlet or JSP page is activated.
*/
}
public void init (Filterconfig filterconfig) throws Servletexception {
}
}
Then add in the configuration file Web.xml: (Note that filtering was first introduced in the Serlvet Specification version 2.3.) Therefore, the Web.xml file must use more than 2.3 versions of the DTD. )
<filter>
<filter-name>AuthFilter</filter-name>
<filter-class>com.drp.util.filter.AuthFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthFilter</filter-name>
<url-pattern>*.jsp</url-pattern>//表示对所有jsp文件有效
</filter-mapping>
This will go to the login page if the user is not logged in.