// BooksRemote.java package ent1; import java.util.Vector; import javax.ejb.Remote; @Remote public interface BooksRemote { void addBook(String title); Vector getBooks(); boolean removeBook(String title); void removeBooks(); }
// BooksBean.java package ent1; import java.util.Vector; import javax.annotation.PreDestroy; import javax.ejb.Remove; import javax.ejb.Stateful; @Stateful public class BooksBean implements BooksRemote { private Vector books = new Vector(); public void addBook(String title) { books.add(title); } public Vector getBooks() { return books; } public boolean removeBook(String title) { if ( books.contains(title)) { books.remove(title); return true; } else return false; } // Remove method @Remove public void removeBooks() { System.out.println("About to destroy EJB instance ..."); } }
<html> <body> <form action="books"> Enter Book Title : <input type="Text" name="title" /> <p/> <input type="submit" value="Add Book" name="op" /> <input type="submit" value="List Books" name="op" /> </form> </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>BooksServlet</servlet-name> <servlet-class>BooksServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>BooksServlet</servlet-name> <url-pattern>/books</url-pattern> </servlet-mapping> <ejb-ref> <ejb-ref-name>ejb/books</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <home/> <remote>ent1.BooksRemote</remote> </ejb-ref> </web-app>
import ent1.BooksRemote; import java.io.*; import javax.naming.InitialContext; import javax.servlet.*; import javax.servlet.http.*; public class BooksServlet extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { HttpSession s = request.getSession(true); BooksRemote books = (BooksRemote) s.getAttribute("booksbean"); // if current session doesn't have an instance of EJB then get access to EJB and store reference to Session if (books == null) { InitialContext ctx = new InitialContext(); books = (BooksRemote) ctx.lookup("java:comp/env/ejb/books"); s.setAttribute("booksbean", books); } // findout the required operation - Add Book or List Books String op = request.getParameter("op"); String title = request.getParameter("title"); if (op.equals("Add Book")) { books.addBook(title); } else { for (String st : books.getBooks()) { out.println(st + "<br/>"); } } } catch (Exception ex) { out.println(ex.getMessage()); } finally { out.close(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } }
http://localhost:8080/ent1-war/books.html