Source Code Cross Referenced for SampleServlet.java in  » J2EE » JOnAS-4.8.6 » samplehttp » servlet » 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 » samplehttp.servlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package samplehttp.servlet;
002:
003:        //import java
004:        import java.io.IOException;
005:        import java.io.PrintWriter;
006:        import java.util.StringTokenizer;
007:
008:        //import javax
009:        import javax.naming.Context;
010:        import javax.naming.InitialContext;
011:        import javax.servlet.ServletException;
012:        import javax.servlet.http.HttpServlet;
013:        import javax.servlet.http.HttpServletRequest;
014:        import javax.servlet.http.HttpServletResponse;
015:        import javax.rmi.PortableRemoteObject;
016:        import samplehttp.beans.SessHome;
017:        import samplehttp.beans.Sess;
018:        import samplehttp.beans.SessLocalHome;
019:        import samplehttp.beans.SessLocal;
020:
021:        /**
022:         * 
023:         */
024:        public class SampleServlet extends HttpServlet {
025:
026:            /**
027:             * Called by the server (via the service method) to allow a servlet to 
028:             * handle a GET request. 
029:             * @param request an HttpServletRequest object that contains the request 
030:             * the client has made of the servlet
031:             * @param response an HttpServletResponse object that contains the
032:             * response the servlet sends to the client
033:             * @throws IOException if an input or output error is detected when the 
034:             * servlet handles the GET request
035:             * @throws ServletException if the request for the GET could not be handled
036:             */
037:            public void doGet(HttpServletRequest request,
038:                    HttpServletResponse response) throws IOException,
039:                    ServletException {
040:                //System.out.println(Thread.currentThread().getName()+"  SampleServlet doGet");
041:                response.setContentType("text/html");
042:                PrintWriter out = response.getWriter();
043:                out.println("<html>");
044:                out.println("<head>");
045:                out.println("<title>");
046:                out.println("Sample Servlet </title>");
047:                out.println("</head>");
048:                out
049:                        .println("<body style=\"background : white; color : black;\">");
050:                out.println("<h1>Sample Servlet accessing a protected EJB");
051:
052:                out.println("<h2>Initial context / UserTransaction </h2>");
053:                out.println("<ul>");
054:                Context initialContext = null;
055:                try {
056:                    initialContext = new InitialContext();
057:                    out.println("<li>Initial context OK</li>");
058:                } catch (Exception e) {
059:                    out.print("<li>Cannot get initial context for JNDI: ");
060:                    out.println(e + "</li>");
061:                    return;
062:                }
063:
064:            }
065:
066:            public void doPost(HttpServletRequest request,
067:                    HttpServletResponse response) throws IOException,
068:                    ServletException {
069:
070:                //System.out.println(Thread.currentThread().getName()+"  SampleServlet doPost");
071:                StringTokenizer st = new StringTokenizer(request
072:                        .getParameter("param"));
073:                String timetowait = (String) st.nextElement();
074:                String flag = (String) st.nextElement();
075:                //System.out.println("timetowait = "+timetowait);
076:                Long lg = new Long(timetowait.trim());
077:                long twait = lg.longValue();
078:
079:                int ejbflg = new Integer(flag).intValue();
080:                // ejbflg = 0 ---> no ejb
081:                // ejbflg = 1 ---> ejb via remote interface
082:                // ejbflg = 2 ---> ejb via local interface
083:                // ejbflg = 3 ---> ejb via remote interface + getConnection
084:
085:                //System.out.println("waiting : "+twait +" ejb = "+ejbflg);
086:                if (ejbflg == 0) {
087:                    //System.out.println("No ejb");
088:                    try {
089:                        Thread.currentThread().sleep(twait);
090:                    } catch (InterruptedException e) {
091:                        //System.out.println("sleep:"+e);
092:                    }
093:
094:                } else {
095:
096:                    Context initialContext = null;
097:                    try {
098:                        initialContext = new InitialContext();
099:
100:                    } catch (Exception e) {
101:                        System.err
102:                                .println("Cannot get initial context for JNDI: "
103:                                        + e);
104:                        return;
105:                    }
106:                    if (ejbflg == 2) {
107:                        // ejbflg = 2 ---> ejb via local interface
108:                        SessLocalHome sh = null;
109:                        SessLocal s = null;
110:                        try {
111:                            sh = (SessLocalHome) initialContext
112:                                    .lookup("java:comp/env/ejb/SessLocal");
113:                            s = sh.create();
114:                            s.process(twait);
115:                            s.remove();
116:                        } catch (Exception e) {
117:                            System.err.println("Exception caught: " + e);
118:                            return;
119:                        }
120:                    } else {
121:                        // ejbflg = 1 or 3 ---> ejb via remote interface
122:                        SessHome sh = null;
123:                        Sess s = null;
124:                        try {
125:                            sh = (SessHome) PortableRemoteObject.narrow(
126:                                    initialContext
127:                                            .lookup("java:comp/env/ejb/Sess"),
128:                                    SessHome.class);
129:                            s = sh.create();
130:                            if (ejbflg == 1) {
131:                                // ejbflg = 1 ---> ejb via remote interface
132:                                s.process(twait);
133:                            } else {
134:                                // ejbflg = 3 ---> ejb business method + getConnection
135:                                s.processwithconn(twait);
136:                            }
137:                            s.remove();
138:                        } catch (Exception e) {
139:                            System.err.println("Exception caught: " + e);
140:                            return;
141:                        }
142:                    }
143:                }
144:                String okmsg = "OK\n";
145:                response.setContentLength(okmsg.length());
146:                PrintWriter out = response.getWriter();
147:                out.print(okmsg);
148:
149:            }
150:
151:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.