PeopleFinderServletParampublic class PeopleFinderServletParam extends HttpServlet Class: PeopleFinderServletParam
XDoclet tags: |
Fields Summary |
---|
private Logger | mLog |
Constructors Summary |
---|
public PeopleFinderServletParam()Default constructor.
super();
|
Methods Summary |
---|
public void | doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
handleRequest(req, resp);
| public void | doPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
handleRequest(req, resp);
| private void | handleRequest(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.String | personToHTML(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();
|
|