How to automatically jump to the page to be accessed before logon after Logon

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.