KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > enterprise > servlet_stateless_war > Servlet2Stateless


1 /*
2  * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
3  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4  */

5 package enterprise.servlet_stateless_war;
6
7 import java.io.*;
8
9 import javax.ejb.EJB JavaDoc;
10
11 import javax.servlet.*;
12 import javax.servlet.http.*;
13
14 import javax.naming.*;
15
16 import enterprise.servlet_stateless_ejb.*;
17
18 // Though it is perfectly fine to declare the dependency on the bean
19
// at the type level, it is not required for stateless session bean
20
// Hence the next two lines are commented and we rely on the
21
// container to inject the bean.
22
// @EJB(name="StatelessSession", beanInterface=StatelessSession.class)
23

24 public class Servlet2Stateless
25     extends HttpServlet {
26
27     // Using injection for Stateless session bean is still thread-safe since
28
// the ejb container will route every request to different
29
// bean instances. However, for Stateful session beans the
30
// dependency on the bean must be declared at the type level
31

32     @EJB JavaDoc
33     private StatelessSession sless;
34
35     public void service(HttpServletRequest req, HttpServletResponse resp)
36             throws ServletException, IOException {
37
38         resp.setContentType("text/html");
39         PrintWriter out = resp.getWriter();
40
41         try {
42
43             out.println("<HTML> <HEAD> <TITLE> Servlet Output </TITLE> </HEAD> <BODY BGCOLOR=white>");
44             out.println("<CENTER> <FONT size=+1> Servlet2Stateless:: Please enter your name </FONT> </CENTER> <p> ");
45             out.println("<form method=\"POST\">");
46             out.println("<TABLE>");
47             out.println("<tr><td>Name: </td>");
48             out.println("<td><input type=\"text\" name=\"name\"> </td>");
49             out.println("</tr><tr><td></td>");
50             out.println("<td><input type=\"submit\" name=\"sub\"> </td>");
51             out.println("</tr>");
52             out.println("</TABLE>");
53             out.println("</form>");
54             String JavaDoc val = req.getParameter("name");
55
56             if ((val != null) && (val.trim().length() > 0)) {
57                 out
58                         .println("<FONT size=+1 color=red> Greeting from StatelessSessionBean: </FONT> "
59                                 + sless.sayHello(val) + "<br>");
60             }
61             out.println("</BODY> </HTML> ");
62
63         } catch (Exception JavaDoc ex) {
64             ex.printStackTrace();
65             System.out.println("webclient servlet test failed");
66             throw new ServletException(ex);
67         }
68     }
69
70 }
71
Popular Tags