How do I implement a web application with a Servlet that is listening on a port for socket connections?
-
1btw, what language is the remote client written in? If you are using Java in the remote client, it might be better to use RMI or even remote EJB calls.Vineet Reynolds– Vineet Reynolds2009-08-30 18:35:25 +00:00Commented Aug 30, 2009 at 18:35
-
Yes! the client is written in Java.Kevin Boyd– Kevin Boyd2009-08-31 04:39:10 +00:00Commented Aug 31, 2009 at 4:39
3 Answers
Having the servlet open ServerSockets is a bad code smell. This is primarily because it is the container's responsibility to manage sockets (among other resources like worker threads, sessions etc).
That said, I do not think you need a servlet in the first place. Unless you want to access some of the container's services, it would be better if you use a J2SE application to manage ServerSockets.
2 Comments
I assume you don't mean the front-door HTTP connection, which you get for free with the servlet container... But if you want to add, say, an admin service you could create a listener thread that sets some global state in the servlet. Note that this is not considered kosher (and I believe may even violate the servlet standard).
3 Comments
Not totally sure what you want to achieve, but you can have a look at client/server programming if that's what you need. Other than that, you could implement your web application as normal but change the default port to whatever suits your need.