com.sun.grizzly.samples.comet
Class TwitterServlet
java.lang.Object
javax.servlet.GenericServlet
javax.servlet.http.HttpServlet
com.sun.grizzly.samples.comet.TwitterServlet
- All Implemented Interfaces:
- Serializable, Servlet, ServletConfig
public class TwitterServlet
- extends HttpServlet
Twitter like Comet application. This Servlet
implement the logic
needed to support micro blogging a la Twitter.com. Users can blog about what
they are doing and can also follow their friends. When an update is made
by one user, all its follower gets updated automatically. The updated words
can be moved on the screen and all follower will see the move.
This Servlet
demonstrate how multiple CometContext
can be
used to easily isolate suspended connection (aka CometHandler
to only
push messages to a subset of those suspended connection. It also demonstrate
how to push messages to a single CometHandler
There is one CometContext
per user. CometHandler
associated
with the user suspended connection are added to their CometContext
and added to the CometContext
of the users they are following.
- Author:
- Jeanfrancois Arcand
- See Also:
- Serialized Form
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
TwitterServlet
public TwitterServlet()
init
public void init(ServletConfig config)
throws ServletException
- Grab an instance of
ServletContext
- Specified by:
init
in interface Servlet
- Overrides:
init
in class GenericServlet
- Parameters:
config
-
- Throws:
ServletException
doGet
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
IOException
- Same as
doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
- Overrides:
doGet
in class HttpServlet
- Parameters:
request
- response
-
- Throws:
ServletException
IOException
doPost
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
IOException
- Based on the
ServletRequest.getParameter(java.lang.String)
action value, decide
if the connection needs to be suspended (when the user logs in) or if the
CometContext
needs to be updated (by the user or by its follower.
There is one CometContext
per suspended connection, representing
the user account. When one user B request to follow user A, the CometHandler
associated with user B's CometContext
is also added to user A
CometContext
. Hence when user A push message (CometContext.notify()
all CometHandler
gets the CometEvent
, which means user B
will be updated when user A update its micro blog.
The suspended connection on the client side is multiplexed, e.g.
messages sent by the server are not only for a single component, but
shared amongs several components. The client side include a message board
that is updated by notifying the owner of the CometContext
. This
is achieved by calling CometContext.notify(CometEvent,CometHandler)
- Overrides:
doPost
in class HttpServlet
- Parameters:
request
- response
-
- Throws:
ServletException
IOException
Copyright © 2012 Oracle Corporation. All Rights Reserved.