package com.brainysoftware.tassie.servlet; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import javax.naming.*; import javax.rmi.PortableRemoteObject; import com.brainysoftware.tassie.ejb.Search; import com.brainysoftware.tassie.ejb.SearchHome; public class SearchServlet extends HttpServlet { private String keyword; /**Process the HTTP Get request*/ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { displayPage(request, response); } /**Process the HTTP Post request*/ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { keyword = request.getParameter("keyword"); displayPage(request, response); } private void displayPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* In addition to the header and footer, There are 4parts in this page: 1. The Welcome Title 2. Search form 3. The search result table 4. The link to the Check Shopping Cart page */ response.setContentType("text/html"); PrintWriter out = response.getWriter(); // header out.println(""); out.println(""); out.println("Welcome to Tassie Online Bookstore"); out.println(""); out.println(""); out.println("
"); // Welcome Title out.println("

Welcome To Tassie Online Bookstore

"); // Search form out.println("
"); out.println("
"); out.println("Title: "); out.println(""); out.println("
"); // Search result out.println("
"); out.println("

Search Result

"); out.println("
"); displaySearchResult(out); out.println("
"); // Link to the Check Shopping Cart link out.println("
"); out.println("Check Shopping Cart"); // footer out.println("
"); out.println(""); out.println(""); // Displaying the Search form } /**Clean up resources*/ private void displaySearchResult(PrintWriter out) { if (keyword!=null && !keyword.trim().equals("")) { // keyword okay, display the result here. Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); properties.put(Context.PROVIDER_URL, "localhost:1099"); try { // Get a naming context InitialContext jndiContext = new InitialContext(properties); // Get a reference to the Bean Object ref = jndiContext.lookup("Search"); // Get a reference from this to the Bean's Home interface SearchHome home = (SearchHome) PortableRemoteObject.narrow (ref, SearchHome.class); // Create an Adder object from the Home interface Search searchBean = home.create(); ArrayList arrayList = searchBean.search(keyword); int rowCount = arrayList.size(); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); for (int i=0; i"); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); } out.println("
TitleAuthorPublisher 
" + s[1] + "" + s[2] + "" + s[3] + "Details
"); } catch(Exception e) { System.out.println(e.toString()); } } } }