1 package samplehttp.servlet; 2 3 import java.io.IOException; 5 import java.io.PrintWriter; 6 import java.util.StringTokenizer; 7 8 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 24 public class SampleServlet extends HttpServlet { 25 26 37 public void doGet(HttpServletRequest request, HttpServletResponse response) 38 throws IOException, ServletException { 39 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 StringTokenizer st = new StringTokenizer(request.getParameter("param")); 68 String timetowait = (String)st.nextElement(); 69 String flag = (String)st.nextElement(); 70 Long lg = new Long(timetowait.trim()); 72 long twait = lg.longValue(); 73 74 int ejbflg = new Integer(flag).intValue(); 75 80 if( ejbflg == 0 ) { 82 try { 84 Thread.currentThread().sleep(twait); 85 } catch (InterruptedException e) { 86 } 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 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 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 s.process(twait); 121 } else { 122 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 |