This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Listener in servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Listener in servlets" Watch "Listener in servlets" New topic
Author

Listener in servlets

Sachin Deshpande
Greenhorn

Joined: Jan 31, 2001
Posts: 25
Is it wise to have a listener (in other words, open a ServerSocket) in a servlet and listen for connections?
Wolfgang Stein
Greenhorn

Joined: Mar 26, 2001
Posts: 11
I have never done this but imagine some general pitfalls:
- long socket processing would probably time-out the browser client.
- socket-processing is usually multi-threaded. HttpServletResponse objects must not be used in separate threads if the original request thread already committed a response.
- Establishing a socket-listener in each ServletRequest is certainly not what you intend to do. You probably want to establish it in the servlets 'init()' Method or in static initalization.
- A servlet container may unload a servlet unexpectedly (not only on servber shutdown). You should catch these events in the 'destroy()' or 'finalize()' method respectively.
Due the restricted lifecycle of servlets i don't see much sense in a servlets socket processing so far (beside some kind of remote monitoring). What do you want to do exactly?
Wolfgang
Sachin Deshpande
Greenhorn

Joined: Jan 31, 2001
Posts: 25
I am developing a system.
There only one entry and exit point to the system ie via DMServlet.
Its recieves 2 variables thru the url.
Depending on the value of these variables, DMServlet calls other servlets (sub-servlets) which do some processing. The data obtained by these servlets has to be sent back to DMServlet which in turn will understand what has to be sent to the user.
Since the DMServlet and the various sub-servlets are loaded on diffrent m/cs/JVMs, the HttpSession class cannot be used to pass the information.
The problem how data is to be sent from subservlets to DMServlets.
Hence I was wondering if DMServlet could listen and wait for data coming from sub-servlets by opneing a ServerSocket.
Any ideas?
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi Sachin,

Hence I was wondering if DMServlet could listen and wait for data coming from sub-servlets by opneing a ServerSocket.

Well, yes, you can do it.The process is called Http Tunnelling.This is a method of writing serialized objects using HTTP connection.You are creating a sub-protocol inside the HTTP protocol- that is "tunneling" inside another protocol.This relieves you from the hassles of dealing with the actual transport layer.
Some pros of this technique:
1. It is simple to use because of Serializable interface.
2. It is very fast for lightweight objects
3. It sheilds you from the communicating layer
4. Most important, it provides you a method of "tunneling" through firewalls
Some cons :
1. You need to make sure that all your objects and nested objects implement the Serializable interface
2. You typically use this in Applet-Servlet communication, and this could pose security issues to limit you to connect to the applet originating server.
3. It would give you a performance hit, if you try to pass larger objects using this technique
As far as the code goes, you would need to use the java.net.* package.
You may open the connection using :

Then you can write your serialized object using this connection.You would need to use java.io.ObjectOutputStream for it.
Finally you can read the contents in your applet using the java.io.ObjectInputStream.
Hope this helps,
Regards,



------------------
Sandeep Desai
vgdesai@vsnl.com

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Wolfgang Stein
Greenhorn

Joined: Mar 26, 2001
Posts: 11
Sachin,
to access your servlet use a URLConnection as Desai suggested. There is also a java.net.HttpURLConnection class, but i didn't use it so far.
You don't have to communicate serialized objects necessarily. For simple text or html responses of the subservlet you can read the InputStream directly using 'getInputStream()' on the open URLConnection.
Object serialization might be the appropriate approach regarding a clean architecture or complex information exchange. It might fail if your are not the author of the subservlet or your boss prefers quick solutions :-)
Good luck,
Wolfgang
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi Sachin,
Wolfgang is right!You donot need to use serialization, if you have simple text/html responses.
However, going by what you have said

Since the DMServlet and the various sub-servlets are loaded on diffrent m/cs/JVMs, the HttpSession class cannot be used to pass
the information.

I assume, you would want to store some objects in the HttpSession class, and use the same in your sub-servlets located on the different machine/JVM's.In that case, you have 2 options :
1. Use Serialization
2. Store the HttpSession Object in the DB, and retrieve it, for later use.
The second option is preferable, if your objects are quite large in size and you have a complex CS in place
The first option is OK for transporting small objects (certainly not image files,etc.), which may not clog your networks.
There are trade-offs, in both the design and you have to choose judiciously.
Regards,


------------------
Sandeep Desai
vgdesai@vsnl.com

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Listener in servlets
 
Similar Threads
How to load file at the time of start of the application
what to put here
? for web-applications
servlet startup order
can't enable or disable events