📄 preface.html
字号:
The virtual properties of a DynaBean can't be called by standard Java methods,
but work well with components that rely on reflection and introspection.
</p>
<p>
In a Struts application, you can use DynaBeans to describe your HTML forms.
This strategy can avoid creating a formal JavaBean subclass to store a few simple properties.
</p>
<p>
For more about DynaBeans, see
</p>
<ul>
<li>
The Commons BeanUtils
<a href="http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/package-summary.html#package_description">
Package Description</a> and <a href="http://jakarta.apache.org/commons/beanutils/api/index.html">Javadocs</a>.
</li>
</ul>
</div>
<h2 id="resources">0.6 Properties Files and ResourceBundles</h2>
<div class="indent">
<p>Java applications, including web applications, are often configured using
<a href="http://java.sun.com/j2se/1.4.1/docs/api/java/util/Properties.html">Properties</a>
files. Properties files are the basis for the
<a href="http://java.sun.com/j2se/1.4.1/docs/api/java/util/ResourceBundle.html">ResourceBundles</a> that Struts uses to provide message resources
to an application. </p>
<p>For more about Properties files, see: </p>
<ul>
<li>
<a href="http://java.sun.com/docs/books/tutorial/essential/attributes/properties.html">
<strong>Using Properties to Manage Program Attributes</strong>
</a> in The Java Tutorial</li>
</ul>
<p>Java ResourceBundles use one or more Properties files to provide internationalized messages
to users based their
<a href="http://java.sun.com/j2se/1.4.1/docs/api/java/util/Locale.html">Locale</a>.
Support for localizing an application was built into Struts from the ground-up.</p>
<p>For more about localization and ResourceBundles, see </p>
<ul>
<li>
<a href="http://java.sun.com/docs/books/tutorial/i18n/resbundle/concept.html">
<strong>About the ResourceBundle Class</strong>
</a>
in the Java Tutorial</li>
</ul>
</div>
<h2 id="servlets">0.7 Java Servlets</h2>
<div class="indent">
<p>
Since Java is an object-orientated language, the
<a href="http://java.sun.com/products/servlet/">Java Servlet</a>
platform strives to cast HTTP into an object-orientated form.
This strategy makes it easier for Java developers to concentrate on what they need their application to do --
rather than the mechanics of HTTP.
</p>
<p>
HTTP provides a standard mechanism for extending servers called the Common Gateway Interface, or CGI.
The server can pass a request to a CGI-aware program, and the program will pass back a response.
Likewise, a Java-aware server can pass a request to a servlet container.
The container can fulfill the request or it can pass the request back to the HTTP server.
The container decides whether it can handle the request by checking its list of servlets.
If there is a servlet registered for the request, the container passes the request to the servlet.
</p>
<p>
When a request comes in, the container checks to see if there is a servlet registered for that request.
If there is a match,
the request is given to the servlet.
If not, the request is returned to the HTTP server.
</p>
<p>
It's the container's job to manages the servlet lifecycle.
The container creates the servlets, invokes the servlets, and ultimately disposes the servlets.
</p>
<p>
A servlet is generally a subclass of <code>javax.servlet.http.HttpServlet</code>.
A servlet must implement four methods, which are invoked by the container as needed:
</p>
<ul>
<li>
<strong>public void init(ServletConfig config)</strong> - Called by the
servlet container when the servlet instance is first created, and
before any request is processed.</li>
<li>
<strong>public void doGet(HttpServletRequest request,
HttpServletResponse response)</strong> - Called to process a
specific request received using the HTTP <code>GET</code> protocol,
which generates a corresponding dynamic response.</li>
<li>
<strong>public void doPost(HttpServletRequest request,
HttpServletResponse response)</strong> - Called to process a
specific request received using the HTTP <code>POST</code> protocol,
which generates a corresponding dynamic response.</li>
<li>
<strong>public void destroy()</strong> - Called by the servlet
container when it takes this servlet instance out of service,
such as when a web application is being undeployed or when the
entire container is being shut down.</li>
</ul>
<p>
Struts provides a ready-to-use servlet for your application [<code>org.apache.struts.action.ActionServlet</code>].
As a Struts developer, you can then just write objects that the Struts ActionServlet calls when needed.
But it is still helpful to understand the basics of what servlets are,
and the role they play in a Java web application.
</p>
<p>
For more about Java Servlets, see:
</p>
<ul>
<li>
<a href="http://java.sun.com/products/servlet/">
<strong>The
Java Servlet Technology</strong>
</a> page at
<code>java.sun.com</code>
</li>
<li>
<a href="http://java.sun.com/products/servlet/download.html">
<strong>The
Servlet 2.2 and 2.3 Specifications</strong>
</a> download page at
<code>java.sun.com</code>
</li>
<li>
<a href="http://java.sun.com/webservices/docs/1.0/tutorial/doc/Servlets.html">
<strong>Java Servlet Technology</strong>
</a> in the Java Web Services Tutorial.</li>
<li>
<a href="http://java.sun.com/webservices/docs/1.0/tutorial/doc/WebApp.html">
<strong>Web Applications</strong>
</a> in the Java Web Services Tutorial.</li>
</ul>
</div>
<h2 id="threads">0.7.1 Servlets and threads</h2>
<div class="indent">
<p>
To boost performance, the container can multi-thread servlets.
Only one instance of a particular servlet is created,
and each request for that servlet passes through the same object.
This strategy helps the container make the best use of available resources.
The tradeoff is that the servlet's <code>doGet()</code> and <code>doPost()</code> methods must be programmed in a <em>thread-safe</em> manner.
</p>
<p>
For more about servlets and thread-safety, see:
</p>
<ul>
<li>
<a href="http://java.sun.com/webservices/docs/1.0/tutorial/doc/Servlets5.html#64386">Controlling Concurrent Access to Shared Resources</a> in Java Web Services Tutorial.
</li>
</ul>
</div>
<h2 id="context">0.7.2 Servlet Context</h2>
<div class="indent">
<p>The <em>ServletContext</em> interface [<code>javax.servlet.ServletContext</code>] defines a servlet's view of
the web application within which the servlet is running. It is
accessible in a servlet via the <code>getServletConfig()</code> method,
and in a JSP page as the <code>application</code> implicit variable.
Servlet contexts provide several APIs that are very useful in building
Struts based web applications:</p>
<ul>
<li>
<em>Access To Web Application Resources</em> - A servlet can
access static resource files within the web application using the
<code>getResource()</code> and <code>getResourceAsStream()</code>
methods.</li>
<li>
<em>Servlet Context Attributes</em> - The context makes available
a storage place for Java objects, identified by string-valued keys.
These attributes are global to the entire web application, and may
be accessed by a servlet using the <code>getAttribute()</code>,
<code>getAttributeNames()</code>, <code>removeAttribute()</code>, and
<code>setAttribute()</code> methods. From a JSP page, servlet
context attributes are also known as "application scope beans".
</li>
</ul>
<p>
For more about the servlet context, see:
</p>
<ul>
<li>
<a href="http://java.sun.com/webservices/docs/1.0/tutorial/doc/Servlets10.html#64724">Accessing the Web Context</a> in Java Web Services Tutorial.
</li>
</ul>
</div>
<h2 id="request">0.7.3 Servlet Request</h2>
<div class="indent">
<p>Each request processed by a servlet is represented by a Java
interface, normally a <code>HttpServletRequest</code>
[<code>javax.servlet.http.HttpServletRequest</code>].
The request interface provides an object-oriented mechanism to access
all of the information that was included in the underlying HTTP request,
including:</p>
<ul>
<li>
<em>Cookies</em> - The set of cookies included with this request
are available via the <code>getCookies()</code> method.</li>
<li>
<em>Headers</em> - HTTP headers that were included with
the request are accessible by name. You can enumerate the names
of all included headers.</li>
<li>
<em>Parameters</em> - Request parameters, including those from
the query string portion of the URL and from the embedded content
of the request (POST only) are available by name.</li>
<li>
<em>Request Characteristics</em> - Many other characteristics
of the incoming HTTP request, such as the method used (normally
GET or POST) the protocol scheme used ("http" or "https"), and
similar values.</li>
<li>
<em>Request URI Information</em> - The original request URI
being processed is available via <code>getRequestURI()</code>.
In addition, the constituent parts into which the servlet container
parses the request URI (contextPath, servletPath, and pathInfo) are
available separately.</li>
<li>
<em>User Information</em> - If you are using
<a href="#security">Container Managed Security</a>, you can ask for
the username of the authenticated user, retrieve a
<code>Principal</code> object representing the current user, and
whether the current user is authorized for a specified role.</li>
</ul>
<p>In addition, servlet requests support <em>request attributes</em>
(from JSP, these are "request scope beans"), analogous to the servlet
context attributes described above. Request attributes are often used
to communicate state information from a business logic class that
generates it to a view component (such as a JSP page) that will use
the information to produce the corresponding response.</p>
<p>The servlet container guarantees that a particular request will
be processed by a servlet on a single thread. Therefore, you do not
generally have to worry about the thread safety of your access to
request properties and attributes.</p>
<p>
For more about the servlet request, see:
</p>
<ul>
<li>
<a href="http://java.sun.com/webservices/docs/1.0/tutorial/doc/Servlets7.html#64433">Getting Information from Requests</a> in Java Web Services Tutorial.
</li>
</ul>
</div>
<h2 id="response">0.7.4 Servlet Response</h2>
<div class="indent">
<p>The primary purpose of a servlet is to process an incoming
<a href="#request">Servlet Request</a> [<code>javax.servlet.http.HttpServletRequest</code>]
and convert it into a
corresponding response. This is performed by calling appropriate
methods on the servlet response [<code>javax.servlet.http.HttpServletResponse</code>]
interface. Available methods let you:</p>
<ul>
<li>
<em>Set Headers</em> - You can set HTTP headers that will be
included in the response. The most important header is the
<code>Content-Type</code> header, which tells your client what
kind of information is included in the body of this response.
This is typically set to <code>text/html</code> for an HTML page,
or <code>text/xml</code> for an XML document.</li>
<li>
<em>Set Cookies</em> - You can add cookies to the current
response.</li>
<li>
<em>Send Error Responses</em> - You can send an HTTP error status
(instead of a usual page of content) using
<code>sendError()</code>.</li>
<li>
<em>Redirect To Another Resource</em> - You can use the
<code>sendRedirect()</code> method to redirect the client to
some other URL that you specify.</li>
</ul>
<p>An important principle in using the servlet response APIs is that
any methods you call to manipulate headers or cookies
<strong>MUST</strong> be performed before the first buffer-full of
content has been flushed to the client. The reason for this restriction
is that such information is transmitted at the beginning of the HTTP
response, so trying things like adding a header after the headers have
already been sent will not be effective.</p>
<p>
When you are using presentation pages in a Model 2 application,
you will not generally use the servlet response APIs directly.
In the case of JavaServerPages, the JSP page compiler
in your servlet container will convert your page into a servlet.
The JSP servlet renders the response, interspersing dynamic
information where you have interposed JSP custom tags.
</p>
<p>
Other presentation systems, like Velocity Tools for Struts,
may delegate rendering the response to a specialized servlet,
but the same pattern holds true.
You create a template,
and the dynamic response is generated automatically from the template.
</p>
<p>
For more about the servlet response, see:
</p>
<ul>
<li>
<a href="http://java.sun.com/webservices/docs/1.0/tutorial/doc/Servlets7.html#64531">Constructing Responses</a> in Java Web Services Tutorial.
</li>
</ul>
</div>
<h2 id="filter">0.7.5 Filtering</h2>
<div class="indent">
<p>If you are using a servlet container based on version
<strong>2.3</strong> or later of the Servlet Specification (such as
Tomcat 4.x), you can take advantage of the new Filter APIs
[<code>javax.servlet.Filter</code>] that
let you compose a set of components that will process a request or
response. Filters are aggregated into a chain in which each filter
has a chance to process the request and response before and after
it is processed by subsequent filters (and the servlet that is ultimately
called).</p>
<p>
The Struts 1.x series (versions 1.0, 1.1, and so forth) require only version 2.2 or later of the Servlet
Specification to be implemented by your servlet container, so Struts does not itself utilize Filters
at this time.
The next generation of Struts (the 2.x series) will be based on Servlet 2.3 or later.
It is likely that the Struts 2.x release series will utilize filters.
</p>
<p>
For more about filters, see:
</p>
<ul>
<li>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -