JSP Tag Libraries

Скачать в pdf «JSP Tag Libraries»
Adding a JDBC resource reference for use with our new tag


Once the connection tag is available, we need only add a JDBC resource reference in the web-application deployment descriptor, and we can execute our JSP files using the J2EE managed database connection. Adding a resource reference to the deployment descriptor may seem tricky the first time, so let’s look at listing 12.8 wherein we see a stripped down web-application deployment descriptor used for testing J2EEDbConnectionTag.


Listing 12.8 A working web deployment descriptor with JDBC resource reference


<?xml version=»1.0″ encoding=»ISO-8859-1″?>


<!DOCTYPE web-app


PUBLIC «-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN» «http://java.sun.com/j2ee/dtds/web-app_2.2.dtd«>


<web-app>


<taglib>


<taglib-uri>


http://www.manning.com/j sptagsbook/j2eedatabase-taglib </taglib-uri>


<taglib-location>


/WEB-INF/j2eedatabase.tld </taglib-location>


</taglib> <resource-ref>


<description>A sample database connection for


J2EEDbConnectionTag </description> <res-ref-name>jdbc/BookDataSource</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth>


</resource-ref>


</web-app>


О Declares a JDBC connection resource The <resource-ref> entry indicates that a JDBC connection named java:comp/env/jdbc/BookDataSource should be offered by the container and that the authentication to the database is to be based on the container’s configuration parameters. Note that the java:comp/env/ part of the name is omitted from the <res-ref-name> value for all J2EE environment entries.


If you wonder where the promised linking between the resource reference and the actual database connection configuration is, the answer is that this part is server specific. You will not find it in the deployment descriptor, and will have to use the application server configuration tools provided by your particular application server vendor to link the application’s URL (java:comp/env/jdbc/BookDataSource) with the actual connection configuration.


NOTE Developers are often confused about JNDI entries and their scopes. This is usually due to the environment entries starting with java:comp/env/. All such entries are actually JNDI URLs, so, can one URL (say java:comp/ env/foo) serve a variety of web applications? Can these same URLs point to different values? Couldn’t these URLs become mixed up? The answer is that the environment space is private to the application, such that you may have different web applications come from different vendors that use the same environment entries, and the application server will know to differentiate between them. Thus, each application will always receive whatever was specified for it.

Скачать в pdf «JSP Tag Libraries»