001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.bench.servlet;
023:
024: import java.util.Hashtable;
025: import java.util.Enumeration;
026: import java.util.Random;
027: import java.io.PrintWriter;
028:
029: import javax.servlet.ServletException;
030: import javax.servlet.http.HttpServlet;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033: import javax.servlet.http.HttpSession;
034:
035: import javax.naming.Context;
036: import javax.naming.InitialContext;
037:
038: import org.jboss.test.bench.interfaces.*;
039:
040: public class SimpleServlet extends HttpServlet {
041: org.apache.log4j.Category log = org.apache.log4j.Category
042: .getInstance(getClass());
043: PrintWriter out;
044:
045: protected void doGet(HttpServletRequest req,
046: HttpServletResponse resp) throws ServletException,
047: java.io.IOException {
048:
049: String dest = req.getParameter("dest");
050:
051: resp.setContentType("text/html");
052: out = resp.getWriter();
053:
054: out.println("<html>");
055: out.println("<head>");
056:
057: out.println("<title>HelloEJB</title>");
058: out.println("</head>");
059:
060: out.println("<body>");
061:
062: out.println("<h1>Servlet calling EJB</h1>");
063:
064: if ("SL".equals(dest))
065: callStateless();
066: else if ("Entity".equals(dest))
067: callEntity();
068:
069: out.println("</body>");
070: out.println("</html>");
071: }
072:
073: Context getContext() throws Exception {
074: System.setProperty("java.naming.factory.initial",
075: "org.jnp.interfaces.NamingContextFactory");
076: System.setProperty("java.naming.provider.url", System
077: .getProperty("jbosstest.server.host", "localhost"));
078: System.setProperty("java.naming.factory.url.pkgs",
079: "org.jboss.naming;");
080:
081: return new InitialContext();
082:
083: }
084:
085: void callStateless() {
086: try {
087: Context ctx = getContext();
088: MySessionHome home = (MySessionHome) ctx
089: .lookup("StatelessSession");
090: MySession bean = home.create();
091:
092: out.println("called stateless session and it said: "
093: + bean.getInt());
094:
095: } catch (Exception e) {
096: out.println("<pre>");
097: e.printStackTrace(out);
098: out.println("</pre>");
099: }
100: }
101:
102: void callEntity() {
103: try {
104: Context ctx = getContext();
105: SimpleEntityHome home = (SimpleEntityHome) ctx
106: .lookup("SimpleEntity");
107: SimpleEntity bean = home.create(50000 + new Random()
108: .nextInt());
109:
110: out
111: .println("called entity and it said: "
112: + bean.getField());
113:
114: } catch (Exception e) {
115: out.println("<pre>");
116: e.printStackTrace(out);
117: out.println("</pre>");
118: }
119: }
120:
121: }
|