Source Code Cross Referenced for ServletOp.java in  » J2EE » JOnAS-4.8.6 » org » objectweb » earsample » servlets » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » JOnAS 4.8.6 » org.objectweb.earsample.servlets 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * JOnAS: Java(TM) Open Application Server
003:         * Copyright (C) 1999-2005 Bull S.A.
004:         * Contact: jonas-team@objectweb.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: ServletOp.java 6946 2005-06-14 14:43:43Z kemlerp $
023:         * --------------------------------------------------------------------------
024:         */package org.objectweb.earsample.servlets;
025:
026:        import java.io.IOException;
027:        import java.io.PrintWriter;
028:        import java.net.URL;
029:
030:        import javax.naming.Context;
031:        import javax.naming.InitialContext;
032:        import javax.rmi.PortableRemoteObject;
033:        import javax.servlet.ServletException;
034:        import javax.servlet.http.HttpServlet;
035:        import javax.servlet.http.HttpServletRequest;
036:        import javax.servlet.http.HttpServletResponse;
037:        import javax.transaction.UserTransaction;
038:
039:        import org.objectweb.earsample.beans.secusb.Op;
040:        import org.objectweb.earsample.beans.secusb.OpHome;
041:        import org.objectweb.earsample.beans.secusb.OpLocal;
042:        import org.objectweb.earsample.beans.secusb.OpLocalHome;
043:
044:        /**
045:         * This servlet is an example to show how to access a EJB from a servlet
046:         * @author JOnAS team
047:         */
048:        public class ServletOp extends HttpServlet {
049:
050:            /**
051:             * First amount to buy
052:             */
053:            private static final int FIRST_BUY_AMOUNT = 10;
054:
055:            /**
056:             * Second amount to buy
057:             */
058:            private static final int SECOND_BUY_AMOUNT = 20;
059:
060:            /**
061:             * Third amount to buy (will be rollback)
062:             */
063:            private static final int THIRD_BUY_AMOUNT = 50;
064:
065:            /**
066:             * Called by the server (via the service method) to allow a servlet to
067:             * handle a GET request.
068:             * @param request an HttpServletRequest object that contains the request
069:             * the client has made of the servlet
070:             * @param response an HttpServletResponse object that contains the
071:             * response the servlet sends to the client
072:             * @throws IOException if an input or output error is detected when the
073:             * servlet handles the GET request
074:             * @throws ServletException if the request for the GET could not be handled
075:             */
076:            public void doGet(HttpServletRequest request,
077:                    HttpServletResponse response) throws IOException,
078:                    ServletException {
079:
080:                response.setContentType("text/html");
081:                PrintWriter out = response.getWriter();
082:                out.println("<html>");
083:                out.println("<head>");
084:                out
085:                        .println("<link type=\"text/css\" href=\"../ow_jonas.css\" rel=\"stylesheet\" id=\"stylesheet\">");
086:                out.println("<title>");
087:                out
088:                        .println("Ear Sample of Servlet accessing a protected EJB</title>");
089:                out.println("</head>");
090:                out
091:                        .println("<body style=\"background : white; color : black;\">");
092:                out
093:                        .println("<h1>Ear sample of Servlet accessing a protected EJB");
094:                out
095:                        .println("</h1><img src=\"../img/tomcat.gif\" alt=\"Tomcat Logo\">");
096:                out
097:                        .println("<img src=\"../img/jetty.gif\" alt=\"Jetty Logo\">");
098:                out
099:                        .println("<img src=\"../img/ow_jonas_logo.gif\" alt=\"JOnAS Logo\">");
100:                out.println("<h3>Initial context / UserTransaction </h3>");
101:                out.println("<ul>");
102:                Context initialContext = null;
103:                try {
104:                    initialContext = new InitialContext();
105:                    out.println("<li>Initial context OK</li>");
106:                } catch (Exception e) {
107:                    out.print("<li>Cannot get initial context for JNDI: ");
108:                    out.println(e + "</li>");
109:                    return;
110:                }
111:
112:                // We want to start transactions from client: get UserTransaction
113:                UserTransaction utx = null;
114:                try {
115:                    utx = (UserTransaction) initialContext
116:                            .lookup("java:comp/UserTransaction");
117:                    out.println("<li>Get java:comp/UserTransaction OK</li>");
118:                } catch (Exception e) {
119:                    out.println("<li>Cannot lookup java:comp/UserTransaction: "
120:                            + e + "</li>");
121:                    return;
122:                }
123:                out.println("</ul><br />");
124:
125:                out.println("<h3>");
126:                out
127:                        .println("Lookup on env-entry with java:comp/env/envEntryString");
128:                out.println("</h3>");
129:                out.println("<ul>");
130:                String envEntry = null;
131:                try {
132:                    envEntry = (String) initialContext
133:                            .lookup("java:comp/env/envEntryString");
134:                    out.println("<li>Env entry is : " + envEntry + "</li>");
135:                } catch (Exception e) {
136:                    out.println("<li>Cannot get env-entry on JNDI " + e
137:                            + "</li>");
138:                    return;
139:                }
140:                out.println("</ul><br />");
141:
142:                out.println("</ul><br />");
143:
144:                out.println("<h3>");
145:                out.println("Lookup on URL with java:comp/env/url/URL");
146:                out.println("</h3>");
147:                out.println("<ul>");
148:                URL jonasURL = null;
149:                try {
150:                    jonasURL = (URL) initialContext
151:                            .lookup("java:comp/env/url/URL");
152:                    out.println("<li>URL is : <a href=\"" + jonasURL.toString()
153:                            + ">" + jonasURL.toString() + "</a></li>");
154:                } catch (Exception e) {
155:                    out.println("<li>Cannot get url on JNDI " + e + "</li>");
156:                    return;
157:                }
158:                out.println("</ul><br />");
159:
160:                out
161:                        .println("<h3>Actions realized by the servlet and the EJB</h3>");
162:                out.println("<h3>With local interface</h3>");
163:                out.println("<ul>");
164:                // Connecting to OpLocalHome thru JNDI
165:                OpLocalHome opLocalHome = null;
166:                try {
167:                    opLocalHome = (OpLocalHome) initialContext
168:                            .lookup("java:comp/env/ejb/OpLocal");
169:                } catch (Exception e) {
170:                    out.println("<li>Cannot lookup java:comp/env/ejb/OpLocal: "
171:                            + e + "</li>");
172:                    return;
173:                }
174:                // OpBean creation
175:                OpLocal opLocal = null;
176:                try {
177:                    opLocal = opLocalHome.create("User1");
178:                    out.println("<li>Create a bean</li>");
179:                } catch (Exception e) {
180:                    out.println("<li>Cannot create OpBean: " + e + "</li>");
181:                    return;
182:                }
183:                // First transaction (committed)
184:                try {
185:                    out.println("<li>Start a first transaction</li>");
186:                    utx.begin();
187:                    opLocal.buy(FIRST_BUY_AMOUNT);
188:                    out.println("<li>First request on the new bean</li>");
189:                    opLocal.buy(SECOND_BUY_AMOUNT);
190:                    out.println("<li>Second request on the bean</li>");
191:                    utx.commit();
192:                    out.println("<li>Commit the transaction</li>");
193:                } catch (Exception e) {
194:                    out.println("<li>exception during 1st Tx: " + e + "</li>");
195:                    return;
196:                }
197:                // Start another transaction (rolled back)
198:                try {
199:                    out.println("<li>Start a second transaction</li>");
200:                    utx.begin();
201:                    opLocal.buy(THIRD_BUY_AMOUNT);
202:                    utx.rollback();
203:                    out.println("<li>Rollback the transaction</li>");
204:                } catch (Exception e) {
205:                    out.println("<li>exception during 2nd Tx: " + e + "</li>");
206:                    return;
207:                }
208:                // Get the total bought, outside the transaction
209:                int val = 0;
210:                try {
211:                    val = opLocal.read();
212:                    out.println("<li>Request outside any transaction</li>");
213:                } catch (Exception e) {
214:                    out.println("<li>Cannot read value on t1 : " + e + "</li>");
215:                    return;
216:                }
217:                if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) {
218:                    out.println("<li>Bad value read: " + val + "</li>");
219:                    return;
220:                }
221:                // Remove Session bean
222:                try {
223:                    opLocal.remove();
224:                } catch (Exception e) {
225:                    out.println("<li>Exception on buy: " + e + "</li>");
226:                    return;
227:                }
228:                out.println("</ul><br />");
229:
230:                out.println("<h3>With remote interface</h3>");
231:                out.println("<ul>");
232:                // Connecting to OpHome thru JNDI
233:                OpHome opHome = null;
234:                try {
235:                    opHome = (OpHome) PortableRemoteObject.narrow(
236:                            initialContext.lookup("java:comp/env/ejb/Op"),
237:                            OpHome.class);
238:                } catch (Exception e) {
239:                    out.println("<li>Cannot lookup java:comp/env/ejb/Op: " + e
240:                            + "</li>");
241:                    return;
242:                }
243:                // OpBean creation
244:                Op op = null;
245:                try {
246:                    op = opHome.create("User1");
247:                    out.println("<li>Create a bean</li>");
248:                } catch (Exception e) {
249:                    out.println("<li>Cannot create OpBean: " + e + "</li>");
250:                    return;
251:                }
252:                // First transaction (committed)
253:                try {
254:                    out.println("<li>Start a first transaction</li>");
255:                    utx.begin();
256:                    op.buy(FIRST_BUY_AMOUNT);
257:                    out.println("<li>First request on the new bean</li>");
258:                    op.buy(SECOND_BUY_AMOUNT);
259:                    out.println("<li>Second request on the bean</li>");
260:                    utx.commit();
261:                    out.println("<li>Commit the transaction</li>");
262:                } catch (Exception e) {
263:                    out.println("<li>exception during 1st Tx: " + e + "</li>");
264:                    return;
265:                }
266:                // Start another transaction (rolled back)
267:                try {
268:                    out.println("<li>Start a second transaction</li>");
269:                    utx.begin();
270:                    op.buy(THIRD_BUY_AMOUNT);
271:                    utx.rollback();
272:                    out.println("<li>Rollback the transaction</li>");
273:                } catch (Exception e) {
274:                    out.println("<li>exception during 2nd Tx: " + e + "</li>");
275:                    return;
276:                }
277:                // Get the total bought, outside the transaction
278:                val = 0;
279:                try {
280:                    val = op.read();
281:                    out.println("<li>Request outside any transaction</li>");
282:                } catch (Exception e) {
283:                    out.println("<li>Cannot read value on t1 : " + e + "</li>");
284:                    return;
285:                }
286:                if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) {
287:                    out.println("<li>Bad value read: " + val + "</li>");
288:                    return;
289:                }
290:                // Remove Session bean
291:                try {
292:                    op.remove();
293:                } catch (Exception e) {
294:                    out.println("<li>Exception on buy: " + e + "</li>");
295:                    return;
296:                }
297:                out.println("</ul><br />");
298:
299:                out.println("<h3>");
300:                out.println("Resource Adapter Properties");
301:                out.println("</h3>");
302:                out.println("<ul>");
303:                String eisName = null;
304:                try {
305:                    eisName = (String) initialContext.lookup("eisName");
306:                    out.println("<li>EIS Name is : " + eisName + "</li>");
307:                } catch (Exception e) {
308:                    out.println("<li>Cannot get eis name on JNDI " + e
309:                            + "</li>");
310:                    return;
311:                }
312:                out.println("</ul><br />");
313:
314:                out.println("<strong>Sample is OK.</strong><br />");
315:                out.println("</body>");
316:                out.println("</html>");
317:            }
318:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.