Passa ai contenuti principali

Post

Visualizzazione dei post da 2009

Tomcat pooling

With Database Connection Pooling (DBCP), we can scale our applications to handle increased load and deliver high performance benefits. Using recycled database connection objects cuts the time taken to re-instantiate and load frequently used objects, thus reducing unnecessary overheads.

Our approach to DBCP uses the Jakarta-Commons database connection pool. But first, we need to configure the JNDI DataSource in Tomcat by adding a declaration for the resource to server.xml file, which resides inside the /conf directory of your Tomcat installation (indicated by the environment variable CATALINA_HOME). The JNDI DataSource is used as a factory for connections. One of the major advantages of using a configuration like this is that the characteristics of the pool can be changed without affecting the application code.

server.xml ========================================
.........


<Host > <Context path="/edi-web" docBase="edi-web" debug="5" r…

Tomcat axis tutorial

Axis is essentially a SOAP engine, that is a framework for constructing SOAP processors such as clients, servers, gateways, etc.

Installing Axis on Tomcat

At first download Axis from the Apache project:

http://ws.apache.org/axis/java/index.html

Explode the zip folder. Now you need to deploy the Axis engine to Tomcat: Here's how to do it:

Rename the folder axis under AXIS_HOME/webapps to axis.war and copy the folder under "webapps" of your tomcat

Verify that Axis has been deployed correctly:

http://localhost:8080/axis/


















Publishing Web Services with Axis

Let's say we have a simple class like the following:



package test;

public class HelloWorld {

public String hello(String message) {

return "Invoked with" +message;

}

}
How do we go about making this class available via SOAP? There are a couple of answers to that question, but we'll start with an easy solution.
At first compile the class :
javac -d . HelloWorld.java
Now copy the class under WEB-INF/classes of your axis…

Tomcat maxThreads configuration

Tomcat maxThreads represents the maximum number of request
processing threads to be created by the HTTPConnector.

<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol" maxThreads="250" SSLEnabled="true" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" connectiontimeout="20000"/> This determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to the default value of 200.

How the process works:
At server startup, the HTTP Connector will create a number of processing threads based on the value configured for the minSpareThreads attribute. Each incoming request requires a thread for the duration of that request. If the number of simultaneous requests cannot be handled by the currently available request processing threads, additional threads will be created up to the configured maximu…

Tomcat context listener example

When a web application is deployed a servlet context object, ServletContext, is created and associated with the web application. There is a one-to-one relationship between a servlet context object and the web application. All resources within the web application, such as servlets and JSPs, can retrieve any information stored in the servlet context.
');

As a web application programmer, you may want to initialize objects and place them in the servlet context when it is created and destroy the objects when the servlet context is destroyed.

For example, you may decide to create a connection to a database when the servlet context is created and close the connection when the servlet context is destroyed.

To write an application lifecycle event listener that executes when the servlet context is created and destroyed, write a Java class that implements the javax.Servlet.ServletContextListener class.

This class has two methods with the following signatures (taken from the JavaDocs):

void context…

Tomcat session listener example

As part of Servlet 2.3 specification, we can now make use of session creation and destruction events. Our listener object will be called every time a session is created or destroyed by the server.

You can use two interfaces as listener for your Session:

HttpSessionListener triggers the listener when a new session is created or destroyedHttpSessionBindingListener triggers the listener when an Object is bound/unbound from the SessionHttpSessionListener example:

package com.sample;

import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;

public class SessionCounter implements HttpSessionListener {

private static int activeSessions = 0;

public void sessionCreated(HttpSessionEvent se) {
activeSessions++;
}

public void sessionDestroyed(HttpSessionEvent se) {

if(activeSessions > 0)
activeSessions--;
}



public static int getActiveSessions() {
return activeSessions;
}

}
As you can see, all you have to do is implementing two methods: sessionCreated() method
wi…

Tomcat web xml reference

According to the Servlet 2.4 specification, every Web application should include a deployment descriptor (web.xml file). This file must be placed in the WEB-INF/ directory of the Web application.

There is also a web.xml file under the $CATALINA_HOME/conf directory. This file is similar to a Web application’s web.xml file. However, this particular web.xml file is used to specify the default properties for all Web applications that are running within this server instance.

Be very careful when making modifications to this file (such as any additions or changes) because they will affect all Web applications running on the same server instance. Note also that other application servers may or may not support a global default web.xml, as this is not a requirement for Servlet 2.4 standard compliance.





Web.xml can be formally validated against a schema:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation…