Use session in APC

xiaoxiao2021-03-06  108

1, use session

l As long as you get an HTTPSESSSION object, you can manage and use the session.

l In order to follow the MVC mode, the use of session should be implemented in Handler.

l In Handler, you can use the DispatchContext this context environment to get httpsession objects.

l The following is a tool class that encapsulates simple use of Session:

Package com.fujitsu.eframe.eftool;

Import javax.servlet.http.httpsession;

Import com.fujitsu.uji.dispatchContext;

Import com.fujitsu.uji.http.httpsessionprofile;

PUBLIC CLASS sessionUtil {

PRIVATE Static httpsession session;

Private static httpsession getsession (DispatchContext context) {

IF (session == null) {

Session = (httpsessionprofile) context.getationsProfile ()). getsession ();

}

Return session;

}

Public Static Object GetaTribute (DispatchContext Context, String Name) {

Return getsession (context) .GetaTribute (name);

}

Public Static Void SetAttribute (DispatchContext Context, String Name, Object Value) {

GetSession (Context) .SetaTRibute (name, value);

}

Public Static Void Settimeout (DispatchContext Context, Int Seconds) {

GetSession (Context) .SetMaxInactiveInterVal (Seconds);

}

}

l Here the most important thing is to use the API provided by the APC to get httpsession objects.

Ø Get the HttpSessionProfile object (forced conversion because sessionprofile is the abstract parent class of HttpSessionProfile)

Ø The httpsession object is obtained by the GetSession () method of HttpSessionProfile.

l Also note that the timeout time specified in the setTimeout () method is in seconds.

l This can use session in Handler, for example:

SessionUtil.SetaTribute (Context, "Workarea", Name;

2, monitor session

l You can implement the HTTPSessionListener interface to achieve the creation and destruction of the SESSION. The following is a simple framework:

Package com.fujitsu.eframe.eftool;

Import javax.servlet.http.httpsessionEvent;

Import javax.servlet.http.httpsessionListener;

Public Class SessionListener Implements HttpSessionListener {Public Void Sessioncreated (httpsessionever

System.out.println ("Session Created!");

}

Public void sessiondestroyed (httpsessionever se) {

System.out.println ("session destroyed!");

}

}

l HttpSessionListener interface provides sessioncreated () and sessionDestroyed () two methods, which are called before the session created and the session destroyed

l Therefore, you can do some of the initialization work created after SessionCreated () method; and do some SESSIONDESTROYED () methods to do some session destruction clear work

l To register your listener, you can configure it in Web.xml:

uji.handleexception.wrap

True

com.fujitsu.eframe.eftool.SessionListener

1

uji-taglib

/web-inf/ujiall.tld

l APC defaults using web-app_2_2.dtd, does not support tag, need to be changed to web-app_2_3.dtd

l Use the tag registration listener that needs to be listened

l The above example also uses the tag to specify the valid time of the default session, pay attention to the minute unit

转载请注明原文地址:https://www.9cbs.com/read-100408.html

New Post(0)