1 package example; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import java.util.logging.Level; 7 import java.util.logging.Logger; 8 9 import javax.ejb.CreateException; 10 11 import javax.naming.Context; 12 import javax.naming.InitialContext; 13 import javax.naming.NamingException; 14 15 import javax.servlet.ServletConfig; 16 import javax.servlet.ServletException; 17 import javax.servlet.http.HttpServlet; 18 import javax.servlet.http.HttpServletRequest; 19 import javax.servlet.http.HttpServletResponse; 20 21 30 public class HelloJndiServlet extends HttpServlet { 31 static protected final Logger log = 32 Logger.getLogger(HelloJndiServlet.class.getName()); 33 34 39 private HelloHome helloHome; 40 41 45 public void init(ServletConfig config) 46 throws ServletException 47 { 48 super.init(config); 49 50 53 try { 54 Context ejb = (Context) new InitialContext().lookup("java:comp/env/ejb"); 55 56 60 helloHome = (HelloHome) ejb.lookup("hello"); 61 62 } catch (NamingException e) { 63 throw new ServletException("java:comp/env/ejb",e); 64 } 65 } 66 67 74 public void doGet(HttpServletRequest request, 75 HttpServletResponse response) 76 throws IOException, ServletException 77 { 78 try { 79 PrintWriter out = response.getWriter(); 80 81 response.setContentType("text/html"); 82 83 Hello hello = helloHome.create(); 84 85 out.print("Message: "); 86 out.println(hello.hello()); 87 } catch (CreateException e) { 88 throw new ServletException(e); 89 } 90 } 91 } 92 | Popular Tags |