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.

The fundamental difference between servlets and JSPs is

  • Servlets generate HTML from Java code.

  • JSPs embed Java code in static 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
 1: import java.io.*;
 2: import javax.servlet.*;
 3: import javax.servlet.http.*;
 4:
 5: public class Hello extends HttpServlet {
 6:
 7:     public void doGet(HttpServletRequest req, HttpServletResponse res)
 8:                  throws ServletException, IOException {
 9:         res.setContentType ("text/html");
10:         PrintWriter out = res.getWriter();
11:         String name = req.getParameter("name");
12:         out.println ("<HTML>");
13:         out.println ("<HEAD><TITLE>Hello</TITLE></HEAD>");
14:         out.println ("<BODY>");
15:         out.println ("<H1>Hello " + name + "</H1>");
16:         out.println ("</BODY>");
17:         out.println ("</HTML>");
18:     }
19: }

Listing 13.2. Same Dynamic Page As a JSP
1: <HTML>
2:   <HEAD><TITLE>Hello</TITLE></HEAD>
3:   <BODY>
4:     <% String name = request.getParameter("name"); %>
5:     <H1>Hello <%=name%> </H1>
6:   </BODY>
7: </HTML>

Here you can see that the JSP not only requires far less typing, but 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 JavaBeans and JSP tag libraries (covered on Day 14, “JSP Tag Libraries”), this separation is even more explicit.

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 JSP must be converted into a Java servlet. This 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 processes HTTP requests. The translate and compile process is performed once before the first HTTP request can be 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
18.226.34.25