1 22 package org.jboss.test.bench.servlet; 23 24 import java.util.Hashtable ; 25 import java.util.Enumeration ; 26 import java.util.Random ; 27 import java.io.PrintWriter ; 28 29 import javax.servlet.ServletException ; 30 import javax.servlet.http.HttpServlet ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 import javax.servlet.http.HttpSession ; 34 35 import javax.naming.Context ; 36 import javax.naming.InitialContext ; 37 38 import org.jboss.test.bench.interfaces.*; 39 40 public class SimpleServlet extends HttpServlet { 41 org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass()); 42 PrintWriter out; 43 44 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 45 throws ServletException , java.io.IOException { 46 47 String dest = req.getParameter("dest"); 48 49 resp.setContentType("text/html"); 50 out = resp.getWriter(); 51 52 out.println("<html>"); 53 out.println("<head>"); 54 55 out.println("<title>HelloEJB</title>"); 56 out.println("</head>"); 57 58 out.println("<body>"); 59 60 out.println("<h1>Servlet calling EJB</h1>"); 61 62 if ("SL".equals(dest)) callStateless(); 63 else if ("Entity".equals(dest)) callEntity(); 64 65 out.println("</body>"); 66 out.println("</html>"); 67 } 68 69 Context getContext() throws Exception { 70 System.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory"); 71 System.setProperty("java.naming.provider.url","localhost"); 72 System.setProperty("java.naming.factory.url.pkgs","org.jboss.naming;"); 73 74 return new InitialContext (); 75 76 } 77 78 void callStateless() { 79 try { 80 Context ctx = getContext(); 81 MySessionHome home = (MySessionHome)ctx.lookup("StatelessSession"); 82 MySession bean = home.create(); 83 84 out.println("called stateless session and it said: " + bean.getInt()); 85 86 } catch (Exception e) { 87 out.println("<pre>"); 88 e.printStackTrace(out); 89 out.println("</pre>"); 90 } 91 } 92 93 void callEntity() { 94 try { 95 Context ctx = getContext(); 96 SimpleEntityHome home = (SimpleEntityHome)ctx.lookup("SimpleEntity"); 97 SimpleEntity bean = home.create(50000+ new Random ().nextInt()); 98 99 out.println("called entity and it said: " + bean.getField()); 100 101 } catch (Exception e) { 102 out.println("<pre>"); 103 e.printStackTrace(out); 104 out.println("</pre>"); 105 } 106 } 107 108 } 109 110 111 | Popular Tags |