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: }
|