1 package example.entity.home; 2 3 import java.io.*; 4 5 import javax.servlet.*; 6 import javax.servlet.http.*; 7 8 import javax.ejb.*; 9 import javax.naming.*; 10 11 15 public class HomeServlet extends GenericServlet { 16 19 private Home home; 20 21 24 public void init() 25 throws ServletException 26 { 27 try { 28 32 Context ejb = (Context) new InitialContext().lookup("java:comp/env/ejb"); 33 34 38 home = (Home) ejb.lookup("home"); 39 } catch (Exception e) { 40 throw new ServletException(e); 41 } 42 43 if (home == null) 44 throw new ServletException("can't find home"); 45 } 46 47 50 public void service(ServletRequest request, ServletResponse response) 51 throws IOException, ServletException 52 { 53 PrintWriter pw = response.getWriter(); 54 55 response.setContentType("text/html"); 56 57 try { 58 pw.println("message: " + home.hello() + "<br>"); 59 60 pw.println("1 + 3 = " + home.add(1, 3) + "<br>"); 61 pw.println("7 + 1 = " + home.add(7, 1) + "<br>"); 62 } catch (Exception e) { 63 throw new ServletException(e); 64 } 65 } 66 } 67 | Popular Tags |