Agency Case Study

You will now add a servlet to the Agency case study. The servlet sends the contents of the agency database tables to be displayed by a browser. The rows of the tables are formatted as an HTML table.

The servlet first outputs an HTML form on which the user selects the name of a table from a pull-down list (see Figure 12.25).

Figure 12.25. AgencyTable form.


After the user clicks the Submit Query button, the servlet displays the contents of this table on the same page.

AgencyTable Servlet Code

The AgencyTable servlet overrides the init() method to obtain the servlet context along with the JNDI context for the Agency EJB. The Agency EJB select() method is used in the doGet() method to obtain the rows of table data from the Agency database.

public void init(){
    context = getServletContext();
    try {
        InitialContext ic = new InitialContext();
        Object lookup = ic.lookup("java:comp/env/ejb/Agency");
        AgencyHome home = (AgencyHome)PortableRemoteObject.narrow(lookup, AgencyHome.class);
        agency = home.create();
    }
....
}

The AgencyTable HTML page is generated in the following code.

private final String tables = "<OPTION>Applicant<OPTION>ApplicantSkill
 <OPTION>Customer<OPTION>Job<OPTION>JobSkill <OPTION>Location<OPTION>Matched<OPTION>Skill";
...

out.println ("<HTML>");
out.println ("<HEAD><TITLE>" + agencyName + " List Tables</TITLE></HEAD>");
out.println ("<BODY><FONT FACE=ARIAL COLOR=DARKBLUE");
out.println ("<H1><FONT SIZE=+3>" + agencyName + " List Tables</FONT></H1>");
out.println ("<P>Select a table from the list to display the contents</P>");

out.println ("<FORM>");
out.println ("<SELECT NAME="tableList" SIZE=1>" + tables + "</SELECT>");
out.println ("<INPUT TYPE=submit>");
out.println ("</FORM>");
out.println ("</FONT></BODY>");
out.println ("</HTML>");

The String variable tables contains the names of the database tables encoded in a HTML <OPTION> list.

The <SELECT> tag defines a parameter called tableList. This parameter is used to pass the name of the selected table from the form to the servlet.

The following code checks to see if the parameter has been set. If it has, the outputTable() method is called.

tableName = req.getParameter("tableList");
if (tableName != null) {
    outputTable(out, tableName, res);

In the outputTable() method, it is the agency.select bean that actually does the work. It returns a list of all the rows in the table.

java.util.List query;   // list of String[], first row = column names
query = agency.select(tableName);

The rows of the table are displayed as an HTML table so the columns line up correctly. The attributes to the HTML TABLE tag set the border and background colors, and the cell padding is increased to improve readability. Rows in a table are separated by <TR>...</TR> tags.

out.println ("<TABLE BORDER=1 BORDERCOLOR=SILVER BGCOLOR=IVORY CELLPADDING=5><TR>");

The first item in the list is an array containing the names of the columns in the table. This is output as HTML table header cells.

String[] headerRow = (String[])query.get(0);
for (int i = 0; i < headerRow.length; i++) {
    out.println ("<TH ALIGN=LEFT>" + headerRow[i] + "</TH>");
}
out.println ("</TR>");

The remainder of the rows of the table are output as HTML table data cells.

for (int i = 1; i < query.size(); i++) {
    out.println ("<TR>");
    String[] row = (String[])query.get(i);
    for (int r = 0; r < row.length; r++) {
        out.println ("<TD>" + row[r] + "</TD>");
    }
    out.println ("</TR>");
}
out.println ("</TABLE>");

Deploying the AgencyTable Servlet

Use deploytool to add the AgencyTable Servlet as a servlet Web component in the Agency application. You will need to map the JNDI names used in the code, as shown in Figure 12.26.

Figure 12.26. deploytool EJB Refs.


Figure 12.27 shows the output.

Figure 12.27. AgencyTable servlet output.


The complete listing of the AgencyTable servlet is provided in Listing 12.13 for completeness.

Listing 12.13. AgencyTable Servlet Code
  1: import java.io.*;
  2: import javax.servlet.*;
  3: import javax.servlet.http.*;
  4: import agency.*;
  5: import javax.naming.*;
  6: import java.rmi.*;
  7: import javax.rmi.*;
  8: import javax.ejb.*;
  9:
 10: public class AgencyTableServlet extends HttpServlet
 11: {
 12:     private final String tables = "<OPTION>Applicant<OPTION>ApplicantSkill
 <OPTION>Customer<OPTION>Job<OPTION>JobSkill <OPTION>Location<OPTION>Matched<OPTION>Skill";
 13:     private Agency agency;
 14:     private ServletContext context;
 15:
 16:     public void init(){
 17:         context = getServletContext();
 18:         try {
 19:             InitialContext ic = new InitialContext();
 20:             Object lookup = ic.lookup("java:comp/env/ejb/Agency");
 21:             AgencyHome home = (AgencyHome)PortableRemoteObject.narrow(lookup,
 AgencyHome.class);
 22:             agency = home.create();
 23:         }
 24:         catch (NamingException ex) {
 25:             context.log("NamingException in AgencyTableServlet.init", ex);
 26:         }
 27:         catch (ClassCastException ex) {
 28:             context.log("ClassCastException in AgencyTableServlet.init", ex);
 29:         }
 30:         catch (CreateException ex) {
 31:             context.log("CreateException in AgencyTableServlet.init", ex);
 32:         }
 33:         catch (RemoteException ex) {
 34:             context.log("RemoteException in AgencyTableServlet.init", ex);
 35:         }
 36:     }
 37:
 38:     public void destroy () {
 39:         context = null;
 40:         agency = null;
 41:     }
 42:
 43:     private void outputTable (PrintWriter out, String tableName, HttpServletResponse
 res) throws RemoteException{
 44:
 45:         java.util.List query;   // first row = column names
 46:         query = agency.select(tableName);
 47:
 48:         out.println ("<P><FONT SIZE=+1>Listing of " + tableName + " table</FONT></P>");
 49:         out.println ("<TABLE BORDER=1 BORDERCOLOR=SILVER BGCOLOR=IVORY
 CELLPADDING=5><TR>");
 50:
 51:         String[] headerRow = (String[])query.get(0);
 52:         for (int i = 0; i < headerRow.length; i++) {
 53:             out.println ("<TH ALIGN=LEFT>" + headerRow[i] + "</TH>");
 54:         }
 55:         out.println ("</TR>");
 56:
 57:         for (int i = 1; i < query.size(); i++) {
 58:             out.println ("<TR>");
 59:             String[] row = (String[])query.get(i);
 60:             for (int r = 0; r < row.length; r++) {
 61:                 out.println ("<TD>" + row[r] + "</TD>");
 62:             }
 63:             out.println ("</TR>");
 64:         }
 65:         out.println ("</TABLE>");
 66:     }
 67:
 68:     public void doGet(HttpServletRequest req, HttpServletResponse res)
 69:                  throws IOException {
 70:         try {
 71:             String agencyName = agency.getAgencyName();
 72:             String tableName = null;
 73:
 74:             res.setContentType ("text/html");
 75:             PrintWriter out = res.getWriter();
 76:
 77:             // print out form
 78:             out.println ("<HTML>");
 79:             out.println ("<HEAD><TITLE>" + agencyName + " List Tables</TITLE></HEAD>");
 80:             out.println ("<BODY><FONT FACE=ARIAL COLOR=DARKBLUE");
 81:             out.println ("<H1><FONT SIZE=+3>" + agencyName + " List Tables</FONT></H1>");
 82:
 83:             tableName = req.getParameter("tableList");
 84:             if (tableName != null) {
 85:                outputTable(out, tableName, res);
 86:             }
 87:             out.println ("<P><BR>Select a table from the list to display the contents<
/BR></P>");
 88:             out.println ("<FORM>");
 89:             out.println ("<SELECT NAME="tableList" SIZE=1>" + tables + "</SELECT>");
 90:             out.println ("<INPUT TYPE=submit>");
 91:             out.println ("</FORM>");
 92:
 93:             out.println ("</FONT></BODY>");
 94:             out.println ("</HTML>");
 95:         }
 96:         catch (RemoteException ex) {
 97:             context.log ("RemoteException in AgencyTableServlet.doGet", ex);
 98:             res.sendError (res.SC_INTERNAL_SERVER_ERROR);
 99:         }
100:     }
101: }
						

Use a browser to access your servlet and check that you can retrieve the data from the database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.81.43