As soon as the web began to be used for delivering services, service providers recognized the need for dynamic content. Applets, one of the earliest attempts toward this goal, focused on using the client platform to deliver dynamic user experiences. At the same time, developers also investigated using the server platform for this purpose. Initially, Common Gateway Interface (CGI) scripts were the main technology used to generate dynamic content. Although widely used, CGI scripting technology has a number of shortcomings, including platform dependence and lack of scalability. To address these limitations, Java servlet technology was created as a portable way to provide dynamic, user-oriented content.
A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications access via a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.
The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines life-cycle methods. When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.
The Example Servlets
The Example Servlets
Each programming task is illustrated by one or more servlets. For example,
BookDetailsServlet
illustrates how to handle HTTP GET
requests, BookDetailsServlet
and
CatalogServlet
show how to construct responses, and
CatalogServlet
illustrates how to track session
information.
The data for the bookstore application is maintained in a
database and accessed through the database access class
database.BookDBAO
. The database
package also contains the class Book
which represents a
book. The shopping cart and shopping cart items are represented by the classes
cart.ShoppingCart
and cart.ShoppingCartItem
, respectively.
To deploy and run the application using NetBeans 5.5, follow
these steps:
- Perform all the operations described in Accessing Database from Web Applications.
- In NetBeans 5.5, select File Open Project Folder.
- In the Open Project dialog, navigate to:
- Select the
bookstore1
folder. - Select the Open as Main Project checkbox and the Open Required Projects checkbox.
- Click Open Project Folder.
- In the Projects tab, right-click the
bookstore1
project, and select Deploy Project. - To run the application, open the bookstore URL
http://localhost:8080/bookstore1/bookstore
.
<INSTALL>/javaeetutorial5/examples/web/
To deploy and run the application using Ant, follow these steps:
- In a terminal window, go to
<
INSTALL
>/javaeetutorial5/examples/web/bookstore1/
. - Run the command
ant
. This target will spawn any necessary compilations, copy files to the<
INSTALL
>/javaeetutorial5/examples/web/bookstore1/build/
directory, and create a WAR file and copy it to the<
INSTALL
>/javaeetutorial5/examples/web/bookstore1/dist/
directory. - Start the Application Server.
- Perform all the operations described in Creating a Data Source in the Application Serve.
- To deploy the example, run
ant deploy
. The deploy target outputs a URL for running the application. Ignore this URL, and instead use the one shown in the next step. - To run the application, open the bookstore URL
http://localhost:8080/bookstore1/bookstore
.
To learn how to configure the example, refer to the deployment
descriptor (the
web.xml
file), which includes the
following configurations: - A
display-name
element that specifies the name that tools use to identify the application. - A set of
filter
elements that identify servlet filters contained in the application. - A set of
filter-mapping
elements that identify which servlets will have their requests or responses filtered by the filters identified by thefilter
elements. Afilter-mapping
element can define more than one servlet mapping and more than one URL pattern for a particular filter. - A set of
servlet
elements that identify all the servlet instances of the application. - A set of
servlet-mapping
elements that map the servlets to URL patterns. More than one URL pattern can be defined for a particular servlet. - A set of error-page mappings that map exception types to an HTML page, so that the HTML page opens when an exception of that type is thrown by the application