没有合适的资源?快使用搜索试试~ 我知道了~
首页Servlet与JSP核心编程第二版第一卷英文 Core Servlets and JavaServer Pages(Volume.1 .2nd.Edition)
资源详情
资源评论
资源推荐
© Prentice Hall and Sun Microsystems Press. Personal use only.
Training courses from the book’s author:
http://courses.coreservlets.com/
• Personally developed and taught by Marty Hall
• Available onsite at your organization (any country)
• Topics and pace can be customized for your developers
• Also available periodically at public venues
• Topics include Java programming, beginning/intermediate servlets
and JSP, advanced servlets and JSP, Struts, JSF/MyFaces, Ajax,
GWT, Ruby/Rails and more. Ask for custom courses!
AN OVERVIEW OF
S
ERVLET AND JSP
T
ECHNOLOGY
Topics in This Chapter
• Understanding the role of servlets
• Building Web pages dynamically
• Looking at servlet code
• Evaluating servlets vs. other technologies
• Understanding the role of JSP
Made by Charles Zhu, contact me by visiting http://xifangyuhui.javaeye.com
3
© Prentice Hall and Sun Microsystems Press. Personal use only.
Training courses from the book’s author:
http://courses.coreservlets.com/
• Personally developed and taught by Marty Hall
• Available onsite at your organization (any country)
• Topics and pace can be customized for your developers
• Also available periodically at public venues
• Topics include Java programming, beginning/intermediate servlets
and JSP, advanced servlets and JSP, Struts, JSF/MyFaces, Ajax,
GWT, Ruby/Rails and more. Ask for custom courses!
1
Servlet and JSP technology has become the technology of choice for developing
online stores, interactive Web applications, and other dynamic Web sites. Why? This
chapter gives a high-level overview of the technology and some of the reasons for its
popularity. Later chapters provide specific details on programming techniques.
1.1 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 data-
bases or applications on the HTTP server. Their job is to perform the following tasks,
as illustrated in Figure 1–1.
Figure 1–1 The role of Web middleware.
Legacy Application
Java Application
Web Service
...
Database
Web Server
Client (End User)
JDBC
JNI
RMI
SOAP
...
(Servlets/JSP)
Chapter 1 ■ An Overview of Servlet and JSP Technology
4
© Prentice Hall and Sun Microsystems Press. Personal use only.
J2EE training from the author: http://courses.coreservlets.com/
1. 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.
2. 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.
3. 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 applica-
tion, and embed the results inside a document.
4. Send the explicit data (i.e., the document) to the client.
This document can be sent in a variety of formats, including text
(HTML or XML), binary (GIF images), or even a compressed format
like gzip that is layered on top of some other underlying format. But,
HTML is by far the most common format, so an important servlet/JSP
task is to wrap the results inside of HTML.
5. Send the implicit HTTP response data.
Figure 1–1 shows a single arrow going from the Web middle layer (the
servlet or JSP page) to the client. But, there are really two varieties of
data sent: the document itself and the behind-the-scenes HTTP infor-
mation. Again, both varieties are critical to effective development.
Sending HTTP response data involves telling the browser or other
client what type of document is being returned (e.g., HTML), setting
cookies and caching parameters, and other such tasks. These tasks are
discussed in Chapters 6 and 7.
1.2 Why Build Web Pages Dynamically?
5
© Prentice Hall and Sun Microsystems Press. Personal use only.
J2EE training from the author: http://courses.coreservlets.com/
1.2 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 pro-
gramming, and HTTP, only to see their eyes immediately glaze over. Finally, in exas-
peration, 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.”
“Huh? Why wait until the client requests the page and then have a program build
the result? Why not just build the Web page ahead of time?”
Yes, many client requests can be satisfied by prebuilt documents, and the server
would handle these requests without invoking servlets. In many cases, however, a
static result is not sufficient, and a page needs to be generated for each request.
There are a number of reasons why Web pages need to be built on-the-fly:
• The Web page is based on data sent by the client.
For instance, the results page from search engines and order-
confirmation pages at online stores are specific to particular user
requests. You don’t know what to display until you read the data that
the user submits. Just remember that the user submits two kinds of
data: explicit (i.e., HTML form data) and implicit (i.e., HTTP request
headers). Either kind of input can be used to build the output page. In
particular, it is quite common to build a user-specific page based on a
cookie value.
• The Web page is derived from data that changes frequently.
If the page changes for every request, then you certainly need to build
the response at request time. If it changes only periodically, however,
you could do it two ways: you could periodically build a new Web page
on the server (independently of client requests), or you could wait and
only build the page when the user requests it. The right approach
depends on the situation, but sometimes it is more convenient to do
the latter: wait for the user request. For example, a weather report or
news headlines site might build the pages dynamically, perhaps
returning a previously built page if that page is still up to date.
• The Web page uses information from corporate databases or
other server-side sources.
If the information is in a database, you need server-side processing
even if the client is using dynamic Web content such as an applet.
Imagine using an applet by itself for a search engine site:
Chapter 1 ■ An Overview of Servlet and JSP Technology
6
© Prentice Hall and Sun Microsystems Press. Personal use only.
J2EE training from the author: http://courses.coreservlets.com/
“Downloading 50 terabyte applet, please wait!” Obviously, that is silly;
you need to talk to the database. Going from the client to the Web tier
to the database (a three-tier approach) instead of from an applet
directly to a database (a two-tier approach) provides increased
flexibility and security with little or no performance penalty. After all,
the database call is usually the rate-limiting step, so going through the
Web server does not slow things down. In fact, a three-tier approach is
often faster because the middle tier can perform caching and
connection pooling.
In principle, servlets are not restricted to Web or application servers that handle
HTTP requests but can be used for other types of servers as well. For example, serv-
lets could be embedded in FTP or mail servers to extend their functionality. And, a
servlet API for SIP (Session Initiation Protocol) servers was recently standardized
(see
http://jcp.org/en/jsr/detail?id=116). In practice, however, this use of servlets
has not caught on, and we’ll only be discussing HTTP servlets.
1.3 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.
Figure 1–2 shows the result.
The code is explained in detail in Chapter 3 (Servlet Basics), but for now, just
notice four points:
• It is regular Java code. There are new APIs, but no new syntax.
• It has unfamiliar import statements. The servlet and JSP APIs are
not part of the Java 2 Platform, Standard Edition (J2SE); they are a
separate specification (and are also part of the Java 2 Platform,
Enterprise Edition—J2EE).
• It extends a standard class (
HttpServlet). Servlets provide a rich
infrastructure for dealing with HTTP.
• It overrides the
doGet method. Servlets have different methods to
respond to different types of HTTP commands.
剩余673页未读,继续阅读
xifangyuhui
- 粉丝: 3
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- stc12c5a60s2 例程
- Android通过全局变量传递数据
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论6