Sessions are cleared when a JSF page is redirected.

Source: Internet
Author: User

Use JSF During development, it is hard to avoid Session Scope Of Mbean For these Bean , We want it to be retained during operations on the same page and cleared during page Jump. This article introduces an automatic Session .

1.First, addSessionUsed to record the original pageUri,CodeAs follows:

<%

Session. setattribute ("pagefrom", request. getrequesturi ());

%>

2.AddFilter, Intercept allJSFSessionProcessing:Web. xmlAdd the following code:


sessionguard
sessionguard
net. moon. filter. sessionguardfilter

the key of sessions want to keep to next page, others
will be delete
keepsessionkeys
userid; pageid; COM. sun. faces. logicalviewmap; COM. sun. faces. application. statemanagerimpl. serialid; javax. faces. request. charset



sessionguard
*. JSF

Where,Com. Sun. Faces. logicalviewmap; com. Sun. Faces. application. statemanagerimpl. serialid; javax. Faces. Request. charsetIsJSFSaved statusSession, Must be retained.

3.WriteSessionguardfilterTo determine whether the request page is the current page. If notSessionThe Code is as follows:

Package net. Moon. filter;

Import java. Io. ioexception;
Import java. util. arraylist;
Import java. util. enumeration;
Import java. util. List;
Import java. util. stringtokenizer;

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. httpsession;


Public class sessionguardfilter implements filter {
Private list <string> keepsessionkeys = new arraylist <string> ();

Public void destroy (){
// Todo auto-generated method stub

}

@ Suppresswarnings ("unchecked ")
Public void dofilter (servletrequest request, servletresponse response,
Filterchain) throws ioexception, servletexception {
// Todo auto-generated method stub
Httpservletrequest hrequest = (httpservletrequest) request;
Httpsession session = hrequest. getsession ();

// Delete useless sessions when change page
If (! (Hrequest. getrequesturi (). Replace (". JSF", ". jsp"). Equals (Session
. Getattribute ("pagefrom ")))){
Enumeration <string> en = session. getattributenames ();
While (EN. hasmoreelements ()){
String key = en. nextelement ();
If (! Keepsessionkeys. Contains (key )){
Session. removeattribute (key );
}
}
}
}

Public void Init (filterconfig) throws servletexception {
// Todo auto-generated method stub
Stringtokenizer ST = new stringtokenizer (filterconfig
. Getinitparameter ("keepsessionkeys "),";");
While (St. hasmoretokens ()){
Keepsessionkeys. Add (St. nexttoken ());
}
}
}

 

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.