KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > MessageServlet


1 package example;
2
3 import java.util.logging.Logger JavaDoc;
4
5 import java.io.PrintWriter JavaDoc;
6 import java.io.IOException JavaDoc;
7
8 import javax.servlet.GenericServlet JavaDoc;
9 import javax.servlet.ServletRequest JavaDoc;
10 import javax.servlet.ServletResponse JavaDoc;
11 import javax.servlet.ServletException JavaDoc;
12
13 import com.caucho.services.message.MessageSender;
14
15 public class MessageServlet extends GenericServlet JavaDoc {
16   private static final Logger JavaDoc log =
17     Logger.getLogger(MessageServlet.class.getName());
18
19   private MessageSender _sender;
20   private int _count;
21
22   /**
23    * Sets the message sender (called by IoC configuration)
24    */

25   public void setSender(MessageSender sender)
26   {
27     _sender = sender;
28   }
29   
30   /**
31    * Sends the message.
32    */

33   public void service(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
34     throws IOException JavaDoc, ServletException JavaDoc
35   {
36     String JavaDoc message = "sample message: " + _count++;
37
38     response.setContentType("text/html");
39
40     PrintWriter JavaDoc out = response.getWriter();
41     out.println("message: " + message + "<br>");
42     
43     log.info("sending: " + message);
44
45     _sender.send(null, message);
46     
47     log.info("complete send");
48
49     out.println("received message: " + MyListener.getLastMessage() + "<br>");
50   }
51 }
52
Popular Tags