07-07-2012, 12:50 PM
Building Java HTTP servlets
Building Java HTTP servlets.pdf (Size: 269.19 KB / Downloads: 96)
Tutorial tips
What are the prerequisites?
This tutorial teaches basic concepts about servlets: what, why, and how. You should be
familiar with Java programming concepts such as the structure of the language
(packages, classes, and methods) and how object-oriented programming differs from
procedural programming. You don't need prior experience writing servlets.
To write the servlets described in this tutorial, you simply need an editing environment.
This can be as basic as an operating system editor. In a development environment,
many people use IDEs because they have debuggers and other features specific to
writing and testing code.
To compile the servlets, you'll need the Java compiler (javac.exe). You'll also need
the Java server development kit (JSDK), which is typically an extension to the JDK.
Most servers support servlets based on the JSDK Version 2.1. You can test your
servlets with a utility included in the JSDK. The JSDK 2.0 has a utility called
servletrunner, while JSDK 2.1 comes with a small HTTP server.
Introduction to servlets
What does a servlet do?
A servlet is a server-side software program, written in Java code, that handles
messaging between a client and server. The Java Servlet API defines a standard
interface for the request and response messages so your servlets can be portable
across platforms and across different Web application servers.
Servlets can respond to client requests by dynamically constructing a response that is
sent back to the client. For example, in this tutorial you'll write a servlet to respond to
an HTTP request.
Because servlets are written in the Java programming language, they have access to
the full set of Java APIs. This makes them ideal for implementing complex business
application logic and especially for accessing data elsewhere in the enterprise. The
Java Database Connectivity (JDBC) API, which allows Java programs to access
relational databases (and is beyond the scope of this tutorial), is one example.
Because there are no graphics associated with servlets, access to the GUI Java APIs
(the AWT) is not relevant.
How does a servlet work?
A servlet runs inside a Java-enabled application server. Application servers are a
special kind of Web server; they extend the capabilities of a Web server to handle
requests for servlets, enterprise beans, and Web applications. There is a distinct
difference between a Web server and an application server. While both can run in the
same machine, the Web server runs client code such as applets and the application
server runs the servlet code.
How do you run a servlet?
Servlets are either invoked as an explicit URL reference or are embedded in HTML and
invoked from a Web application. You don't execute a Java command to run a servlet
(as you would an applet); instead, you issue a URL command pointing to the location of
the servlet.
Servlets are located in any directory on a machine where an application server is
running. Typically, there is a servlet directory to which you will ftp or copy your class
files so the application server can find them. This location can be configured differently,
depending on the administrator and the environment.
Servlets are loaded when they are invoked for the first time from a client. Alternatively,
they can be loaded when the application server is started; this is a configuration
decision.