Passa ai contenuti principali


Visualizzazione dei post da 2014

Tomcat mod_proxy configuration

Configuring Tomcat with mod_proxy  mod_proxy configuration is the simplest way to integrate Apache tomcat with Apache server. In order to do that,we need to add the module and redirect the URL to a virtual host.
Open the httpd.conf:
1. Place the following lines of code after the other LoadModule directives:

LoadModule proxy_module modules/ LoadModule proxy_http_module modules/ 2. Place the following lines of code with your other VirtualHost, or at the bottom of the file:

NameVirtualHost *<VirtualHost *>ServerName ProxyRequests Off <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPass / http://localhost:8080/ ProxyPassReverse / http://localhost:8080/ <Location/> Order allow,deny Allow from all</Location></VirtualHost> Save the configuration file and restart Apache Web server in order to test the integration using mod_proxy.
[root@localhost bin]# ./apachectl stop
[root@root@localhost bin]# ./apachectl start

How to configure Tomcat JVM parameters

How to increase JVM size in Tomcat In order to change the JVM heap size for Tomcat, we need to pickup the file and modify the value for the JAVA_OPTS parameter. For example, suppose we want to change the max heap size from 256 MB to 512 MB while setting the Perm Gen = 256 MB.
Here's how to modify the JAVA_OPTS:
JAVA_OPTS="-Xms128m -Xmx512m -XX:MaxPermSize=256m" The changes in the JVM settings will take effect on the server restart. You can verify the change done in the JVM parameter by running the jmap command that is part of the JDK distribution. This tool requires passing the processId of Tomcat. Example:

C:\Users\tomcat>jmap -heap 3860
Attaching to process ID 3860, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.51-b03

using thread-local object allocation.
Parallel GC with 4 thread(s)

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 2122317824 (2024.0MB)
   NewSize   …

Tomcat Context Path configuration

Tomcat context path configuration Every time we deploy an application we need to setup a context path for the application. The context path will be used for accessing the application from a browser:
Even if it's actually possible to deploy an application on the Root Context Path ("/") this is usually reserved in special cases, for example if you need to provide a welcome page for your server or in other circumstances.

Enabling a context path The context path in Tomcat can be enabled in two ways:

GUI using the Tomcat Web Application ManagerCommand-line configuration in server.xml
In order to enable Context Path using the GUI interface, login into the Tomcat Application Manager:

You can create the context path using the Deploy tab.

Click on Browse and select the required WAR file. Then click on Deploy. It will take some seconds seconds to deploy the application (based on the application size) .
The following screenshot shows the appl…

Tomcat Manager Configuration

This tutorial shows how to configure the tomcat manager application that can be used to administrate Tomcat. We will also show how to enable the default user and password and how to grant the roles required to run the manager application.
The Tomcat Manager is a very powerful tool for Tomcat administration. It allows you to use the following features:

Remote application deployment Cleanup of Idle sessionApplication undeployment and redeploymentAnalysis of memory leaksJVM statusServer status
The Tomcat Manager by default is disabled in Tomcat 7. In order to enable it, pickup the tomcat-users.xml in the conf folder of Tomcat 7. This file contains two users: tomcat and role1. These roles however are not enabled to use the manager gui, as you need an user with the "manager-gui" roles. So change the file so that it look like this:

<tomcat-users><role rolename="manager-gui"/><user username="tomcat" password="s3cret" roles="manager-gu…

Tomcat Websocket example

A WebSocket is a full-duplex communication mechanism that allows both textual and binary messages to be sent between clients and servers, without the HTTP request/response life cycle. WebSockets allow either the client or the server to send a message at any time, providing an asynchronous solution for working with data while the user is performing a task.
Websockets API are included in Tomcat 7 Web server distribution so you don't have to download any extra library:

In this tutorial we will show how to create a WebSocket example using Apache Tomcat and Eclipse.
Start by creating on Eclipse a new Dynamic project named websocket-example:
We will now create a server side class named WebSocketDemo that is going to echo messages from a Javascript client:
package com.sample;import;import java.nio.ByteBuffer;import javax.websocket.OnMessage;import javax.websocket.Session;import javax.websocket.server.ServerEndpoint; @ServerEndpoint("/echo") publicclass WebSoc…

Tomcat session timeout

Tomcat session timeout is defined into the CATALINA_HOME/conf/web.xml file by the following directive:
<web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation=""><session-config><session-timeout>30</session-timeout></session-config></web-app> You can however override the default session timeout by setting in your application's WEB-INF/web.xml file the session timeout as well.

The number in session-timeout tag is in minutes. To make it unlimited or no time out, simply set the number to -1 and restart your Tomcat Service.
<web-app version="2.5" xmlns=

Tomcat configuration files

A Tomcat distribution contains the following configuration files which are necessary for proper administration of the Web server. Let's see in detail each file:

catalina.policy : This file contains the security policy permissions for Tomcat 7. It enforces the security policy permissions by JVM on the web : This file describes the shared definition of the server, shared loader, and JARs, which need to be scanned at the time of the server startup.server.xml : This is the main configuration file for Tomcat and it mainly contains the Connector port configuration. It holds critical information, such as the IP address, port, virtual host, context path, and so on.tomcat-users.xml : This file is used for authentication, authorization, and role-based definitions. It is used to implement a database of users/passwords/roles for authentication and container-managed security. To add/remove users or assign/unassign roles to existing users, edit this file.logging.p…

Yum install tomcat on Linux

This tutorial shows how to perform a Tomcat 7 installation using the yum installation utility.
The pre-requisite for installing Tomcat is that you have a JDK on your machine, if you haven't got one, you can install it by executing as root user:
[root@localhost ~]#  yum install java
Next you can start installing Tomcat. There are at least two ways for installing it:
Installation option 1: Move to and download the latest stable release of Tomcat. Once done, unzip it to a folder where you have writing permissions:
[tomcat@localhost ~] $ unzip

Once installed, you can start Tomcat by executing from the TOMCAT_HOME/bin folder:
[tomcat@localhost ~] $
Installation option 2: You can use the yum utility for installing Tomcat. At the time of writing, just Tomcat 6 is available on the yum repository. You can install it by running: [root@localhost ~]# yum install tomcat6 tomcat6-webapps tomcat6-admin-webapps
The tomcat6-webapps is not m…

Tomcat Access logs

Access logs are customized logs, which record all requests processed by the server giving information about:

The user that has accessed the applicationWhat components of the application are accessedRemote IP and so on
These logs play a vital role in traffic analysis of many applications to analyze the bandwidth requirement and they also help in troubleshooting the application under a heavy load. These logs are configured in  server.xml in  TOMCAT_HOME/conf .
You can configure access logs according to the environment and your auditing requirements. Let's see the pattern format of the access logs and understand how we can customize the logging format:

And now some explanation about the parameters contained in the AccessLogValve:

Class Name: This parameter defines the class name used for the generation of logs. By default, Apache Tomcat 7 uses the  org.apache.catalina.valves.AccessLogValve class for the access logs.Directory: This parameter defines the directory location for the log fi…

How to change Tomcat port

By default, Tomcat runs on port 8080 using the HTTP protocol. As this port is well known by developers, it is easier for hackers to hit the port and trap the server. So it's always recommended to change the connector port and also the AJP port, which runs on 8009, to secure Tomcat.
Connectors are configured in server.xml in the conf directory of your Tomcat installation.
Before changing Tomcat port, you should verify that the port is not being used by other services. You can check the following operating system configuration files in order to avoid port conflict between the two services.

Linux: /etc/services
Windows: C:\Windows\System32\drivers\etc\services

Tomcat users configuration

Configuring Tomcat users is required in order to be able to use Tomcat Manager application. The main configuration file for tomcat users is tomcat-users.xml that resides in the conf folder of Tomcat 7. In this file, user roles and their authentication are configured.
Here is for example how to configure it to add one user named tomcat that has all roles required to manage the manager GUI application and the Manager script shell:

tomcat connectiontimeout configuration

Apache tomcat contains a connectionTimeout parameter which defines the amount of time Tomcat will wait for three things:

The total amount of time it takes to receive an HTTP GET request.The total amount of time between receipt of TCP packets on a POST or PUT request.The total amount of time between ACKs on transmissions of TCP packets in responses.
Changing the connectionTimeout In some cases it might be necessary to increase it especially if the response takes too much to be completed on the server, leaving the connection without a response. To increase the connection timeout on tomcat server follow the following steps :

1. Open the file server.xml residing in TOMCAT_HOME/conf/.
2. Set the variable connectionTimeout in it to Value in Milliseconds.(i.e 1000 milliseconds = 1 second)

For example :
If connectionTimeout is not defined, the default value is 60000 (i.e. 60 seconds); however the standard server.xml that ships with Tomcat sets this to 20000 (i.e. 20 seconds).
You can use a val…

Tomcat Maven plugin tutorial

This tutorial will show you how you can create a Web application with Maven and deploy it using Maven plugin for Tomcat in a matter of minutes. The prerequisite is that you have installed in your machine:
Apache Maven 2.X or 3.X Apache Tomcat 7.X  JDK 1.6 or higher  We will show how to create the project from Eclipse IDE. From Eclipse File menu choose to create a New | Maven Project:

In the next screen make sure that the option Create a Simple project (Skip archetype selection) is unchecked and select a Workspace for your project:

Click Next. In the following screen choose an archetype for your project. There are plenty of available Maven archetype that are suitable for a Web application to be executed on Tomcat. We would suggest to use the maven-archetype-webapp that will create a startup skeleton for your Web projects:

Finally in the last screen choose a GroupId for your project. Choose an ArtifactId that will match to the Eclipse project name. Select a Version and a Package for you…

Tomcat Thread Pool configuration

In order to accept incoming requests, Tomcat uses a Thread pool. You can configure two types of Thread Pools: Shared Pool and Dedicated Pool.
A Shared pool as the name inplies, can be shared among various components in Tomcat. So, for example if you have three connectors in your configuration, then you can use a single shared pool to serve requests for all of them. Here is how to configure a shared thread pool:

Open Tomcat's server.xml file and include the Executor definition:

namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/>
Once you have added the shared thread pool, you need to reference it in your Connector configuration as follows:
namePrefix="catalina-exec-" maxThreads="200" minSpareThreads="4"/>
A Dedicated threadpool is a thread pool which is dedicated to only one connector definition. You can use it in a scenario when you are expecting a peak of connections and you want to have ful…

Tomcat Datasource configuration using MySQL

Configuring a MySQL Datasource on Tomcat Pre-requisite. Download MySQL JDBC Driver from:

In order to configure a Datasource on Tomcat for MySQL Database you need to follow these three simple steps:

1) Define the Datasource in server.xml
Include a Datasource configuration in your Tomcat's server.xml file within the Context section, containing information about the JDBC URL, username and password and the maximum number of active connections allowed in the pool:

<Resource name="jdbc/MySQLDS" auth="Container" type="javax.sql.DataSource" username="root" password="admin" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/schema" maxActive="15" maxIdle="7" defaultTransactionIsolation="READ_COMMITTED" validationQuery="Select 1"/>2) Install…

Tomcat Datasource configuration using Oracle

Pre-requisite. Download Oracle JDBC Driver from: Oracle JDBC Drivers are available at Oracle Site:

In order to configure a Datasource on Tomcat for Oracle Database you need to follow these three simple steps:

1) Define the Datasource in server.xml
Include a Datasource configuration in your Tomcat's server.xml file within the Context section, containing information about the JDBC URL, username and password and the maximum number of active connections allowed in the pool:

<Resource name="jdbc/OracleDS" auth="Container" type="javax.sql.DataSource" username="scott" password="tiger" driverClassName="com.mysql.jdbc.Driver" url="jdbc:oracle:thin:@localhost:1521:test" maxActive="15" maxIdle="7" defaultTransactionIsolation="READ_COMMITTED" …