Http://blog.csdn.net/zrwlc2008/archive/2007/07/19/1698981.aspximport
Java. Io. ioexception;
Import
Java.net. urlencoder;
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
Com. ww. Golf. Bo. player;
Import
Com. ww. Golf. util. actionutils;
Public
Class
Logfilter
Implements
Filter {
Protected
Filterconfig;
Public
Void
Init (filterconfig config)
Throws
Servletexception {
This
. Filterconfig
=
Config;
}
Public
Void
Dofilter (servletrequest srequest, servletresponse sresponse,
Filterchain chain)
Throws
Ioexception, servletexception {
Httpservletrequest request
=
(Httpservletrequest) srequest;
Httpservletresponse response
=
(Httpservletresponse) sresponse;
Player player
=
(Player) request. getsession (). getattribute (
Actionutils. player_obj_key );
If
(Player
=
Null
){
Stringbuffer Surl
=
New
Stringbuffer (
""
);
If
(Request. getrequesturi ()
! =
Null
){
Surl
. Append (urlencoder. encode (request. getrequesturi (),
"
UTF-8
"
));
}
If
(Request. getquerystring ()
! =
Null
){
Surl. append (
"
?
"
);
Surl. append (urlencoder
. Encode (request. getquerystring (),
"
UTF-8
"
));
}
System. Out. println (
"
URL:
"
+
Surl );
Response. sendredirect (
"
.../Index. jsp?
"
+
Actionutils. go_uri_key
+
"
=
"
+
Surl. tostring ());
}
Else
{
Chain. dofilter (request, response );
}
}
Public
Void
Setfilterconfig (
Final
Filterconfig ){
This
. Filterconfig
=
Filterconfig;
}
Public
Void
Destroy (){
This
. Filterconfig
=
Null
;
}
}
Configure the filter in the web. xml file of the application.
<
Filter
>
<
Filter-name
>
Logf
</
Filter-name
>
<
Filter-class
>
Com. ww. Golf. Filter. logfilter
</
Filter-class
>
</
Filter
>
<
Filter-Mapping
>
<
Filter-name
>
Logf
</
Filter-name
>
<
URL-Pattern
>
/Player /*
</
URL-Pattern
>
</
Filter-Mapping
>
The function of this Code is: when accessing localhost: 8080/test/player/
The pages in the directory (assuming all the pages displayed after login) will go through the filter and then verify the session,
If you have not logged on or timed out, return to the login page and set the path to be accessed (such as/player/userinfo. jsp? Id = 111) sent to the login page,
The login page is recorded with a hidden tag and submitted to the struts action together with the username and password. After the verification is passed, Action
Directly jump to the access path userinfo. jsp.
When writing this code, request. getrequesturi
Only requests without parameters can be obtained, such as "userinfo. jsp? Id = 111 "only" userinfo. jsp "can be obtained"
The parameter is lost, and an error is reported when the automatic jump is performed. I found some articles to remember.
Request. getquerystring () is used to obtain parameters in the request. Both are available. Ah, it's really "the foundation is poor, the more you learn, the more you forget ". You have to find some basic resources.
Expected.