What Is a JSP?

A JSP is just another servlet, and like HTTP servlets, a JSP is a server-side Web component that can be used to generate dynamic Web pages. You can think of JSPs as HTML web pages with embedded Java, whereas servlets are Java classes that generate HTML.

To illustrate this difference, Listings 13.1 and 13.2 are the same Web page coded as a servlet and as a JSP, respectively. Each Web page simply reads a parameter called name from the HTTP request and creates an HTML page displaying the value of the name parameter. Listing 13.1 shows the servlet, and Listing 13.2 shows the JSP.

Listing 13.1. Simple Dynamic Page as a Servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Hello extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res)
                 throws ServletException, IOException {
        res.setContentType ("text/html");
        PrintWriter out = res.getWriter();
        String name = req.getParameter("name");
        out.println ("<HTML>");
        out.println ("<HEAD><TITLE>Hello</TITLE></HEAD>");
        out.println ("<BODY>");
        out.println ("<H1>Hello " + name + "</H1>");
        out.println ("</BODY>");
        out.println ("</HTML>");
    }
}

Listing 13.2. Same Dynamic Page as a JSP
<HTML>
  <HEAD><TITLE>Hello</TITLE></HEAD>
  <BODY>
    <H1>Hello ${param.name}</H1>
  </BODY>
</HTML>

NOTE

This syntax only works for JSP 2.0 containers (J2EE 1.4 onward). For JSP 1.2 or earlier (J2EE up to 1.3) the following should replace the contents of the <BODY> tag.

<H1>Hello <%= request.getParameter("name") %> </H1>


Here you can see that the JSP not only requires far less typing, but, from the programmer's point of view, a lot of the work is being done for you. How the JSP achieves the same effect with far less code will soon become clear.

Separating Roles

With a servlet, the Java programmer was forced to generate the entire HTML. With a JSP, it is much easier to separate the HTML from the application tasks. With the use of the JavaServer Pages Standard Tag Library (JSTL), JavaBeans, and custom tag libraries, this separation is even more explicit. JSTL and tag libraries are covered in more detail on Day 14.

Using JSPs, the Web designer can concentrate on the design and development of the HTML page. When a dynamic element is needed, the developer can use a pre-written bean or tag library to provide the data. The Java programmer can concentrate on developing a useful set of beans and tag libraries encapsulating the complex Java required to retrieve the data from an EJB, a database, or any other data source.

Translation and Execution

JSPs differ from servlets in one other respect. Before execution, a Web container converts the JSP into a Java servlet. This is done in two stages:

  1. The JSP text is translated into Java code.

  2. The Java code is compiled into a servlet.

The resulting servlet class processes HTTP requests. This translate and compile process is performed once before the first HTTP request is processed. The JSP lifecycle is covered in more detail later.

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

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