"The time has come," the Walrus said, "To talk of many things: Of shoes -- and ships -- and sealing-wax -- Of cabbages -- and kings -- And why the sea is boiling hot -- And whether pigs have wings."
The framework documentation is written for active web developers and assumes a working knowledge about how Java web applications are built. Before getting started, you should understand the basics of several key technologies:
java.sun.com,
including a link to download the
JavaBeans 1.01 Specification
[java.util.Map].
A Map is a simple collection of name and value pairs.
Maps are often used "behind the scenes" as a flexible way
to store dynamic data.
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.
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.
It's the container's job to manages the servlet lifecycle. The container creates the servlets, invokes the servlets, and ultimately disposes the servlets.
A servlet is generally a subclass of
javax.servlet.http.HttpServlet.
A servlet must implement four methods, which are invoked
by the container as needed:
GET
protocol,
which generates a corresponding dynamic response.
POST
protocol,
which generates a corresponding dynamic response.
[org.apache.struts.action.ActionServlet].
As a developer, you can then just write objects
that the ActionServlet calls when needed.
But it is still helpful to understand the
servlet essentials,
and the role they play in a Java web application.
For more about Java Servlets, see:
java.sun.com
java.sun.com
doGet()
and
doPost()
methods must be programmed in a
thread-safe
manner.
For more about servlets and thread-safety, see:
[javax.servlet.ServletContext]
defines a servlet's view of
the web application within which the servlet is running.
It is
accessible in a servlet via the
getServletConfig()
method,
and in a JSP page as the
application
implicit variable.
Servlet contexts provide several APIs that are very useful
in building web applications:
getResource()
and
getResourceAsStream()
methods.
getAttribute(),
getAttributeNames(),
removeAttribute(),
and
setAttribute()
methods. From a JSP page, servlet
context attributes are also known as "application
scope beans".
HttpServletRequest
[javax.servlet.http.HttpServletRequest].
The request interface provides an object-oriented
mechanism to access
all of the information that was included in the underlying
HTTP request,
including:
getCookies()
method.
getRequestURI()
.
In addition, the constituent parts into which the
servlet container
parses the request URI (contextPath, servletPath, and
pathInfo) are
available separately.
Principal
object representing the current user, and
whether the current user is authorized for a specified
role.
[javax.servlet.http.HttpServletRequest]
and convert it into a
corresponding response. This is performed by calling
appropriate
methods on the servlet response
[javax.servlet.http.HttpServletResponse]
interface. Available methods let you:
Content-Type
header, which tells your client what
kind of information is included in the body of this
response.
This is typically set to
text/html
for an HTML page,
or
text/xml
for an XML document.
sendError()
.
sendRedirect()
method to redirect the client to
some other URL that you specify.
[javax.servlet.Filter]
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).
Struts 1.2 and earlier require your container to implement
version 2.2 or later of the Servlet Specification,
so those versions of the Struts framework do not use
Filters internaly. Beginning with Struts 1.3, a container
that supports version 2.3 or later of the Servlet
Specification is required.
Struts 2 uses a filter as the base of the controller,
instead of a servlet.
For more about filters, see:
javax.servlet.http.HttpSession
interface.
The servlet container will use one of two techniques
(cookies or
URL rewriting) to ensure that the next request from the
same user will
include the
session id
for this session, so that state
information saved in the session can be associated with
multiple
requests. This state information is stored in
session
attributes
(in JSP, they are known as "session scope beans").
To avoid occupying resources indefinitely when a user fails to
complete
an interaction, sessions have a configurable
timeout interval.
If the time gap between two requests exceeds this
interval, the session
will be timed out, and all session attributes removed. You
define a
default session timeout in your web application deployment
descriptor,
and you can dynamically change it for a particular session
by calling
the
setMaxInactiveInterval()
method.
Unlike requests, you need to be concerned about thread
safety on
your session attributes (the methods these beans provide,
not the
getAttribute()
and
setAttribute()
methods
of the session itself). It is surprisingly easy for there
to be
multiple simultaneous requests from the same user, which
will therefore
access the same session.
Another important consideration is that session attributes
occupy
memory in your server
in between
requests. This can have
an impact on the number of simultaneous users that your
application can
support. If your application requirements include very
large numbers of
simultaneous users, you will likely want to minimize your
use of
session attributes, in an effort to control the overall
amount of memory
required to support your application.
For more about sessions, see:
javax.servlet.http.HttpSession
One detail that can be configured in the Web application deployment descriptor is container-managed security. Declarative security can be used to protect requests for URIs that match given patterns. Pragmatic security can be used to fine-tune security make authorization decisions based on the time of day, the parameters of a call, or the internal state of a Web component. It can also be used to restrict authentication based on information in a database.
For more about security, see:
print
statement, everything in a JavaServer Page is written to
the response,
except
what is
placed within special Java statements.
With
JavaServer Pages
you can start by writing the page in standard HTML and
then add the
dynamic features using statements in the Java language or
by using
JSP tags.
The framework distribution includes several JSP tags
that make it easy to access the framework's
features from a JavaServer Page.
For more about JavaServer Pages and Custom JSP Tag
Libraries see
java.sun.com
java.sun.com
For an open source implementation of JSF, visit our sibling project, Apache MyFaces.
For more about JSTL and JavaServer Faces see
While the framework can work with any approach to user authentication and authorization, version 1.1 and later offers direct support for the standard Java Authentication and Authorization Service (JAAS). You can now specify security roles on an action-by-action basis.
For more about JAAS, see the Sun Developer Network product page.
A popular extension for handling security in a Java web application, including a framework application, is SecurityFilter .
Web applications based on JavaServer Pages sometimes commingle database code, page design code, and control flow code. In practice, we find that unless these concerns are separated, larger applications become difficult to maintain.
One way to separate concerns in a software application is to use a Model-View-Controller (MVC) architecture. The Model represents the business or database code, the View represents the page design code, and the Controller represents the navigational code.
The term "MVC" originated with the SmallTalk Model-View-Controller framework. In Smalltalk MVC, the View updates itself from the Model, via the "Observer" pattern. The original MVC pattern is like a closed loop: The View talks to the Controller, which talks to the Model, which talks to the View.
But, a direct link between the Model and the View is not practical for web applications, and we modify the classic MVC arrangement so that it would look less like a loop and more like a horseshoe with the controller in the middle.
In the MVC/Model 2 design pattern, application flow is mediated by a central Controller. The Controller delegates requests - in our case, HTTP requests - to an appropriate handler. The handlers are tied to a Model, and each handler acts as an adapter between the request and the Model. The Model represents, or encapsulates, an application's business logic or state. Control is usually then forwarded back through the Controller to the appropriate View. The forwarding can be determined by consulting a set of mappings, usually loaded from a database or configuration file. This provides a loose coupling between the View and Model, which can make applications significantly easier to create and maintain.
While MVC is a convenient paradigm, many workers find that applcations may utilize more than three layers. For example, within the Model, there is often distinct business logic and data access layers.
The framework provides the control layer for a Model 2 web applications. Developers can use this layer with other standard technologies to build the business, data access, and presentation layers.
For more about MVC, see