Java Reference
In-Depth Information
Unlike the examples in Part II , which use the "classic" JDBC Core API approach of getting a
Connection from the DriverManager , this servlet illustrates the preferred way of getting a
connection, which is to use a javax.sql.DataSource . You can, of course, use the DriverManager
instead.
Cross-
Reference
The javax.sql.DataSource is discussed in Chapter 4 and is used
throughout the examples in Part III of this topic.
Notice that, in addition to the doPost() method, the doGet() method has been minimally
implemented with a simple call to doPost() . The main reason for doing this is to make it easier to
check out the servlet from a browser by simply typing the entire GET string into the browser's address
window. Here's an example:
http://jdbc.j-
machines.com/servlet/LoginServlet?UserName=OneFish&Password=TwoFish
The ResultSet the SQL query returns is used to determine whether a MemberID has been assigned
to this UserName. If not, the servlet forwards the user to the new member sign up page. Similarly, if the
UserName is recognized but the password is bad, the user is forwarded to a page that lets the user
retry or request that the password be e-mailed. If the UserName and password are found in the
database, the user is forwarded to the welcome page.
Forwarding is handled using a javax.servlet . RequestDispatcher object. To get a
RequestDispatcher , use the ServletContext object's getRequestDispatcher() method,
passing it the desired URL as an argument. Notice the format of this argument: a slash ("/") followed by
the relative path, ending with the name of the resource. This is very important to remember, since, as
soon as you call the servlet, you move from the HTTP server's environment, which may be a virtual
host's root directory under Apache, to the servlet environment, which will probably be somewhere under
Tomcat's root directory.
You should use forwarding when the servlet's job is done and the next page is logically decoupled from
the servlet's functions to such an extent that another resource can handle it. Remember, however, that
if you have already written any output from the servlet, using a ServletOutputStream or a
PrintWriter , you can't use the RequestDispatcher.forward method; it will throw an
IllegalStateException . In this case, you can use the RequestDispatcher.include() method
instead.
Deployment
To deploy the login servlet, you have to put the class file into the appropriate directory. This is the usual
path for a simple Tomcat installation:
TOMCAT/WEBAPPS/ROOT/WEB-INF/CLASSES
Tomcat maintains a configuration file that defines URL mappings, so that the /servlet/ path is
mapped to this directory. You can set up your own mappings as needed by editing this file.
To use the Opta2000 driver, you need to put the Opta2000.jar into a suitable directory and modify
Tomcat's class path in the tomcat.properties file in the Tomcat/conf directory. In this example,
the .jar file is saved in the /lib directory, and Tomcat's class path is modified by adding this line in
the tomcat.properties file:
wrapper.classpath=lib/Opta2000.jar
To make the use of servlets even easier, Sun came up with the idea of Java Server Pages, or JSPs. A
brief introduction to JSPs is given in the next section .
Using Java Server Pages
Search WWH ::




Custom Search