January 23, 2007 on 8:25 pm | In Java |
JavaSercer Pages The servlet class, defined by the element, is given a name through the element. A number of elements, with nested
and
elements, are used to define the following initialization parameters: jdbcDriverClassName, jdbcURL, dbUserName, and dbUserPassword. If you use a JDBC 2.0 connection pool, you need to define the URL used to get a reference from JNDI to it instead of all these parameters. The last servlet element, , tells the container that this servlet should be initialized when the web application is started. The container initializes servlets in the relative order specified by this element, from the lowest number to the highest. If two servlets have the same value, their relative start order is undefined. The servlet reads all the initialization parameters in its init( ) method, creates a DataSourceWrapper instance, and sets it as a ServletContext attribute named exampleDS: public void init( ) throws ServletException { ServletConfig config = getServletConfig( ); String jdbcDriverClassName = config.getInitParameter(”jdbcDriverClassName”); String jdbcURL = config.getInitParameter(”jdbcURL”); String dbUserName = config.getInitParameter(”dbUserName”); String dbUserPassword = config.getInitParameter(”dbUserPassword”); // Make sure a driver class and JDBC URL is specified if (jdbcDriverClassName == null || jdbcURL == null) { throw new UnavailableException(”Init params missing”); } DataSource ds = null; try { ds = new DataSourceWrapper(jdbcDriverClassName, jdbcURL, dbUserName, dbUserPassword); } catch (Exception e) { throw new UnavailableException(”Cannot create connection pool” + “: ” + e.getMessage( )); } getServletContext( ).setAttribute(”exampleDS”, ds); } All servlets and JSP pages in the application can now obtain a reference to the DataSource. Servlets use the ServletContext getAttribute( ) method to accomplish this. For JSP pages, the DataSource appears as an application scope object. All the database custom actions introduced in Chapter 9 look for a DataSource in the application scope, so all you have to do to use the one created by the initialization servlet is to provide the name: <%@ page language="java" contentType="text/html" %> <%@ taglib uri="/orataglib" prefix="ora" %> SELECT * FROM Employee WHERE FirstName LIKE ? AND LastName LIKE ? AND Dept LIKE ? ORDER BY LastName Note how the dataSource attribute value matches the name of the ServletContext attribute holding the reference to the DataSource, set by the initialization servlet. It’s much better to let an initialization servlet create the DataSource, as described here, than to use the custom action described in Chapter 9. With a servlet, all information about the JDBC driver class, URL, user and password is in one place (the WEB-INF/web.xml file), as opposed to being repeated in every JSP page that uses the database custom actions. This makes it easier to change the information when needed. Also, if you decide at some point to use another connection pool implementation, such as a true JDBC 2.0 connection pool available from your JDBC driver or database vendor, you can easily change the servlet’s init( ) method. So even for a pure JSP application, I recommend that you use an application initialization servlet like the one described here. page 241
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check professional tomcat hosting services
Sorry, the comment form is closed at this time.