Java Reference
In-Depth Information
* @param response encapsulates the response from the servlet
*/
This is the class definition. We have elected to inherit from the most com-
mon servlet type, or HTTPS ervlet. We get a request object and a response
object. These represent the initial request and the page that we will send
back as a response.
public void performTask(
HttpServletRequest request,
HttpServletResponse response) {
Listing 3.4 shows the meat of the workhorse method. This is the proverbial
10 KB script. The performTask method consolidates all of the different paths
into the servlet to process the request. These may take the form of HTTP GET s
or POST s. In this method, we can begin to see where we start to break down
architecturally. We are printing the HTML response page directly from this
method. These print statements are sprinkled throughout the method, along
with database requests and a little error processing.
Listing 3.4
We hopelessly tangle the model and return-trip view
try {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
+ "HTML 4.0 Transitional//EN\">\n"
+ "<HTML>\n"
+ "<HEAD><TITLE>Message Board</TITLE></HEAD>\n"
+ "<BODY>\n");
B View logic
C View
logic
D Model
logic
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver")
.newInstance();
Connection con = null;
E Model
logic
String url = "jdbc:db2:board";
con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
ResultSet rs =
stmt.executeQuery("SELECT subject, author, board" +
" from posts");
F Model
logic
G View
logic
out.println("<h1>Message board posts</h1>");
out.println("<TABLE border=\"1\">");
out.println("<TD><b>subject</b></TD>\n");
out.println("<TD><b>author</b></TD>\n");
out.println("<TD><b>board</b></TD>\n");
Search WWH ::




Custom Search