10-04-2012, 02:41 PM
AN OVERVIEW OF SERVLET AND JSP TECHNOLOGY
Overview.pdf (Size: 167.98 KB / Downloads: 79)
A Servlet’s Job
Servlets are Java programs that run on Web or application servers, acting as a middle
layer between requests coming from Web browsers or other HTTP clients and databases
or applications on the HTTP server.
Read the explicit data sent by the client.
The end user normally enters this data in an HTML form on a Web
page. However, the data could also come from an applet or a custom
HTTP client program. Chapter 4 discusses how servlets read this data.
Read the implicit HTTP request data sent by the browser.
Figure 1–1 shows a single arrow going from the client to the Web
server (the layer where servlets and JSP execute), but there are really
two varieties of data: the explicit data that the end user enters in a
form and the behind-the-scenes HTTP information. Both varieties are
critical. The HTTP information includes cookies, information about
media types and compression schemes the browser understands, and
so forth; it is discussed in Chapter 5.
Generate the results.
This process may require talking to a database, executing an RMI or
EJB call, invoking a Web service, or computing the response directly.
Your real data may be in a relational database. Fine. But your database
probably doesn’t speak HTTP or return results in HTML, so the Web
browser can’t talk directly to the database. Even if it could, for security
reasons, you probably would not want it to. The same argument
applies to most other applications. You need the Web middle layer to
extract the incoming data from the HTTP stream, talk to the application,
and embed the results inside a document.
Why Build Web Pages Dynamically?
After Marty wrote the first edition of Core Servlets and JavaServer Pages, various of
his non-software-savvy friends and relations would ask him what his book was about.
Marty would launch into a long, technical discussion of Java, object-oriented programming,
and HTTP, only to see their eyes immediately glaze over. Finally, in exasperation,
they would ask, “Oh, so your book is about how to make Web pages, right?”
“Well, no,” the answer would be, “They are about how to make programs that
make Web pages.”
A Quick Peek at Servlet Code
Now, this is hardly the time to delve into the depths of servlet syntax. Don’t worry,
you’ll get plenty of that throughout the book. But it is worthwhile to take a quick look
at a simple servlet, just to get a feel for the basic level of complexity.
Listing 1.1 shows a simple servlet that outputs a small HTML page to the client.
The code is explained in detail in Chapter 3 (Servlet Basics), but for now, just
notice four points:
Efficient
With traditional CGI, a new process is started for each HTTP request. If the CGI
program itself is relatively short, the overhead of starting the process can dominate
the execution time. With servlets, the Java virtual machine stays running and handles
each request with a lightweight Java thread, not a heavyweight operating system process.
Similarly, in traditional CGI, if there are N requests to the same CGI program,
the code for the CGI program is loaded into memory N times.
Convenient
Servlets have an extensive infrastructure for automatically parsing and decoding
HTML form data, reading and setting HTTP headers, handling cookies, tracking sessions,
and many other such high-level utilities. In CGI, you have to do much of this
yourself. Besides, if you already know the Java programming language, why learn
Perl too? You’re already convinced that Java technology makes for more reliable and
reusable code than does Visual Basic, VBScript, or C++. Why go back to those languages
for server-side programming?
Powerful
Servlets support several capabilities that are difficult or impossible to accomplish
with regular CGI. Servlets can talk directly to the Web server, whereas regular CGI
programs cannot, at least not without using a server-specific API. Communicating
with the Web server makes it easier to translate relative URLs into concrete path
names, for instance.
The Role of JSP
A somewhat oversimplified view of servlets is that they are Java programs with
HTML embedded inside of them. A somewhat oversimplified view of JSP documents
is that they are HTML pages with Java code embedded inside of them.