11.6 Comparing Servlets to JSP Pages

In Section 4.3, we presented an example of a servlet that outputs the values of three designated form parameters. The code for that servlet is repeated here in Listing 11.4. Listing 11.5 (Figure 11-4) shows a version rewritten in JSP, using JSP expressions to access the form parameters. The JSP version is clearly superior : shorter, simpler, and easier to maintain.

Figure 11-4. Result of ThreeParams.jsp .

graphics/11fig04.jpg

Now, this is not to say that all servlets will convert to JSP so cleanly. JSP works best when the structure of the HTML page is fixed but the values at various places need to be computed dynamically. If the structure of the page is dynamic, JSP is less beneficial. Sometimes servlets are better in such a case. And, of course, if the page consists of binary data or has little static content, servlets are clearly superior. Furthermore, sometimes the answer is neither servlets nor JSP alone, but rather a combination of the two. For details, see Chapter 15 (Integrating Servlets and JSP: The Model View Controller (MVC) Architecture).

Listing 11.4 ThreeParams.java
 package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ThreeParams extends HttpServlet {   public void doGet(HttpServletRequest request,                     HttpServletResponse response)       throws ServletException, IOException {     response.setContentType("text/html");     PrintWriter out = response.getWriter();     String title = "Reading Three Request Parameters";     String docType =       "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +       "Transitional//EN\">\n";     out.println(docType +                 "<HTML>\n" +                 "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +                 "<BODY BGCOLOR=\"#FDF5E6\">\n" +                 "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +                 "<UL>\n" +                 "  <LI><B>param1</B>: "                 + request.getParameter("param1") + "\n" +                 "  <LI><B>param2</B>: "                 + request.getParameter("param2") + "\n" +                 "  <LI><B>param3</B>: "                 + request.getParameter("param3") + "\n" +                 "</UL>\n" +                 "</BODY></HTML>");   } } 
Listing 11.5 ThreeParams.jsp
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Reading Three Request Parameters</TITLE> <LINK REL=STYLESHEET       HREF="JSP-Styles.css"       TYPE="text/css"> </HEAD> <BODY> <H1>Reading Three Request Parameters</H1> <UL>   <LI><B>param1</B>:  <%= request.getParameter("param1") %>  <LI><B>param2</B>:  <%= request.getParameter("param2") %>  <LI><B>param3</B>:  <%= request.getParameter("param3") %>  </UL> </BODY></HTML> 


Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net