Servlet Advanced Features


Servlets have other features that greatly improve their maintainability in the J2EE environment.

Initialization Parameters for Servlets

One way you could expand your servlet would be to add a connection to a database to retrieve, for example, the user 's account balance when he enters his username in the HTML form. The servlet reading from the database would need information such as the JDBC driver name being used along with the URL of the database.

There are many ways you could conceive of to pass initialization parameters to the servlet. You could have a file living in a particular directory or some other method. However, some of the best things that J2EE provides are simplicity and consistency across different vendor's application server implementation.

The J2EE method of passing initialization parameters to a servlet is very simple. In the web.xml file that you created you add an XML stanza that looks like this:

 
 <init-param> <param-name>dbClass</param-name> <param-value>org.postgresql.Driver</param-value> </init-param> 

This addition enables you to get, in this case, the class name of the database driver that you will use to connect to the database. Your code to access this initialization parameter would look like this:

 
 public void init( ServletConfig config ) throws ServletException { super.init( config); String dbClass = getInitParameter( "dbClass" ); } 

The init() method of a servlet enables the developer to do any sort of initialization that is required before any requests are processed . As shown, this can include reading configuration parameters, but can also include, for example, getting a connection to a database to be used during processing.

Database Access Using WebLogic Server 7.0 Services

As shown earlier in this chapter, you could use the init() method on a servlet to get the parameters needed to connect to the database. You would have to have parameters for the class name as we have shown, the database URL, and probably a user name and password to access the database. With this information you could modify your servlet as follows :

 
 import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.io.*; public class MyFirstServlet extends HttpServlet { private Connection connection = null; . . . 

We have added an import statement to get the JDBC- related classes and we have added a private member variable of type Connection.

Now you could modify the init() method to include the following (exception and error handling has been removed to simplify the view):

 
 String dbURL = getInitParameter( "dbURL" ); String dbUserName = getInitParameter( "dbUserName" ); String dbUserPassword = getInitParameter( "dbUserPassword" ); Class.forName( dbClass ).newInstance(); connection = DriverManager.getConnection( dbURL, dbUserName, dbUserPassword ); 

Now anywhere in your servlet that you needed access to a database you could use the member variable "connection". However, there are several problems with this approach. The biggest problem is that not all JDBC connections are thread safe. Remember that, by default, servlets are multi-threaded and that many threads could be executing your service methods simultaneously . This could lead to disastrous results in the database or at least a greatly enhanced possibility that our database would become corrupt.

What if we modified the code so that the connection was not a member variable but instead we created a new one each time we needed it? This would solve our thread problem. Each thread would get its own database connection so there would be no concurrency issues. However, creating a database connection is a fairly expensive operation. Because most Web transactions are short-lived, we would be creating and destroying connections at a fairly high rate. Additionally, what if 1,000 clients requested access to our servlet at the same time? We would quickly overload the database with client connections.

WebLogic 7.0 has two ways of improving this situation drastically: database connection pools and the J2EE DataSource.

Using WebLogic 7.0 Connection Pools

In WebLogic 7.0, a developer can use the JDBC connection pool provided by WebLogic. This pool contains identical connections to a database for use within WebLogic Server. Developers that need a database connection access it from the pool, use it as a normal JDBC Connection, and return it when they are done. This prevents the constant opening and closing of database connections, increasing the speed and scalability of the application.

To use the pool, follow these steps:

  1. Load the pool driver. The pool driver works in the same way that a normal JDBC driver works. The driver name is weblogic.jdbc.pool.Driver . So, where our earlier code loaded the driver the only difference is the name:

       
      Class.forName("weblogic.jdbc.pool.Driver").newInstance();  
  2. Create a connection using the URL for the driver. The URL of the pool driver is jdbc:weblogic:pool . Use it as we did above. A difference is that WebLogic JDBC pools are named so you will need to append the name of your pool onto the previous URL:

       
      connection = DriverManager.getConnection( "jdbc:weblogic:pool:myServletPool", dbUserName, dbUserPassword );  
  3. When you are done with the connection, close it.

Make sure that you call the close() method on the Connection object when you finish with your JDBC calls, so that the connection is returned to the pool:

 
 connection.close(); 

It is important to note that the WebLogic 7.0 connection pool is not a J2EE standard. You should not use it where portability to other application servers is a concern.

Using a DataSource for Database Connections

A DataSource is a J2EE server-side class that refers to a connection pool. A DataSource is slightly harder to configure and access than a WebLogic pool, but it is part of the J2EE standard and should be used when J2EE portability is important.

Configuring a DataSource is beyond the scope of this chapter. However, to use the DataSource you have to do the following:

  1. Look up the DataSource in the JNDI tree:

       
      Context ctx = new InitialContext(ht); // Look up the DataSource object javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup ("myServletDataSource");  
  2. Get a JDBC connection from the DataSource:

       
      java.sql.Connection connection = ds.getConnection();  
  3. Use the connection as normal.



BEA WebLogic Platform 7
BEA WebLogic Platform 7
ISBN: 0789727129
EAN: 2147483647
Year: 2003
Pages: 360

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net