FileDocCategorySizeDatePackage
PeopleFinderServlet.javaAPI DocExample7234Thu Dec 15 22:40:04 GMT 2005com.oreilly.jent.annotation.xdoclet.web

PeopleFinderServlet

public class PeopleFinderServlet extends HttpServlet
Class: PeopleFinderServlet XDoclet tags:
web.servlet
name="PeopleFinderServlet" display-name="PeopleFinder Servlet"
web.servlet-mapping
url-pattern="/pf"
web.resource-ref
name="jdbc/MyDataSource" type="javax.sql.DataSource" auth="Container"
jboss.resource-ref
res-ref-name="jdbc/MyDataSource" jndi-name="jdbc/DataSourceX"

Fields Summary
private Logger
mLog
Constructors Summary
public PeopleFinderServlet()
Default constructor.

    
           
      
        super();
    
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)

        handleRequest(req, resp);
    
public voiddoPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)

        handleRequest(req, resp);
    
private voidhandleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Main request handler, invoked from doGet() and doPost() methods

        Person[] people = new Person[0];
        String error = null;
        
        // Convert the tag's attributes into a set of SearchArgs
        SearchArg[] sargs = new SearchArg[3];
        sargs[0].setName(PersonDAO.FIRST_NAME);
        sargs[0].setValue(request.getParameter("firstName"));
        sargs[1].setName(PersonDAO.LAST_NAME);
        sargs[1].setValue(request.getParameter("lastName"));
        sargs[2].setName(PersonDAO.EMAIL);
        sargs[2].setValue(request.getParameter("email"));
        
        // Get the PeopleFinder EJB home
        try {
            Context ctx = new InitialContext();
            PeopleFinderLocalHome home = 
                (PeopleFinderLocalHome)ctx.lookup("java:comp/env/ejb/PeopleFinder");
            PeopleFinderLocal finder = home.create();
            people = finder.findPeople(sargs);
        }
        catch (NamingException ne) {
            error = "Failure accessing EJB component";
            mLog.severe("Failed to locate/access EJB component: " + ne.getMessage());
        }
        catch (CreateException ce) {
            error = "Failure accessing EJB component";
            mLog.severe("Failed to create EJB component: " + ce.getMessage());
        }
        catch (InvalidSearchException ise) {
            error = "Invalid search parameters.";
            mLog.severe("Search parameters were invalid: " + ise.getMessage());
        }
        catch (PersistenceException pe) {
            error = "Failure accessing data source";
            mLog.severe("Persistence error during search: " + pe.getMessage());
        }
        
        // Output HTML results
        PrintWriter out = response.getWriter();
        // Print the header
        out.println("<HTML><HEAD><TITLE>Search Results</TITLE></HEAD><BODY>");
        // If there's an error, just print it, prefaced with "Error:".
        if (error != null) {
            out.println("<center><h3>Error: " + error + "</h3></center>");
        }
        // If there are no results, print a notice to that effect
        else if (people.length == 0) {
            out.println("<center><h3>No matching people found.</h3></center>");
        }
        // Otherwise, print the results, one person per row, in a table
        else {
            // Open the table
            out.println("<center><table border=\"0\">");
            out.println("<tr bgcolor=\"#CCCCCC\">");
            out.println("<th>Last name</th>");
            out.println("<th>First name</th>");
            out.println("<th>Email addresses</th></tr>");
            // Print each person's data
            for (int i = 0; i < people.length; i++) {
                Person p = people[i];
                // Use our util method to convert the person
                // data into an HTML fragment
                out.println(personToHTML(p));
            }
            out.println("</table>");
        }
        // Close the page
        out.println("</BODY></HTML>");
    
protected static java.lang.StringpersonToHTML(com.oreilly.jent.annotation.people.Person p)
Convert a Person's data into a row in the table on the results page. Note that this utility assumes a particular structure to the results page (a table with so many columns, with a particular order to the data in the columns, etc.)

        StringBuffer buf = new StringBuffer();
        buf.append("<tr bgcolor=\"#EEEEEE\">");
        buf.append("<td>" + p.getLastName() + "</td>");
        buf.append("<td>" + p.getFirstName() + "</td>");
        buf.append("<td>");
        for (int i = 0; i < p.getEmailAddresses().length; i++) {
            buf.append(p.getEmailAddresses()[i]);
            if (i < p.getEmailAddresses().length - 1) {
                buf.append("<br>");
            }
        }
        buf.append("</td>");
        buf.append("</tr>");
        return buf.toString();