KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samplehttp > servlet > SampleServlet


1 package samplehttp.servlet;
2
3 //import java
4
import java.io.IOException;
5 import java.io.PrintWriter;
6 import java.util.StringTokenizer;
7
8 //import javax
9
import javax.naming.Context;
10 import javax.naming.InitialContext;
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 import javax.rmi.PortableRemoteObject;
16 import samplehttp.beans.SessHome;
17 import samplehttp.beans.Sess;
18 import samplehttp.beans.SessLocalHome;
19 import samplehttp.beans.SessLocal;
20
21 /**
22  *
23  */

24 public class SampleServlet extends HttpServlet {
25
26     /**
27      * Called by the server (via the service method) to allow a servlet to
28      * handle a GET request.
29      * @param request an HttpServletRequest object that contains the request
30      * the client has made of the servlet
31      * @param response an HttpServletResponse object that contains the
32      * response the servlet sends to the client
33      * @throws IOException if an input or output error is detected when the
34      * servlet handles the GET request
35      * @throws ServletException if the request for the GET could not be handled
36      */

37     public void doGet(HttpServletRequest request, HttpServletResponse response)
38         throws IOException, ServletException {
39         //System.out.println(Thread.currentThread().getName()+" SampleServlet doGet");
40
response.setContentType("text/html");
41         PrintWriter out = response.getWriter();
42         out.println("<html>");
43         out.println("<head>");
44         out.println("<title>");
45         out.println("Sample Servlet </title>");
46         out.println("</head>");
47         out.println("<body style=\"background : white; color : black;\">");
48         out.println("<h1>Sample Servlet accessing a protected EJB");
49
50         out.println("<h2>Initial context / UserTransaction </h2>");
51         out.println("<ul>");
52         Context initialContext = null;
53         try {
54             initialContext = new InitialContext();
55             out.println("<li>Initial context OK</li>");
56         } catch (Exception e) {
57             out.print("<li>Cannot get initial context for JNDI: ");
58             out.println(e + "</li>");
59             return;
60         }
61      
62     }
63     public void doPost(HttpServletRequest request, HttpServletResponse response)
64         throws IOException, ServletException {
65  
66         //System.out.println(Thread.currentThread().getName()+" SampleServlet doPost");
67
StringTokenizer st = new StringTokenizer(request.getParameter("param"));
68         String timetowait = (String)st.nextElement();
69         String flag = (String)st.nextElement();
70         //System.out.println("timetowait = "+timetowait);
71
Long lg = new Long(timetowait.trim());
72         long twait = lg.longValue();
73
74         int ejbflg = new Integer(flag).intValue();
75         // ejbflg = 0 ---> no ejb
76
// ejbflg = 1 ---> ejb via remote interface
77
// ejbflg = 2 ---> ejb via local interface
78
// ejbflg = 3 ---> ejb via remote interface + getConnection
79

80         //System.out.println("waiting : "+twait +" ejb = "+ejbflg);
81
if( ejbflg == 0 ) {
82             //System.out.println("No ejb");
83
try {
84                 Thread.currentThread().sleep(twait);
85             } catch (InterruptedException e) {
86                 //System.out.println("sleep:"+e);
87
}
88
89         } else {
90
91             Context initialContext = null;
92             try {
93                 initialContext = new InitialContext();
94  
95             } catch (Exception e) {
96                 System.err.println("Cannot get initial context for JNDI: "+e);
97                 return;
98             } if( ejbflg == 2) {
99                 // ejbflg = 2 ---> ejb via local interface
100
SessLocalHome sh = null;
101                 SessLocal s = null;
102                 try {
103                     sh = (SessLocalHome) initialContext.lookup("java:comp/env/ejb/SessLocal");
104                     s = sh.create();
105                     s.process(twait);
106                     s.remove();
107                 } catch (Exception e) {
108                     System.err.println("Exception caught: " + e );
109                     return;
110                 }
111             } else {
112                 // ejbflg = 1 or 3 ---> ejb via remote interface
113
SessHome sh = null;
114                 Sess s = null;
115                 try {
116                     sh = (SessHome) PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Sess"), SessHome.class);
117                     s = sh.create();
118                     if( ejbflg == 1) {
119                         // ejbflg = 1 ---> ejb via remote interface
120
s.process(twait);
121                     } else {
122                         // ejbflg = 3 ---> ejb business method + getConnection
123
s.processwithconn(twait);
124                     }
125                     s.remove();
126                 } catch (Exception e) {
127                     System.err.println("Exception caught: " + e );
128                     return;
129                 }
130             }
131         }
132         String okmsg = "OK\n";
133         response.setContentLength(okmsg.length());
134         PrintWriter out = response.getWriter();
135         out.print(okmsg);
136         
137
138     }
139
140 }
141
Popular Tags