Do JSPs Replace Servlets?
Home ] Up ] What's  a Java Server Page? ] How Does a JSP Work? ] [ Do JSPs Replace Servlets? ] JSP Expressions vs. Scriplets ] Conditional JSPs ] JSP Declarations ] A JSP Hit Counter ]

 

 

NO!

It is important to recognize that, in any given project, and especially in larger ones, the right solution is a suitable mix of JSPs and servlets.

Why?

  • Servlets (Java code) is good at processing, database access, etc.  They are much less capable for presentation.
  • JSPs are much easier to use for presentation.  In particular, standard HTML development tools such as Dreamweaver can be used for JSP development. However, JSPs are not ideal for processing, database access and the like. 

We want to discourage anyone who thinks that going completely JSP or completely servlet is a good idea.

A mix of servlets and JSPs allows a project to separate the form from the function, and to farm out development and maintenance work to the appropriate person/group.

For example, remember the code we wrote for a simple form data servlet?  Here it is again:
/*
 * Form1Servlet.java
 *
 * Created on November 24, 2006
 */

package formExamples;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * A servlet to process the parameters from a form.
 *
 * @author Bryan Higgs
 * @version
 */
public class Form1Servlet extends HttpServlet
{
  /** 
   * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
   * @param request servlet request
   * @param response servlet response
   */
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException
  {
    response.setContentType("text/html; charset=UTF-8");
    PrintWriter out = response.getWriter();
    String docType = "<!DOCTYE html PUBLIC \"-//w3c//DTD XHTML 1.0 Strict//EN\"" + 
	             "         \"http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
    out.println(docType);
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet Form1Servlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet Form1Servlet at " + request.getContextPath () + "</h1>");
        out.println("<ul>");
        out.println(  "<li><b>first-name:</b> " + request.getParameter("first-name") + "</li>");
        out.println(  "<li><b>last-name:</b> " + request.getParameter("last-name") + "</li>");
        out.println(  "<li><b>occupation:</b> " + request.getParameter("occupation") + "</li>");
        out.println("</ul>");
        out.println("</body>");
        out.println("</html>");
    out.close();
  }
  
  /** 
   * Handles the HTTP <code>GET</code> method.
   * @param request servlet request
   * @param response servlet response
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException
  {
    processRequest(request, response);
  }
  
  /** 
   * Handles the HTTP <code>POST</code> method.
   * @param request servlet request
   * @param response servlet response
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException
  {
    processRequest(request, response);
  }
  
  /** 
   * Returns a short description of the servlet.
   */
  public String getServletInfo()
  {
    return "Short description";
  }
}

Here's the equivalent JSP:

<!DOCTYE html PUBLIC "-//w3c//DTD XHTML 1.0 Strict//EN" 
                     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>JSP Form1JSP</title>
  </head>
  <body>
    <h1>JSP Form1JSP at <%= request.getContextPath() %></h1>
    <ul>
      <li><b>first-name:</b> <%= request.getParameter("first-name") %></li>
      <li><b>last-name:</b> <%= request.getParameter("last-name") %></li>
      <li><b>occupation:</b> <%= request.getParameter("occupation") %></li>
    </ul>
  </body>
</html>

Here's what it outputs:

Does that convince you that it's a lot less effort to implement presentation using a JSP?

 
The page was last updated February 19, 2008