KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > examples > statelessbean > ClientServletStateless


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ClientServletStateless.java 1118 2006-09-19 07:38:24Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.examples.statelessbean;
27
28 import java.io.IOException JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.http.HttpServlet JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37
38 /**
39  * Servlet's client for the stateless session bean.
40  * @author Florent Benoit
41  */

42 public class ClientServletStateless extends HttpServlet JavaDoc {
43
44     /**
45      * Serializable class uid.
46      */

47     private static final long serialVersionUID = 6893863749912962928L;
48
49     /**
50      * Called by the server (via the service method) to allow a servlet to
51      * handle a GET request.
52      * @param request an HttpServletRequest object that contains the request the
53      * client has made of the servlet
54      * @param response an HttpServletResponse object that contains the response
55      * the servlet sends to the client
56      * @throws IOException if an input or output error is detected when the
57      * servlet handles the GET request
58      * @throws ServletException if the request for the GET could not be handled
59      */

60     @Override JavaDoc
61     public void doGet(final HttpServletRequest JavaDoc request, final HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc {
62
63         response.setContentType("text/html");
64         PrintWriter JavaDoc out = response.getWriter();
65         out.println("<html>");
66         out.println("<head>");
67         out.println("<title>");
68         out.println("Client of stateless session bean</title>");
69         out.println("</head>");
70         out.println("<body>");
71
72         // no operation ? displays button for hello world and calculator
73
String JavaDoc operation = request.getParameter("operation");
74         if (operation != null) {
75             if (operation.equals("helloWorld")) {
76                 displayHelloWorld(out);
77             } else if (operation.equals("add")) {
78                 // get two parameters
79
String JavaDoc param1 = request.getParameter("p1");
80                 String JavaDoc param2 = request.getParameter("p2");
81                 if (param1 != null && param2 != null) {
82                     int v1 = Integer.parseInt(param1);
83                     int v2 = Integer.parseInt(param2);
84                     displayResult(out, v1, v2);
85                 } else {
86                     out.println("Missing values for operation add");
87                 }
88             }
89         }
90         out.println("<hr width=\"80%\"/>");
91         displayDefault(out);
92
93
94         out.println("</body>");
95         out.println("</html>");
96         out.close();
97     }
98
99     /**
100      * Call HelloWorld method.
101      * @param out the given writer
102      */

103     private void displayHelloWorld(final PrintWriter JavaDoc out) {
104         out.println("Calling helloWorld() method");
105         out.println("<br>");
106         try {
107             getBean().helloWorld();
108             out.println("helloWorld() method called OK.");
109         } catch (Exception JavaDoc e) {
110             displayException(out, "Cannot call helloworld on the bean", e);
111         }
112     }
113
114     /**
115      * By default, call helloWorld method.
116      * @param out the given writer
117      */

118     private void displayDefault(final PrintWriter JavaDoc out) {
119         out.println("<form method=get action=\"\" enctype=\"multipart/form-data\">");
120         out.println("sum of a + b :");
121         out.println("<p><input type=hidden name=\"operation\" value=\"add\"></p>");
122         out.println("<p><input type=text name=p1 value=\"1\"></p>");
123         out.println("<p><input type=text name=p2 value=\"2\"></p>");
124         out.println("<p><input type=submit value=\"add !\"></p>");
125         out.println("</form>");
126         out.println("<form method=get action=\"\" enctype=\"multipart/form-data\">");
127         out.println("<p><input type=hidden name=\"operation\" value=\"helloWorld\"></p>");
128         out.println("<p><input type=submit value=\"hello world !\"></p>");
129         out.println("</form>");
130     }
131
132     /**
133      * Prints he result of the sum of val1 and val2.
134      * @param out the given writer
135      * @param val1 first arg for add method
136      * @param val2 second arg for add method
137      */

138     private void displayResult(final PrintWriter JavaDoc out, final int val1, final int val2) {
139         out.println("<br> Sum of '" + val1 + "' and '" + val2 + "' = ");
140         try {
141             int sum = getBean().add(val1, val2);
142             out.println(sum);
143         } catch (Exception JavaDoc e) {
144             displayException(out, "<br>Cannot call add() method on the bean", e);
145         }
146     }
147
148     /**
149      * If there is an exception, print the exception.
150      * @param out the given writer
151      * @param errMsg the error message
152      * @param e the content of the exception
153      */

154     private void displayException(final PrintWriter JavaDoc out, final String JavaDoc errMsg, final Exception JavaDoc e) {
155         out.println("<p>Exception : " + errMsg);
156         out.println("<pre>");
157         e.printStackTrace(out);
158         out.println("</pre></p>");
159     }
160
161     /**
162      * Lookup the stateless bean and gets a reference on it.
163      * @return the stateless bean business interface.
164      * @throws Exception if the bean cannot be retrieved.
165      */

166     private StatelessRemote getBean() throws Exception JavaDoc {
167         Context JavaDoc initialContext = new InitialContext JavaDoc();
168         Object JavaDoc o = initialContext.lookup("org.objectweb.easybeans.examples.statelessbean.StatelessBean" + "_"
169                 + StatelessRemote.class.getName() + "@Remote");
170
171         if (o instanceof StatelessRemote) {
172             StatelessRemote statelessBean = (StatelessRemote) o;
173             return statelessBean;
174         }
175         throw new Exception JavaDoc("Cannot cast object into StatelessRemote");
176
177     }
178
179 }
180
Popular Tags