4.3. Building your own DbForms-capable applications

Use the checklist below as a guide for setting up a DbForms application.

  1. Copy the file bin/dbforms.tld from the DbForms distribution into the WEB-INF directory of your web application.

    Note. The file dbforms.tld was named taglib.tld in some prior versions. The name taglib.tld was changed because it was too generic and the risk of naming conflicts too great.

  2. Copy the file bin/dbformsX.Y.jar (where X.Y is the release number) from the DbForms distribution into the WEB-INF/lib directory of your web application.
  3. Copy the jars in the dependend directory of the DbForms distribution into the WEB-INF/lib directory of your web application.
  4. Create or modify the J2EE deployment descriptor WEB-INF/web.xml file to include the following items. See Section 4.3.1, “Edit deployment descriptor web.xml for more details.
    • Three servlet elements to define the control, config, and file servlets and their URL mappings. Other servlets may also be needed for additional capabilities such as JasperReports, Excel output, and CSV output.
    • A tag library declaration for the DbForms tag library.
    • Security measures for your application.
  5. Create a file WEB-INF/dbforms-config.xml that defines the tables, the fields, and the database connection to use. See Chapter 8, Configuration Files for details of the contents of this file. This can be generated AUTOMATICALLY (along with JSP pages) by using the DevGui application. See Chapter 5, DevGui for more information.

    It is also possible to use more than one configuration file. Simply list them with commas between them in the dbformsConfig init param of the ConfigServlet in web.xml as shown in this fragment from the dbforms-config.xml file.

       <init-param>
         <param-name>dbformsConfig</param-name>
         <param-value>
           /WEB-INF/dbforms-config.xml,/WEB-INF/custom/dbforms-config.xml
         </param-value>
       </init-param>
               

  6. Optionally, create a WEB-INF/dbforms-errors.xml file that defines error messages. See the xmlErrors tag in the separate DbForms Custom Tag Library document in the docs/taglib directory for more information.
  7. In each JSP page that will use the DbForms custom tags, add a line at the top of the page like this:
       <%@ taglib uri="/WEB-INF/dbforms.tld" prefix="db" %>
            
    The prefix "db" followed by a colon is placed in front of each tag from the DbForms tag library. The above declaration gives the association between the tag library and the prefix. If you wish, you can use a prefix other that "db". This document, however, assumes a prefix of "db".
  8. Verify the structure of your deployment. See Section 4.3.2, “Typical deployment structure of applications using DbForms” for an example of a complete directory and file structure.
  9. Check that the database configuration defined in dbforms-config.xml is really working. Whether this is an easy task or not, depends heavily on the database server you are using. One way to do this is to use the database tab in DevGui (Section 5.4.2, “Database Tab”) to set up a simple, non-JNDI and non-connection pool connection. Common reasons for database-related problems are:
    • JDBC drivers not in classpath (of servlet container).
    • Connection pool classess not in classpath.
    • Driver/Connection-pool resources (property-files) not in classpath.
    • Misspelled connection URLs or JNDI keys.
    • Database authentication problems (network versus direct connect, user id, passwords).

4.3.1. Edit deployment descriptor web.xml

4.3.1.1. Changes in web.xml to enable DbForms

The listing shown below is a complete deployment descriptor for a DbForms application. If the web application has additional servlets in addition to the servlets that comprise DbForms, they would be added to the descriptor in the normal manner.

The order of elements in web.xml is important so be sure to follow the DTD/Schema. For convenience, the top level of the root element web-app is shown below.

    
  <!ELEMENT 
      web-app (icon?, display-name?, description?, distributable?,
      context-param*, filter*, filter-mapping*, listener*, 
      servlet*, servlet-mapping*, session-config?, mime-mapping*, 
      welcome-file-list?, error-page*, taglib*, 
      resource-env-ref*, resource-ref*, security-constraint*,
      login-config?, security-role*, env-entry*, ejb-ref*,  
      ejb-local-ref*)>
          

Add the following servlet, servlet-mapping, taglib, and context-param (optional) elements to the web-app element in the /WEB-INF/web.xml file.

The sample listing below has portions keyed to remarks that follow.

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

  <!DOCTYPE web-app
      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
      "http://java.sun.com/dtd/web-app_2_3.dtd">

  <web-app>

    <!--============= DBFORMS STYLING TEMPLATES =============-->
    <context-param> 
      <param-name>templateBase</param-name> 1
      <param-value>templates</param-value> 
    </context-param>

    <!--=========== DbForms Configuration Servlet ===========--> 
    <servlet> 
      <servlet-name>config</servlet-name> 
      <servlet-class>org.dbforms.servlets.ConfigServlet</servlet-class> 

      <init-param> 
        <param-name>log4j.configuration</param-name> 2
        <param-value>/WEB-INF/log4j.properties</param-value> 
      </init-param>

      <init-param> 
        <param-name>digesterDebugLevel</param-name> 
        <param-value>0</param-value> 
      </init-param> 

      <init-param> 
        <param-name>resourceBundle</param-name> 3
        <param-value>ApplicationResources</param-value> 
      </init-param> 

      <init-param> 
        <param-name>validation</param-name> 4
        <param-value>/WEB-INF/validation.xml</param-value> 
      </init-param> 

      <!--More than one validation file is possible-->
      <!--init-param>
        <param-name>validation</param-name>
        <param-value>/WEB-INF/validation.xml</param-value>
      </init-param-->
      
      <init-param> 
        <param-name>validator-rules</param-name> 
        <param-value>/WEB-INF/validator-rules.xml</param-value> 
      </init-param> 

      <load-on-startup>1</load-on-startup> 
    </servlet> 

    <!--=========== DbForms Controller Servlet ==============-->     
    <servlet> 
      <servlet-name>control</servlet-name> 
      <servlet-class>org.dbforms.servlets.Controller</servlet-class> 

      <init-param> 
        <param-name>maxUploadSize</param-name> 5
        <param-value>80000</param-value> 
      </init-param> 
    </servlet> 

    <!--=========== DbForms FileServlet =====================--> 
    <servlet>
      <servlet-name>file</servlet-name> 
      <servlet-class>org.dbforms.servlets.FileServlet</servlet-class>    
      <load-on-startup>2</load-on-startup> 
    </servlet>

    <!--==== Controller Servlet and FileServlet Mappings========-->
    <servlet-mapping> 
      <servlet-name>control</servlet-name> 
      <url-pattern>/servlet/control</url-pattern> 
    </servlet-mapping>  

    <servlet-mapping>
      <servlet-name>file</servlet-name> 
      <url-pattern>/servlet/file</url-pattern> 
    </servlet-mapping>                 

     <!--=========== Session config =====================-->

     <session-config>
       <session-timeout>30</session-timeout>
     </session-config>

     <!--=========== Welcome File List =====================-->

     <welcome-file-list>
       <welcome-file>index.jsp</welcome-file>
     </welcome-file-list>  

     <!--=========== DbForms Tag Library Descriptor ==========--> 

     <taglib>
       <taglib-uri>/WEB-INF/dbforms.tld</taglib-uri>
       <taglib-location>/WEB-INF/dbforms.tld</taglib-location>
     </taglib>

     <!--=========== Application Security Controls ==========--> 

   </web-app>
        

Remarks:

1

This is optional. See Section 11.3.4.1, “Template base directory ” for more information.

2

The initialization parameter log4j.configuration should point to a valid log4j property file. If this parameter is not defined, the log4j standard configuration will be used for logging.

Now that log4j handles all console output, you would think that by setting log4j to display fatal errors only, your console would be virtually empty. Well, no! The current version of DbForms uses the struts digester class to parse xml files at startup. This class sends info to System.out. The initialization parameter digesterDebugLevel setting can be used to specify (to the digester) the amount of detail to display. Values can range from 0 (no debug) to 10 (Show all).

Sample log4j.properties file:

   #begin log4j.props
   #IMPORTANT -  Watch for trailing whitespaces after each statement!!!

   #log4j.rootCategory=debug, stdout, logFile
   #log4j.rootCategory=warn, stdout
   log4j.rootCategory=error, stdout
   log4j.category.org.dbforms=warn

   #out to console 
   log4j.appender.stdout=org.apache.log4j.ConsoleAppender
   log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

   # Pattern to output the caller's file name and line number. 
   log4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

   #out to file 
   log4j.appender.logFile=org.apache.log4j.RollingFileAppender

   log4j.appender.logFile.File=c:\log4j\dbforms.txt
   #log4j.appender.logFile.File=/var/tomcat/logs/dbforms.log
   log4j.appender.logFile.MaxFileSize=100KB
   log4j.appender.logFile.MaxBackupIndex=1
              

3

DbForms v1.1 supports internationalization (i18n). Labels, messages, etc. can be stored in "resource bundle" files and retrieved using appropriate DbForm tags. The optional initialization parameter resourceBundle setting is used to specify the resource bundle root name. Refer to Section 16.2, “Defining Resource Bundles ” for more detail.

4

Enhanced validation features are available because the Apache Commons-Validator framework has been integrated into DbForms. This framework requires the use of two distinct xml descriptor files. The optional initialization parameters validator-rules and validation are used to specify these files. Refer to Section 17.1, “Commons-Validator framework ” for more discussion.

5

The initialization parameter maxUploadSize tells DbForms how big of a file upload it should accept. If no value is set, a default value will be used. Setting any kind of upload size is important to protect the system against DoS (Denial of Service) attacks.

4.3.1.2. Deploying a security aware application

DbForms inherits all of the security controls present in the servlet container, either an application server or a dedicated servlet engine such as Tomcat. Therefore, please refer to documentation for the Application Server or Servlet Engine being used for information on this topic. The Java Servlet specification also contains information about web application security.

A typical security-aware web application deployment descriptor has elements inside its web-app element as shown below.

  <!-- ==================================================== -->
  <!-- =           APPLICATION SECURITY                   = -->
  <!-- ==================================================== -->

  <!-- =================== authentication ================= -->
  <!--
    We protect everything in directory /protected
    however, be sure NOT to protect login.jsp or logout.jsp since,
    in most containers, this would lead to endless recursion
    which ends with a crash of the Java Virtual Machine ("StackOverFlow")
  -->

  <security-constraint>
    <web-resource-collection>
      <web-resource-name>myapp</web-resource-name>
      <url-pattern>/protected/*</url-pattern>
    </web-resource-collection>

    <auth-constraint>
      <role-name>a_users</role-name>
      <role-name>b_users</role-name>
      <role-name>guests</role-name>
    </auth-constraint>
  </security-constraint>

  <!--=================== login config ====================-->
  <login-config>
    <auth-method>FORM</auth-method>
    <realm-name>Form-Based Authentication Area</realm-name>

    <form-login-config>
      <form-login-page>/login.jsp</form-login-page>
      <form-error-page>/login.jsp</form-error-page>
    </form-login-config>
  </login-config>

  <!--<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>Example Basic Authentication Area</realm-name>
  </login-config>-->

  <!--=================== roles ===========================-->
  <security-role>
    <description>Have read only access</description>
    <role-name>guests</role-name>
  </security-role>

  <security-role>
    <description>Registered users can do stuff, others not.</description>
    <role-name>a_users</role-name>
  </security-role>

  <security-role>
    <description>Registered users can do less then users_a</description>
    <role-name>b_users</role-name>
  </security-role>
        

Remarks:

User authentication and mapping of users to roles are both performed by the servlet container. For Tomcat, this is controlled via the Realm element in the conf/server.xml file.

DbForms provide some additional security controls beyond the controls inherited through the container. See Chapter 9, Security for fine-grained definition of rights for data access and manipulation.

4.3.2. Typical deployment structure of applications using DbForms

Finally, here a short list of files to check when deploying a web application using DbForms:

Example 4.1. Structure of a typical DbForm application

Bold items are required.

  /yourfile1.jsp
  /yourfile2.jsp
  /yourfile1.html
  /yourfile2.html
  /yourfile1.css

  ../templates/yourTemplateA/yourTemplateA_begin.jsp
  ../templates/yourTemplateA/yourTemplateA_end.jsp
  ../templates/templateB/templateB_begin.jsp
  ../templates/templateB/templateB_end.jsp

  /WEB-INF/web.xml
  /WEB-INF/dbforms-config.xml
  /WEB-INF/dbforms.tld
  /WEB-INF/log4j.properties
  /WEB-INF/validation.xml
  /WEB-INF/validator-rules.xml

  /WEB-INF/lib/dbforms2.X.Y.jar
  /WEB-INF/lib/cactus-13-1.7dev20041109.jar
  /WEB-INF/lib/cewolf-0.10.1.jar
  /WEB-INF/lib/commons-beanutils-1.6.jar
  /WEB-INF/lib/commons-collections-2.1.jar
  /WEB-INF/lib/commons-dbcp-1.1.jar
  /WEB-INF/lib/commons-digester-1.5.jar
  /WEB-INF/lib/commons-el-1.0.jar
  /WEB-INF/lib/commons-fileupload-1.0.jar
  /WEB-INF/lib/commons-httpclient-2.0.jar
  /WEB-INF/lib/commons-lang-2.0.jar
  /WEB-INF/lib/commons-logging-1.0.3.jar
  /WEB-INF/lib/commons-pool-1.1.jar
  /WEB-INF/lib/commons-validator-1.0.2.jar
  /WEB-INF/lib/hsqldb-1.7.1.jar
  /WEB-INF/lib/httpunit-1.5.4.jar
  /WEB-INF/lib/itext-1.01.jar
  /WEB-INF/lib/jasperreports-0.6.4.jar
  /WEB-INF/lib/jcommon-0.9.6.jar
  /WEB-INF/lib/jfreechart-0.9.21.jar
  /WEB-INF/lib/junit-3.8.1.jar
  /WEB-INF/lib/log4j-1.2.8.jar
  /WEB-INF/lib/maxq-0.95dev.jar
  /WEB-INF/lib/nekohtml-0.9.3.jar
  /WEB-INF/lib/oro-2.0.8.jar
  /WEB-INF/lib/poi-2.0-final-20040126.jar
  /WEB-INF/lib/protomatter-1.1.8-pre5.jar
  /WEB-INF/lib/rhino-1.5R4.1.jar
  /WEB-INF/lib/servletapi2.3
  /WEB-INF/lib/servletapi2.4
  /WEB-INF/lib/xalan-2.5.1.jar
  /WEB-INF/lib/xercesImpl-2.6.0.jar
  /WEB-INF/lib/xmlParserAPIs-2.2.1.jar
                        
  /WEB-INF/classes/your_package/yourservlet1.class
  /WEB-INF/classes/your_package/yourservlet2.class
                
  /WEB-INF/classes/your_package/yourinterceptorI.class
  /WEB-INF/classes/your_package/yourinterceptorII.class

  /WEB-INF/classes/...                
        

Remarks

The above list of required JAR files is dependent on the version of DbForms being used and also dependent on features of DbForms used in the application. The list is current as of version 2.5 but it is prudent to check the contents of directory dependend in the binary distribution since that directory defines the official set of dependencies.

The JDBC driver may, in some cases such as for simple connections, be placed in the WEB-INF/lib directory.

For Tomcat, the directory common/lib may also be used to place the DbForms JAR file and other dependencies in a single location that can be shared among multiple DbForms applications.

Other dependencies, such as JDBC drivers may be placed in Tomcat directory server/lib where they may be used by Tomcat to be shared among all web applications in the container.

In some cases, when there is a verson conflict between a DbForms dependency and the same library needed by the servlet container, a JAR file must be placed in the server/lib directory (or its equivalent in a container other than Tomcat) instead of the application's WEB-INF/lib directory.