2014年4月16日 星期三

What is a JSP?

JavaServer Pages

Yesterday, you looked at developing Web applications using Java servlets.
Servlets have the advantage of being able to generate the HTML Web page
dynamically. The disadvantage of servlets is the fact that the developer must
generate a lot of HTML formatting information from within Java. Servlets can
be described as large amounts of boring HTML println() statements inter-
spersed with small amounts of interesting Java code.
Servlets make it difficult to differentiate the presentation layer from the logic
layer of an application. This duality of purpose means that servlets do not allow
the roles of HTML designer and Java programmer to be easily separated.
Writing servlets requires the members of the development team to be either
• Java programmers who must learn HTML and Web design
• Web designers who must learn Java

In practice, there are very few Java programmers who make good Web design-
ers, and fewer Web designers who make good Java programmers.
JavaServer Pages are servlets that are written in HTML. Actually, there is a bit
more to it than that, but the Java code on a JSP is usually either non-existent or
very simple, and can be readily understood by non-Java programmers.In today’s lesson, you will learn
• What a JSP is and how its implementation differs from servlets
• The JSP lifecycle—what you have to do and what the Web server will do for you
• How to deploy a JSP
• How to use JavaBeans to hide Java functionality from the JSP
• How to develop a Web application using JSPs
Today’s work builds directly on the knowledge gained yesterday because many of the
mechanisms used in JSPs are the same as servlets.

What is a JSP?
A JSP is just another servlet, and like HTTP servlets, a JSP is a server-side Web compo-
nent 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: }


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 separa-
tion 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 develop-
ing 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 per-
formed once before the first HTTP request can be processed. The JSP lifecycle is cov-
ered in more detail later.

沒有留言:

張貼留言