Oreilly.Java.Server.Pages.3rd.Edition.chm 笔记

 1.What's the difference between cgi and servlet?
CGI is not an efficient solution. For every request, the web server
has to create a new operating-system process, load a Perl interpreter and
the Perl script, execute the script, and then dispose of it when it's
done.
This is very taxing for the server and doesn't scale well when the
amount of traffic increases
To provide a more efficient solution, various alternatives to CGI have
been added to programmers' toolboxes over the last few years
While these solutions offer better performance and scalability, each
one is supported only by a subset of the popular web servers.
The Java Servlet API, introduced in early 1997, provides a solution to
the portability issue.these technologies suffer from a common problem
HTML code embedded inside programs.you've probably seen endless calls
to out.println( ) that contain scores of HTML tags
For the individual developer working on a simple web site, this
approach may work fine, but it makes it difficult for people with different
skills to work together to develop a web application.

2.a JSP page can also contain Java code embedded in so-called scripting
elements
the JSP Expression Language (EL) and the JSP Standard Tag Library
(JSTL),Java code in a page is rarely needed
In addition, embedding too much code in a web page is no better than
using HTML elements in a server-side program, and often leads to a web
application that is hard to maintain and debug. The examples in this book
rarely use scripting elements, but they are described in detail in
Chapter 16.

3.JSP gets around this problem by compiling each JSP page into
executable code the first time it's requested (or on demand), and invoking the
resulting code directly on all subsequent requests. When coupled with a
persistent Java virtual machine on a JSP-enabled web server, this
allows the server to handle JSP pages much faster.

4.The JSP Advantage:
JSP combines the most important features found in the alternatives:
JSP supports both scripting- and element-based dynamic content, and
allows developers to create custom tag libraries to satisfy
application-specific needs.
JSP pages are compiled for efficient server processing.
JSP pages can be used in combination with servlets that handle the
business logic, the model favored by Java servlet template engines.
In addition, JSP has a couple of unique advantages that make it stand
out from the crowd:
JSP is a specification, not a product. This means vendors can compete
with different implementations, leading to better performance and
quality. It also leads to a less obvious advantage, namely that when so many
companies have invested time and money in the technology, chances are
it will be around for a long time, with reasonable assurances that new
versions will be backward compatible; with a proprietary technology,
this is not always a given.
JSP is an integral part of J2EE, a complete platform for enterprise
class applications. This means that JSP can play a part in the simplest
applications to the most complex and demanding.

get/post方法的区别
5.Request Parameters:
can be sent in one of two ways: tacked on to the URI in the form of a
query string or sent as part of the request message body. This is an
example of a URL with a query string:
http://www.weather.com/forecast?city=Hermosa+Beach&state=CA

A GET request always uses a query string to send parameter values,
while a POST request always sends them as part of the body (additionally,
it can send some parameters as a query string, just to make life
interesting).
6.Due to the differences in how parameters are sent by GET and POST
requests, as well as the differences in their intended purpose, browsers
handle the requests in different ways. A GET request, parameters and
all, can easily be saved as a bookmark, hardcoded as a link, and the
response cached by the browser. Also, the browser knows that no damage is
done if it needs to send a GET request again automatically, for instance
if the user clicks the Reload button.
A POST request, on the other hand, can't be bookmarked as easily; the
browser would have to save both the URI and the request message body.
Since a POST request is intended to perform some possibly irreversible
action on the server, the browser must also ask the user if it's okay to
send the request again

2.2.2servlet的生命周期
A servlet container is the connection between a web server and the
servlets. It provides the runtime environment for all the servlets on the
server as defined by the servlet specification, and is responsible for
loading and invoking those servlets when the time is right
6.The container typically loads a servlet class when it receives the
first request for the servlet, gives it a chance to initialize itself,
and then asks it to process the request. Subsequent requests use the
same, initialized servlet until the server is shut down. The container then
gives the servlet a chance to release resources and save its state (for
instance, information accumulated during its lifetime).

7.servelt的disadvantage
the pure servlet-based approach still has a few problems:
Thorough Java programming knowledge is needed to develop and maintain
all aspects of the application, since the processing code and the HTML
elements are lumped together.
Changing the look and feel of the application, or adding support for a
new type of client (such as a WML client), requires the servlet code to
be updated and recompiled.
It's hard to take advantage of web page development tools when
designing the application interface. If such tools are used to develop the web
page layout, the generated HTML must then be manually embedded into the
servlet code, a process which is time consuming, error prone, and
extremely boring.

3.2jsp中的术语:
8.1Everything in the page that isn't a JSP element is called template
text.
JSP has no dependency on HTML; it can be used with any markup language.
Template text is always passed straight through to the browser
When a JSP page request is processed, the template text and dynamic
content generated by the JSP elements are merged, and the result is sent
as the response to the browser.

3.3jsp处理
Converting the JSP page to a servlet and compiling the servlet form the
translation phase.
The JSP container is also responsible for invoking the JSP page
implementation class (the generated servlet) to process each request and
generate the response. This is called the request processing phase
比如从hello.jsp到HelloServlet.java是Translation
phase.从HelloServlet.java到HelloServlet.class是Request processing phase.
As long as the JSP page remains unchanged, any subsequent request goes
straight to the request processing phase (i.e., the container simply
executes the class file).
When the JSP page is modified, it goes through the translation phase
again before entering the request processing phase
these two containers—a servlet container and a JSP container—are
often combined in one package under the name web container.

3.3.1JSP Elements
There are three types of JSP elements you can use: directive, action,
and scripting. A new construct added in JSP 2.0 is an Expression
Language (EL) expression; let's call this a forth element type, even though
it's a bit different than the other three.
3.3.1.1 Directive elements:
<%@ page ... %>  Defines page-dependent attributes, such as session
tracking, error page, and buffering requirements
<%@ include ... %>   Includes a file during the translation phase
<%@ taglib ... %>   Declares a tag library, containing custom actions,
that is used in the page
3.3.1.2 Standard action elements
<jsp:useBean>  Makes a JavaBeans component available in a page
<jsp:getProperty> Gets a property value from a JavaBeans component and
adds it to the response
<jsp:setProperty> Sets a JavaBeans component property value
<jsp:include> Includes the response from a servlet or JSP page during
the request processing phase
<jsp:forward> Forwards the processing of a request to a servlet or JSP
page
<jsp:param> Adds a parameter value to a request handed off to another
servlet or JSP page using <jsp:include> or <jsp:forward>
<jsp:plugin> Generates HTML that contains the appropriate
browser-dependent elements (OBJECT or EMBED) needed to execute an applet with the
Java Plugin software
除了标准的之外.还有Custom action elements and the JSP Standard Tag
Library(jstl)
3.3.1.4 Scripting elements
<% ... %>  Scriptlet, used to embed scripting code
<%= ... %> Expression, used to embed scripting code expressions when
the result shall be added to the response; also used as request-time
action attribute values
<%! ... %> Declaration, used to declare instance variables and methods
in the JSP page implementation class
3.3.1.5 Expression Language expressions(EL)

6.2 Declaring a Bean in a JSP Page
<jsp:useBean id="cartoon"  class="com.ora.jsp.beans.motd.CartoonBean"
/>
    <img src="images/<jsp:getProperty name="cartoon"
property="fileName" />">

10.1.2Passing Data from One Page to Another(传递数据)
page, request, session(来自于同一个浏览器,单一用户), and
application(所有用户共享)

10.2.1
the information can be sent to the browser in one of three ways:
As a cookie
Embedded as hidden fields in an HTML form
Encoded in the URLs in the response body, typically as links to other
application pages (this is known as URL rewriting)
A session starts when the browser makes the first request for a JSP
page in a particular application. The application can explicitly end the
session (for instance when the user logs out or completes a
transaction), or the JSP container can end it after a period of user inactivity
(the default value is typically 30 minutes after the last request). Note
that there's no way for the server to tell if the user closes the
browser, because there's no permanent connection between the browser and the
server, and no message is sent to the server when the browser
disappears. Still, closing the browser usually means losing the session ID; the
cookie expires, or the encoded URLs are no longer available. So when
the user opens a browser again, the server can't associate the new
request with the previous session, and therefore creates a new session.
However, all session data associated with the previous session remains on
the server until the session times out.
服务器其实并不知道client关闭了浏览器,只是说关闭之后,丢失了sessionId;新开了一个浏览器,新的请求不能关联到原先的session.信息会一直
保存在服务器上.直到过期

10.3 Redirect Versus Forward(重定向和前转)
There's an important difference between a forward and a redirect. When
you forward, the target page is invoked through an internal method call
by the JSP container; the new page continues to process the same
request and the browser isn't aware that more than one page is involved. A
redirect, on the other hand, means that the first page tells the browser
to make a new request to the target page. The URL shown in the browser
is therefore changed to the URL of the new page when you redirect, but
stays unchanged when you use forward. A redirect is slower than a
forward, since the browser has to make a new request. Also, because it
results in a new request, request scope variables are no longer available
after a redirect.
So how do you decide if you should use forward or redirect? To a large
extent it's a matter of preference. I look at it like this: forwarding
is always faster, so that's the first choice. But because the URL in
the browser refers to the start page even after the forward, I ask myself
what happens if the user decides to reload the start page (or just
resize the window: this often reloads the page automatically). In this
example, the start page is the page that adds an item to the shopping cart.
I don't want it to be invoked again on a reload, so I redirect to the
page that displays the catalog and shopping cart content instead. No
harm is done if the user reloads this page

10.4Memory Usage Considerations 内存使用考虑
Here are some things you can do to keep the memory requirements under
control:
Place only objects that really need to be unique for each session in
the session scope. In the shopping cart example, each cart contains only
references to the product beans (not copies of the beans), and the
catalog bean and the product beans are shared by all users.
Set the timeout period for sessions to a lower value than the default.
If you know it's rare that your users leave the site for 30 minutes and
then return, use a shorter period. You can change the timeout for all
sessions in an application through the application's deployment
descriptor (see Appendix F), or by calling session.setMaxInactiveInterval( )
(see Appendix D) in a custom action, bean, or servlet to change it for an
individual session.
Provide a way to end the session explicitly. A good example is a logout
function, or invalidation of the session when something is completed
(for instance when an order form is submitted). In a JSP page, you can
use the <ora:invalidateSession> custom action described in Chapter 13 to
invalidate the session. In a servlet or other custom code, you can use
the HttpSession invalidate( ) method (see Appendix D). Invalidating a
session makes all objects available for garbage collection (the term
used for when the Java runtime removes unused objects to conserve memory).





疑问:
1.http的特性??在j2ee如何解决?
2.jsp中的mvc模式描述3.4中有英文描述
3.web程序代码结构:
WEB-INF directory with a web.xml file, a lib directory, and a classes
directory
The web.xml file contains configuration information for the example
application in the format defined by the servlet and JSP specifications.
It's too early to look at the contents of this file now; we will return
to parts of it when needed.
The lib and classes directories are standard directories, also defined
by the servlet specification. A very common question asked by people
new to servlets and JSP (prior to the standard web application format)
was, "Where do I store my class files so that the server can find them?"
The answer, unfortunately, differed depending on which implementation
was used. With the standard web application format, it's easy to answer
this question: if the classes are packaged in a JAR file, store the JAR
file in the lib directory; otherwise use the classes directory (with
subdirectories mirroring the classes' package structure). The server will
always look for Java class files in these two directories.
JSP pages in any directory except under WEB-INF
and Java class files in either the classes or the lib directory
depending on if the classes are packaged in a JAR file or not
4.taglib的使用
<%@ tablib prefix="ora"
uri="orataglib"%>容器会到web-inf下的目录和jar包中寻找所有tld结尾的
文件.使用uri名字来定位taglib下的uri.
doStartTag() .doEndTag()
5.session与cookies的区别
理论上当cookies关闭时候。session还有其他两种办法来做,重写和隐藏字段
If you want to provide session tracking for browsers that don't support
cookies, you must use the <c:url> action to rewrite all URL references
in your application
拿一个用cookies的代码说明.感觉应该是cookies和session
scope同时使用才有效果
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值