Login filter class;
Public class loginfilter implements filter {
Private filterconfig config;
Private string logon_page;
Private string home_page;
Public void destroy (){
Config = NULL;
}
Public void Init (filterconfig) throws servletexception {
// Retrieve the URI of the logon page and homepage from the deployment descriptor
Config = filterconfig;
Logon_page = filterconfig. getinitparameter ("logon_uri ");
Home_page = filterconfig. getinitparameter ("home_uri ");
If (null = logon_page | null = home_page ){
Throw new servletexception ("Logon page or homepage not found ");
}
}
@ Override
Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain) throws ioexception, servletexception {
Httpservletrequest Req = (httpservletrequest) request;
Httpservletresponse RSp = (httpservletresponse) response;
Login Login = (LOGIN) Req. getsession (). getattribute ("login ");
String request_uri = Req. getrequesturi (). touppercase (); // obtain the URI requested by the user
System. Out. println (request_uri );
String ctxpath = Req. getcontextpath (); // obtain the context path of the Web application.
String uri = request_uri.substring (ctxpath. Length (); // remove the context path and obtain the path of the remaining part.
Try {
If (! Request_uri.equals ("/leamedcall/login") & login = NULL ){
System. out. println ("***********************************");
RSP. sendredirect (home_page + logon_page );
// Printwriter out = RSP. getwriter ();
// Out. println ("<SCRIPT type = 'text/JavaScript '> window. Parent. Location ='" + home_page + logon_page + "'</SCRIPT> ");
System. Out. println (home_page + logon_page );
Return;
}
Else {
Chain. dofilter (request, response );
}
} Catch (ioexception e ){
} Catch (servletexception E1 ){
}
}
}
Web. xml configuration
<Filter>
<Filter-Name> loginfilter </filter-Name>
<Filter-class> filter. loginfilter </filter-class>
<Init-param>
<Param-Name> logon_uri </param-Name>
<Param-value>/login. jsp </param-value>
</Init-param>
<Init-param>
<Param-Name> home_uri </param-Name>
<Param-value>/leamedcall </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> loginfilter </filter-Name>
<URL-pattern>/admin/* </url-pattern>
</Filter-mapping>
If the frame framework is used, jump to the separate page login. jsp
Add
<SCRIPT type = "text/JavaScript">
If (top! = Self ){
If (top. location! = Self. Location)
Top. Location = self. location;
}
</SCRIPT>