001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ClientServletStateless.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.examples.statelessbean;
025:
026: import java.io.IOException;
027: import java.io.PrintWriter;
028:
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import javax.servlet.ServletException;
032: import javax.servlet.http.HttpServlet;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035:
036: /**
037: * Servlet's client for the stateless session bean.
038: * @author Florent Benoit
039: */
040: public class ClientServletStateless extends HttpServlet {
041:
042: /**
043: * Serializable class uid.
044: */
045: private static final long serialVersionUID = 6893863749912962928L;
046:
047: /**
048: * Called by the server (via the service method) to allow a servlet to
049: * handle a GET request.
050: * @param request an HttpServletRequest object that contains the request the
051: * client has made of the servlet
052: * @param response an HttpServletResponse object that contains the response
053: * the servlet sends to the client
054: * @throws IOException if an input or output error is detected when the
055: * servlet handles the GET request
056: * @throws ServletException if the request for the GET could not be handled
057: */
058: @Override
059: public void doGet(final HttpServletRequest request,
060: final HttpServletResponse response) throws IOException,
061: ServletException {
062:
063: response.setContentType("text/html");
064: PrintWriter out = response.getWriter();
065: out.println("<html>");
066: out.println("<head>");
067: out.println("<title>");
068: out.println("Client of stateless session bean</title>");
069: out.println("</head>");
070: out.println("<body>");
071:
072: // no operation ? displays button for hello world and calculator
073: String operation = request.getParameter("operation");
074: if (operation != null) {
075: if (operation.equals("helloWorld")) {
076: displayHelloWorld(out);
077: } else if (operation.equals("add")) {
078: // get two parameters
079: String param1 = request.getParameter("p1");
080: String param2 = request.getParameter("p2");
081: if (param1 != null && param2 != null) {
082: int v1 = Integer.parseInt(param1);
083: int v2 = Integer.parseInt(param2);
084: displayResult(out, v1, v2);
085: } else {
086: out.println("Missing values for operation add");
087: }
088: }
089: }
090: out.println("<hr width=\"80%\"/>");
091: displayDefault(out);
092:
093: out.println("</body>");
094: out.println("</html>");
095: out.close();
096: }
097:
098: /**
099: * Call HelloWorld method.
100: * @param out the given writer
101: */
102: private void displayHelloWorld(final PrintWriter out) {
103: out.println("Calling helloWorld() method");
104: out.println("<br>");
105: try {
106: getBean().helloWorld();
107: out.println("helloWorld() method called OK.");
108: } catch (Exception e) {
109: displayException(out, "Cannot call helloworld on the bean",
110: e);
111: }
112: }
113:
114: /**
115: * By default, call helloWorld method.
116: * @param out the given writer
117: */
118: private void displayDefault(final PrintWriter out) {
119: out
120: .println("<form method=get action=\"\" enctype=\"multipart/form-data\">");
121: out.println("sum of a + b :");
122: out
123: .println("<p><input type=hidden name=\"operation\" value=\"add\"></p>");
124: out.println("<p><input type=text name=p1 value=\"1\"></p>");
125: out.println("<p><input type=text name=p2 value=\"2\"></p>");
126: out.println("<p><input type=submit value=\"add !\"></p>");
127: out.println("</form>");
128: out
129: .println("<form method=get action=\"\" enctype=\"multipart/form-data\">");
130: out
131: .println("<p><input type=hidden name=\"operation\" value=\"helloWorld\"></p>");
132: out
133: .println("<p><input type=submit value=\"hello world !\"></p>");
134: out.println("</form>");
135: }
136:
137: /**
138: * Prints he result of the sum of val1 and val2.
139: * @param out the given writer
140: * @param val1 first arg for add method
141: * @param val2 second arg for add method
142: */
143: private void displayResult(final PrintWriter out, final int val1,
144: final int val2) {
145: out.println("<br> Sum of '" + val1 + "' and '" + val2 + "' = ");
146: try {
147: int sum = getBean().add(val1, val2);
148: out.println(sum);
149: } catch (Exception e) {
150: displayException(out,
151: "<br>Cannot call add() method on the bean", e);
152: }
153: }
154:
155: /**
156: * If there is an exception, print the exception.
157: * @param out the given writer
158: * @param errMsg the error message
159: * @param e the content of the exception
160: */
161: private void displayException(final PrintWriter out,
162: final String errMsg, final Exception e) {
163: out.println("<p>Exception : " + errMsg);
164: out.println("<pre>");
165: e.printStackTrace(out);
166: out.println("</pre></p>");
167: }
168:
169: /**
170: * Lookup the stateless bean and gets a reference on it.
171: * @return the stateless bean business interface.
172: * @throws Exception if the bean cannot be retrieved.
173: */
174: private StatelessRemote getBean() throws Exception {
175: Context initialContext = new InitialContext();
176: Object o = initialContext
177: .lookup("org.ow2.easybeans.examples.statelessbean.StatelessBean"
178: + "_"
179: + StatelessRemote.class.getName()
180: + "@Remote");
181:
182: if (o instanceof StatelessRemote) {
183: StatelessRemote statelessBean = (StatelessRemote) o;
184: return statelessBean;
185: }
186: throw new Exception("Cannot cast object into StatelessRemote");
187:
188: }
189:
190: }
|