Archive for March, 2009

Chapter 6: Advanced Tomcat Features Figure 6-8: No

Tuesday, March 24th, 2009

Chapter 6: Advanced Tomcat Features Figure 6-8: No role-typed MBean is available when lifecycle listeners are disabled. Adding APR Lifecycle Listener and Native SSL Engine Configuration Apache Portable Runtime (APR) is a platform-specific binary library that Tomcat 6 can make use of to boost its Web server performance. The following line in server.xml loads the APR support class, initializes the APR library, and turns on its SSL engine: There exists a single configurable attribute associated with the AprLifecycleListener in Tomcat 6, called SSLEngine. The default value of SSLEngine, if not specified, is on. The following table summarizes the listener configuration. Attribute Description Required? className SSLEngine A listener class that loads and initializes the APR library. This is set to org.apache.catalina.core.AprLifecycleListener. Can be set to on, off, or a specific SSL engine name supported by the APR library. Default is on. This attribute can be useful if you are using OpenSSL native code based SSL implementation and/or a hardware-accelerated SSL solution. Yes No In addition to initialization, the AprLifecycleListener listener also intercepts the after stop event of the server component and calls the terminate method of the APR library to give it a chance to clean itself up.

If you looking for unlimited one inclusive web hosting plan please check web hosting plan website.

Chapter 6: Advanced Tomcat Features Figure 6-7: Lifecycle

Tuesday, March 24th, 2009

Chapter 6: Advanced Tomcat Features Figure 6-7: Lifecycle listener created role-type MBeans Note that you will have to authenticate to use the managerapplication. This means that the manager role must be added to the tomcat-users.xmlfile (either manually or via the adminapplication) for the manager application user. In this test case, the manager role was added to the default Tomcat user. (Chapter 8 discusses the managerapplication.) Removing Default Lifecycle Listeners Next, the lifecycle listeners will be removed. As a result, it is expected that the dynamically created MBeans will no longer be available. To try this out, first stop Tomcat 6. Edit the server.xmlfile by hand and comment out the two elements. The following code shows the elements commented out: –> This means that no lifecycle listener will be configured for the server component, and no role-typed MBeans will be created. Start Tomcat 6 and try the previous proxy URL again. This time, no role-typed MBean will be found, as shown in Figure 6-8 . Because the lifecycle listeners responsible for creating the role-typed MBeans are not configured, the query reveals that no role-typed MBeans are available.

For reliable and cheap web hosting services please check cheap web hosting website.

Chapter 6: Advanced Tomcat Features Although the XML

Monday, March 23rd, 2009

Chapter 6: Advanced Tomcat Features Although the XML configuration syntax of a lifecycle listener is similar to the configuration of a nested component inside a container, technically, a listener is not an architectural component (and definitely not a nested component). A lifecycle listener should be thought of as an extended attribute of the containing XML element. Lifecycle listeners are configured as XML subelements instead of XML element attributes for the following reasons: . Multiple lifecycle listeners can be associated with a single component. . Each listener can be configured with its own set of attributes. The element, representing a lifecycle listener, can be configured with the attributes described in the following table. Attribute Description Required? className descriptors The Java programming language class that implements the listener logic. This class must implement the org.apache .catalina.LifecycleListenerJava interface. A semicolon-separated list of MBean descriptor XML files. This attribute is used to provide JMX compatibility (see Chapter 17 on Tomcat s JMX support) for custom components (that is, custom Valves and custom Realms). Yes No Tomcat 6 Lifecycle Listeners Configuration Tomcat 6 has two custom listener classes that will intercept lifecycle events and create (or destroy) management objects (called MBeans) to support external management of Tomcat. These two custom listener classes are as follows: . org.apache.catalina.mbeans.ServerLifecycle: A listener to create/destroy MBeans for management of Tomcat architectural components. . org.apache.catalina.mbeans.GlobalResourcesLifecycle: A listener to create/destroy MBeans for management of any global resources that may be externally manageable. Displaying MBeans Created by Lifecycle Listeners Using the Manager JMX Proxy The two lifecycle listeners are configured in the default server.xml file. You can see the result of the created MBeans by using the manager application s JMX proxy servlet. Try the following URL: http://localhost:8080/manager/jmxproxy/?qry=*%3Atype%3DRole%2C* This proxy servlet enables you to query specific MBeans that are created within the Tomcat 6 server. MBeans representing roles are created only by the lifecycle listeners described earlier. When these life- cycle listeners are not hooked in, the roles MBeans are not created. The preceding query will enumerate all the roles MBeans. The query should return a list of the four manageable objects (MBeans) for the roles that are defined in tomcat-users.xml, as shown in Figure 6-7 .

For reliable and cheap web hosting services please check cheap web hosting website.

Chapter 6: Advanced Tomcat Features Configuring Lifecycle Listeners

Sunday, March 22nd, 2009

Chapter 6: Advanced Tomcat Features Configuring Lifecycle Listeners Many top-level and nested components in the Tomcat 6 architecture (including Server, Service, Realm, and so on) support the configuration of lifecycle listeners. Lifecycle listeners are Java code modules that can be hooked into the server logic and executed during specific moments during the lifecycle of a component. This capability enables new custom functionality to be introduced to the Tomcat 6 server without having to change the core server code base. With Tomcat 6, the only explicit use of a lifecycle listener is to insert code that enables the server to be managed remotely (through JMX support). Using a simple example, the following section shows how this support code is configured. Lifecycle Events Sent by Tomcat Components Lifecycle listeners are customized code that listens to specific lifecycle events. Lifecycle events are sent by a component, to any configured listener, at well-defined points in a component s lifecycle. These points include the following: . Just before component startup . During component startup . Just after component startup . Just before component stop . During component stop . Just after component stop Developers may use lifecycle listeners to add new processing logic to the Tomcat server. As an administrator, you can add these custom listeners by creating a XML element within the associated component. The Element You can add a lifecycle listener to a Tomcat component (if the component supports lifecycle listeners) by configuring a XML element within the XML definition of the component. Most Tomcat 6 architectural components support lifecycle listeners. In Tomcat 6, listeners for the component are used to create JMX MBeans that represent runtime server structures and global resources. JMX MBeans are objects that enable Tomcat components, structures, and resources to be monitored or accessed via an external management system. Chapter 17 provides more extensive coverage of JMX. More specifically, Tomcat 6 uses the following default server.xmlfragment to add JMX MBean support:

For high quality website hosting services please check cheap web hosting website.

Chapter 6: Advanced Tomcat Features This is the

Sunday, March 22nd, 2009

Chapter 6: Advanced Tomcat Features This is the JSP that will accept user input and submit the e-mail details to the SendMailServletfor sending. Sending E-mail via JavaMail Sessions Start Tomcat 6, and you can test the example that uses JavaMail to send e-mail. Use the following URL: http://localhost:8080/examples/jsp/mail/sendmail.jsp Figure 6-6 shows the JSP-generated form that you can fill out to send e-mail. Figure 6-6: Accessing JavaMail via JNDI You can fill out the form shown in Figure 6-6 to actually send an e-mail message (assuming that you have the SMTP server configured properly). This JSP collects information for an e-mail message from the user, and then submits it to the SendMailServlet for processing and sending. The following code shows how SendMailServlet (or other Web application code) can look up and utilize the JNDI mail session: private final Object lock = new Object(); … // Acquire our JavaMail session object Session session = null; Synchronized(lock) { Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup( java:comp/env ); session=(Session) envCtx.lookup( mail/Session ); } …

If you looking for unlimited one inclusive web hosting plan please check web hosting plan website.

Chapter 6: Advanced Tomcat Features … mail/Session javax.mail.Session

Saturday, March 21st, 2009

Chapter 6: Advanced Tomcat Features … mail/Session javax.mail.Session Container … Downloading and Installing JavaMail 1.3.3 and the JavaBeans Activation Framework 1.1 Libraries Check the %CATALINA_HOME%/lib directory to determine whether you have mail.jarand activation.jar libraries there. These are the JavaMail and JavaMail-dependent JAF libraries. If they are not there, they will need to be downloaded. JavaMail support is a part of the J2EE download, or it can be obtained as an optional download. You can find the required mail.jar library as part of the JavaMail distribution, downloadable from the following URL: http://java.sun.com/products/javamail/downloads/index.html The latest version available at the time of this writing is JavaMail 1.3.3. Because JavaMail 1.3.3 depends on JAF, you will also need to download the JavaBeans Activation Framework from the following URL: http://java.sun.com/products/javabeans/jaf/index.jsp The library that you will need from this download is activation.jar. Compiling and Configuring the SendMailServlet The code distribution includes the binaries for SendMailServlet. This sample servlet may already be part of your Tomcat 6 distribution. If not, copy the SendMailServlet.classfile into the webapps/ examples/WEB-INF/classesdirectory. To configure the servlet, add the following servlet definition and mapping to the web.xmldeployment descriptor of the jsp-examples Web application: SendMailServlet SendMailServlet SendMailServlet /mail/SendMailServlet Creating the sendmail.jsp JSP If sendmail.jsp is not included with your Tomcat 6 distribution, copy it from the code distribution to the %CATALINA_HOME%/webapps/examples/jsp/mail subdirectory (create this directory if necessary).

For high quality java hosting services please check tomcat web hosting website.

Chapter 6: Advanced Tomcat Features Note the use

Friday, March 20th, 2009

Chapter 6: Advanced Tomcat Features Note the use of a lock around this lookup code. This ensures serialized access to the JNDI lookup code. There is production evidence on very highly loaded systems that the code may not be totally thread-safe. (Thanks to Anne Horton for this valuable information.) At this point, myConncontains an instance of a database connection, which can be used to access the MySQL database immediately. Configuring Mail Sessions JavaMail is a standard programming API used by Java developers to create and send e-mail. Tomcat 6 supports JavaMail by providing the JNDI configuration of a JavaMail session as a resource, using its own factory code to create a JavaMail session for the Web application. This enables any Web applications running under Tomcat to use JNDI to look up and use the session. The example in the following section shows how to send e-mail from within a JSP using a JavaMail session configured as a JNDI resource. The JSP will post a form to a collaborating servlet. The servlet will use the configured JavaMail session to send the actual e-mail. It takes advantage of the examplesWeb application that is distributed with Tomcat 6. Adding a Resource Definition to the Application Context Descriptor The first step is to configure a mail session as a JNDI resource. In the Context descriptor of the examples Web application (the $CATALINA_HOME/webapps/examples/META-INF/context.xmlfile), add the following resource definition inside the element. If this file and directory do not exist, add them explicitly:
mail.smtp.host localhost
This will configure the JNDI mail/Session Context, referring to an SMTP server running on localhost. If you are connecting to a remote SMTP server, change the value of localhost to the name or IP address of your server. You can also modify the port used (if it is not at the standard port 25) by setting the mail .smtp.portparameter. Adding a Reference to a Mail Session Resource in the Deployment Descriptor In the deployment descriptor (the $CATALINA_HOME/webapps/jsp-examples/WEB-INF/web.xmlfile), you must declare a reference to the JNDI resource. Add the following lines after the declarations, but before the descriptions in the web.xmlfile:

For high quality website hosting services please check cheap web hosting website.

Chapter 6: Advanced Tomcat Features driverClassName com.mysql.jdbc.Driver url

Friday, March 20th, 2009

Chapter 6: Advanced Tomcat Features
driverClassName com.mysql.jdbc.Driver url jdbc:mysql://localhost/wroxtomcat username empro password empass maxActive 20 maxIdle 30000 maxWait 100
In addition to the preceding configuration, the developer must declare the use of the resource in a deployment descriptor (web.xml) using a element, as shown in the following example: jdbc/WroxTC6 javax.sql.DataSource Container Within the Web applications, the DataSource can be looked up relative to the java:comp/envnaming context. The code used will be similar to the following: private final Object lock = new Object(); … Connection myConn = null; synchronized(lock) { Context myInitialContext = new InitialContext(); Context localContext = (Context) myInitialContext( java:comp/env ); DataSource myDataSource = (DataSource) localContext.lookup( jdbc/wroxTC6 ); myConn = myDataSource.getConnection(); } …

For high quality java hosting services please check tomcat web hosting website.

Chapter 6: Advanced Tomcat Features The next section

Thursday, March 19th, 2009

Chapter 6: Advanced Tomcat Features The next section shows how to apply these elements to configure a JDBC DataSource and JavaMail session. Configuring a JDBC DataSource JDBC features, including connections pooling, are directly supported by Tomcat 6. JDBC is discussed at length in Chapter 13 . For now, it is necessary to know only how JDBC connections (as a JNDI resource) can be passed to Web applications. Your JDBC driver can be placed in the $CATALINA_HOME/lib/ directory. This enables the Tomcat server and your applications to find and access this driver. Finally, you must configure the JNDI resource factory using and elements. In this case, you are configuring the database factory to use the MySQL database, with an application-wide scope. This instance of the DataSource will be available only to the associated application. The definition needs to be placed into a context.xmlfile under the webapps/ [application name]/META-INFdirectory: This segment configures Tomcat s built-in JDBC DataSource factory. The built-in DataSource factory implementation in Tomcat is org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory . A DataSource factory is a class from which new instances of DataSource objects can be obtained. With this factory, the configuration parameters described in the following table are possible. Parameter Description driverClassName maxActive maxIdle maxWait user password url validationQuery Java programming language class name of the JDBC driver. This driver should be placed in the $CATALINA_HOME/lib directory for easy location by the DataSource factory code. The maximum number of active connections in this pool. The maximum number of idle connections in this pool. In milliseconds, indicates the maximum wait for a connection by the DataSource factory before throwing an exception. The user ID used to log on to the database. The password used to log on to the database. The JDBC-compatible URL specifies the database instance to be used. An optional SQL query used to validate a connection. Essentially, the factory will perform this query to ensure that rows are returned before considering the connection valid. For example, you can parameterize the defined JDBC resource by using the following elements:

For high quality jboss hosting services please check jboss web hosting website.

Chapter 6: Advanced Tomcat Features The Element The

Wednesday, March 18th, 2009

Chapter 6: Advanced Tomcat Features The Element The element associates parameters with the resource manager already configured in a element. This element is often used to configure the resource manager. For example, if the is a JDBC DataSource, the may contain the Relational Database Management Server (RDBMS) server location, login name, and password to use. The element can contain the attribute shown in the following table. Attribute Description Required? name Name of corresponding resource Yes Each element can contain one or more / subelements, expressed as follows:
password wrox123
The Element The element refers to a previously configured JNDI resource (typically in the subelement associated with a server), making these resources available to all , , and components. This enables resources to be defined and shared across servers or globally. A element can have the attributes described in the following table. Attribute Description Required? global name type The name of the resource being linked to The name of the resource, accessible by Web application via JNDI lookup The Java programming language class name indicating the type of resource returned Yes Yes Yes For example, if the UserDatabase element is already defined in the server s subelement (see the element example earlier), then it can be referred to within a element of a Web application using the following: This entry will link the previously defined UserDatabaseinstance (named myDatabasein ) to the JNDI addressable resource called localDatabase. The Web application can perform a JNDI lookup for localDatabaseand obtain access to the UserDatabase instance.

For high quality java hosting services please check java web hosting website.