• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

singletons & servlets...

 
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a question whether I'm approaching something with the correct philosophy..

In my web app I want to add a socket listener, so certain devices belonging to customers can connect via sockets rather than keep sending http requests as at present..

What I also want to do is allow them, when logged in to see that their device is in fact connected via a socket and let them do certain things..

My puzzle is the approach for the socket listener.. My plan was to have a SocketListener servlet which loads on startup and sits listening.. I suppose it doesnt have to specifical be a servlet but a plain old class, but I want to it to definately start when the webapp starts.. thats why I thought a servlet would be a suitable approach..
And as I said I need to be able access the class/servlet in a singleton fashion so I can check whats connected to it and other functions..

Has any thoughts they could share ?
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use a class implementing the ServletContextListener interface. It can be a regular class. Configure it in web.xml as:

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A better approach would be to use a ServletContextListener (some info here under "Lifecycle events"). Using servlets loaded st startup for this kind of thing is kind of an abuse of servlets.
 
Dave Brown
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, thats exactly what I need.

Dave
 
This is my favorite tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic