1 25 26 package org.objectweb.easybeans.examples.statelessbean; 27 28 import java.io.IOException ; 29 import java.io.PrintWriter ; 30 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import javax.servlet.ServletException ; 34 import javax.servlet.http.HttpServlet ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 38 42 public class ClientServletStateless extends HttpServlet { 43 44 47 private static final long serialVersionUID = 6893863749912962928L; 48 49 60 @Override 61 public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException , ServletException { 62 63 response.setContentType("text/html"); 64 PrintWriter 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 String operation = request.getParameter("operation"); 74 if (operation != null) { 75 if (operation.equals("helloWorld")) { 76 displayHelloWorld(out); 77 } else if (operation.equals("add")) { 78 String param1 = request.getParameter("p1"); 80 String 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 103 private void displayHelloWorld(final PrintWriter 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 e) { 110 displayException(out, "Cannot call helloworld on the bean", e); 111 } 112 } 113 114 118 private void displayDefault(final PrintWriter 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 138 private void displayResult(final PrintWriter 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 e) { 144 displayException(out, "<br>Cannot call add() method on the bean", e); 145 } 146 } 147 148 154 private void displayException(final PrintWriter out, final String errMsg, final Exception e) { 155 out.println("<p>Exception : " + errMsg); 156 out.println("<pre>"); 157 e.printStackTrace(out); 158 out.println("</pre></p>"); 159 } 160 161 166 private StatelessRemote getBean() throws Exception { 167 Context initialContext = new InitialContext (); 168 Object 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 ("Cannot cast object into StatelessRemote"); 176 177 } 178 179 } 180 | Popular Tags |