1 packagesanglp.servlet;2 3 Importjavax.servlet.*;4 Importjavax.servlet.annotation.WebFilter;5 Importjavax.servlet.annotation.WebInitParam;6 Importjavax.servlet.http.HttpServletRequest;7 Importjavax.servlet.http.HttpSession;8 Importjava.io.IOException;9 Ten one a /** - * Created by Administrator on 2016/10/5. - */ the@WebFilter (filtername = "authority", Urlpatterns = {"/*"},initparams = { -@WebInitParam (name = "encoding", value = "UTF-8"), -@WebInitParam (name = "loginpage", value = "/chap2/login.jsp"), -@WebInitParam (name = "prologin", value= "/chap2/prologin.jsp") + }) - public classAuthorityfilterImplementsFilter { + //configuration information for accessing the filter a Privatefilterconfig config; at - @Override - public voidInit (filterconfig Filterconfig)throwsservletexception { - this. config=filterconfig; - } - in @Override - public voidDestroy () { to this. config=NULL; + } - the @Override * public voidDoFilter (servletrequest servletrequest, servletresponse servletresponse, filterchain Filterchain)throwsioexception, servletexception { $ //gets the configuration parameters for the filterPanax NotoginsengString Encoding=config.getinitparameter ("encoding"); -String Loginpage=config.getinitparameter ("loginpage"); theString Prologin=config.getinitparameter ("prologin"); + //set the character set for request encoding a servletrequest.setcharacterencoding (encoding); theHttpServletRequest hreq=(httpservletrequest) servletrequest; +HttpSession Session=hreq.getsession (true); - //get a page for a customer request $String requestpath=Hreq.getservletpath (); $ //if the user of the session range is null, no Dengli is Indicated. And the user requested is neither the login page nor the page that handles the login - if(session.getattribute ("user") = =NULL&&!requestpath.endswith (loginpage) &&!Requestpath.endswith (PROLOGIN)) { -Servletrequest.setattribute ("tip", "you are not logged in"); the Servletrequest.getrequestdispatcher (loginpage). forward (servletrequest,servletresponse); - Wuyi}Else{ the //Fulfillment Request - Filterchain.dofilter (servletrequest,servletresponse); wu } - } about}
"java EE Enterprise Application Combat Learning record" Authorityfilter