This is the content of a filter,
Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception {httpservletrequest Req = (httpservletrequest) request; incluresp = (response) response; string constring = ""; constring = req. getheader ("Referer"); // get the parent URL -- if it is not directly entered, it is the previously accessed page. If it is entered by the user, this parent URL does not exist if ("". equals (constring) | null = constring) {// if the previous directory is empty, it indicates that the string servletpath = req accessed by the user is directly input. getservletpath (); // the current request URL, removing the IF (servletpath) of several directly accessible pages. contains ("index. JSP ") | servletpath. contains ("admin/login. JSP ") {// skip index. JSP and log on to login. jspchain. dofilter (request, response);} else {resp. sendredirect ("/ejuornal/index. JSP "); // jump back to the homepage} else {chain. dofilter (request, response );}}
The following is the configuration file of the filter.
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>FilterPages</filter-name> <filter-class>com.ejuornal.filter.FilterPages</filter-class> </filter> <filter-mapping> <filter-name>FilterPages</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping></web-app>
At this time, when you directly enter the URL, it will jump back to the home page.
However, there are two issues that need attention. The usage of chain. dofilter (request, response); in the filter ---------- error is as follows:
Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception {httpservletrequest Req = (httpservletrequest) request; incluresp = (response) response; string constring = ""; constring = req. getheader ("Referer"); // obtain the parent urlif ("". equals (constring) | null = constring) {string servletpath = req. getservletpath (); // the current request urlif (servletpath. contains ("index. JSP ") | servletpath. contains ("admin/login. JSP ") {chain. dofilter (request, response);} else {resp. sendredirect ("/ejuornal/index. JSP ") ;}} chain. dofilter (request, response );}
If this is done, it will be as follows:
Two pages overlap because two chains. dofilter (request, response); are executed.
That is to say, executing chain. dofilter (request, response); is the execution of a request.