KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > bench > servlet > SimpleServlet


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.bench.servlet;
23
24 import java.util.Hashtable JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Random JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.http.HttpServlet JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.http.HttpSession JavaDoc;
34
35 import javax.naming.Context JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37
38 import org.jboss.test.bench.interfaces.*;
39
40 public class SimpleServlet extends HttpServlet JavaDoc {
41    org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass());
42     PrintWriter JavaDoc out;
43
44     protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
45         throws ServletException JavaDoc, java.io.IOException JavaDoc {
46
47         String JavaDoc 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 JavaDoc getContext() throws Exception JavaDoc {
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 JavaDoc();
75         
76     }
77     
78     void callStateless() {
79         try {
80             Context JavaDoc 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 JavaDoc e) {
87             out.println("<pre>");
88             e.printStackTrace(out);
89             out.println("</pre>");
90         }
91     }
92     
93     void callEntity() {
94         try {
95             Context JavaDoc ctx = getContext();
96             SimpleEntityHome home = (SimpleEntityHome)ctx.lookup("SimpleEntity");
97             SimpleEntity bean = home.create(50000+ new Random JavaDoc().nextInt());
98
99             out.println("called entity and it said: " + bean.getField());
100             
101         } catch (Exception JavaDoc e) {
102             out.println("<pre>");
103             e.printStackTrace(out);
104             out.println("</pre>");
105         }
106     }
107     
108 }
109
110
111
Popular Tags