Many related examples are found on the internet, but most of them are dwr2 examples. The results are average. I studied dwr3.0 and made a demo to push content to one or more precise targets.
The focus is to add scriptsessionlistener to scriptsessionmanager to listen to the creation and destruction of scriptsession.
In scriptsessionlistener, there are two methods to be implemented: sessioncreated and sessiondestroyed, which correspond to the creation and destruction of scriptsession respectively. You can add the required content to the method.
I configured a servlet in Web. XML to enable auto-running listening.
Initscriptsession. Java
[Java]
View plaincopyprint?
- Import javax. servlet. genericservlet;
- Import javax. servlet. servletcontext;
- Import javax. servlet. servletrequest;
- Import javax. servlet. servletresponse;
- Import javax. servlet. http. httpsession;
- Import org. directwebremoting. container;
- Import org. directwebremoting. scriptsession;
- Import org. directwebremoting. servercontextfactory;
- Import org. directwebremoting. webcontextfactory;
- Import org. directwebremoting. event. scriptsessionevent;
- Import org. directwebremoting. event. scriptsessionlistener;
- Import org. directwebremoting. Extend. scriptsessionmanager;
- Public class initscriptsession extends genericservlet {
- Servletcontext application;
- Public void Init (){
- Container = servercontextfactory. Get (). getcontainer ();
- Scriptsessionmanager manager = container
- . Getbean (scriptsessionmanager. Class );
- Scriptsessionlistener listener = new scriptsessionlistener (){
- Public void sessioncreated (scriptsessionevent eV ){
- Httpsession session = webcontextfactory. Get (). getsession ();
- Scriptsession = eV. getsession ();
- String userid = session. getattribute ("userid"). tostring ();
- Scriptsession. setattribute ("userid", userid );
- System. Err. println ("create ---" + scriptsession. GETID () + "------- put userid into scriptsession"
- + Userid );
- }
- Public void sessiondestroyed (scriptsessionevent eV ){
- System. Out. println ("Destroy -----" + eV. getsession (). GETID ());
- }
- };
- Manager. addscriptsessionlistener (listener );
- }
- Public void Service (servletrequest req, servletresponse res ){
- Init ();
- }
- }
Import javax. servlet. genericservlet; <br/> Import javax. servlet. servletcontext; <br/> Import javax. servlet. servletrequest; <br/> Import javax. servlet. servletresponse; <br/> Import javax. servlet. HTTP. httpsession; </P> <p> Import Org. directwebremoting. container; <br/> Import Org. directwebremoting. scriptsession; <br/> Import Org. directwebremoting. servercontextfactory; <br/> Import Org. directwebremoting. webcontextfactory; <br/> Import Org. directwebremoting. event. scriptsessionevent; <br/> Import Org. directwebremoting. event. scriptsessionlistener; <br/> Import Org. directwebremoting. extend. scriptsessionmanager; </P> <p> public class initscriptsession extends genericservlet {</P> <p> servletcontext application; </P> <p> Public void Init () {<br/> Container = servercontextfactory. get (). getcontainer (); <br/> scriptsessionmanager = container <br/>. getbean (scriptsessionmanager. class); <br/> scriptsessionlistener listener = new scriptsessionlistener () {<br/> Public void sessioncreated (scriptsessionevent eV) {<br/> httpsession session = webcontextfactory. get (). getsession (); <br/> scriptsession = eV. getsession (); <br/> string userid = session. getattribute ("userid "). tostring (); <br/> scriptsession. setattribute ("userid", userid); <br/> system. err. println ("create ---" + scriptsession. GETID () + "------- put userid into scriptsession as" <br/> + userid); <br/>}</P> <p> Public void sessiondestroyed (scriptsessionevent eV) {<br/> system. out. println ("Destroy -----" + eV. getsession (). GETID (); <br/>}< br/>}; <br/> manager. addscriptsessionlistener (listener); <br/>}</P> <p> Public void Service (servletrequest req, servletresponse res) {<br/> Init (); <br/>}< br/>}
In the sessioncreated method, the ID of the current login user is put into the scriptsession. This ID is used to identify the push target during the subsequent push process.
Push method:
[Java]
View plaincopyprint?
- /**
- * Push to a specified user
- * @ Param userid
- * @ Param request
- * @ Return
- */
- Private void send (final string receiverid, final string MSG ){
- Browser. withallsessionsfiltered (New scriptsessionfilter (){
- Public Boolean match (scriptsession session ){
- If (session. getattribute ("userid") = NULL)
- Return false;
- Else
- Return (session. getattribute ("userid"). Equals (receiverid );
- }
- }, New runnable (){
- Public void run (){
- Collection <scriptsession> colls = browser. gettargetsessions ();
- For (scriptsession: colls ){
- Scriptsession. addscript (initfunctioncall (
- "DWR. util. setvalue", "info", MSG ));
- }
- }
- });
- }
/** <Br/> * push to the specified user <br/> * @ Param userid <br/> * @ Param request <br/> * @ return <br/> * /<br/> private void send (final string receiverid, final string MSG) {<br/> browser. withallsessionsfiltered (New scriptsessionfilter () {<br/> Public Boolean match (scriptsession session) {<br/> If (Session. getattribute ("userid") = NULL) <br/> return false; <br/> else <br/> return (Session. getattribute ("userid ")). equals (receiverid); <br/>}< br/>}, new runnable () {<br/> Public void run () {<br/> collection <scriptsession> colls = browser. gettargetsessions (); <br/> for (scriptsession: colls) {<br/> scriptsession. addscript (initfunctioncall (<br/> "DWR. util. setvalue "," info ", MSG); <br/>}< br/>}); <br/>}
Other methods are similar to those found on the Internet. If you need them, you can go to the Internet to study them.
The solution focuses on creating a new scriptsession every time the page is refreshed.
Because the creation mechanism of scriptsession is different from that of httpsession, it will be re-created every time the page is refreshed, And the destruction mechanism will be automatically destroyed only after the connection is lost or fails for a certain period of time, this may cause the server to save a lot of useless scriptsessions, so it will not only affect the performance, but more importantly, it may not implement the functions you want.
The solution is to call a DWR method on the page where you call DWR. Engine. setactivereverseajax (true.
DWR. Engine. setnotifyserveronpageunload (true );
The function of this method is to destroy the current scriptsession when the page is destroyed or refreshed. This ensures that no invalid scriptsession object exists in the scriptsession set obtained by the server.