1 package example.session.local; 2 3 import java.io.*; 4 5 import javax.ejb.*; 6 import javax.naming.*; 7 import javax.servlet.*; 8 import javax.servlet.http.*; 9 10 23 public class CounterServlet extends HttpServlet { 24 29 private CounterHome counterHome; 30 31 35 public void init() 36 throws ServletException 37 { 38 try { 39 45 Context ejb = (Context) new InitialContext().lookup("java:comp/env/ejb"); 46 50 counterHome = (CounterHome) ejb.lookup("session-counter"); 51 } catch (Exception e) { 52 throw new ServletException(e); 53 } 54 } 55 56 63 public void doGet(HttpServletRequest request, 64 HttpServletResponse response) 65 throws IOException, ServletException 66 { 67 try { 68 PrintWriter out = response.getWriter(); 69 70 response.setContentType("text/html"); 71 72 Counter counter = counterHome.create(); 73 74 out.print("Count: "); 75 out.print(counter.hit()); 76 out.println("<br>"); 77 78 out.print("Count: "); 79 out.print(counter.hit()); 80 out.println("<br>"); 81 } catch (CreateException e) { 82 throw new ServletException(e); 83 } 84 } 85 } 86 | Popular Tags |