KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > GreetingClientServlet


1 package example;
2
3 import java.io.PrintWriter JavaDoc;
4 import java.io.IOException JavaDoc;
5
6 import javax.servlet.ServletRequest JavaDoc;
7 import javax.servlet.ServletResponse JavaDoc;
8 import javax.servlet.GenericServlet JavaDoc;
9 import javax.servlet.ServletException JavaDoc;
10
11 /**
12  * The injection filter adds configuration objects to the request
13  * attributes.
14  */

15 public class GreetingClientServlet extends GenericServlet JavaDoc {
16   private String JavaDoc _name = "generic";
17   private GreetingAPI _greeting;
18
19   /**
20    * Sets the client servlet name.
21    */

22   public void setName(String JavaDoc name)
23   {
24     _name = name;
25   }
26
27   /**
28    * Sets the client greeting.
29    */

30   public void setGreeting(GreetingAPI greeting)
31   {
32     _greeting = greeting;
33   }
34
35   /**
36    * Servlet init
37    */

38   public void init()
39     throws ServletException JavaDoc
40   {
41     if (_greeting == null)
42       throw new ServletException JavaDoc("GreetingClientServlet needs a configured greeting");
43   }
44
45   /**
46    * Runs the servlet
47    */

48   public void service(ServletRequest JavaDoc req, ServletResponse JavaDoc res)
49     throws IOException JavaDoc, ServletException JavaDoc
50   {
51     PrintWriter JavaDoc out = res.getWriter();
52
53     out.println(_name + ": " + _greeting.greeting());
54   }
55 }
56
Popular Tags