Tuesday, October 13, 2009

Different Ways to get DB connection in java

1. Simple JSP database connectivity

This is first most commonly used database connectivity by entry level java applications. We have to write our code in JSP file and open connection in same file. Advance JSP database connection can include a connection JSP file in all related JSP file where database connection is required.

<%@ page language="java" import="java.sql.*"%>
<%
Connection conn = null;

Class.forName("com.mysql.jdbc.Driver").newInstance();

String sURL="jdbc:mysql://localhost:3306/DBName";
String sUserName="UserName";
String sPwd="Password";

conn = DriverManager.getConnection(sURL,sUserName,sPwd);

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from tableName");
%>


Simple JSP Java database connectivity





<%
if(conn!=null)
conn.close();
%>
Advance JSP connection by including connection file in connection required JSP

dbConn.jsp

<%
Connection conn = null;

Class.forName("com.mysql.jdbc.Driver").newInstance();

String sURL="jdbc:mysql://localhost:3306/DBName";
String sUserName="UserName";
String sPwd="Password";

conn = DriverManager.getConnection(sURL,sUserName,sPwd);

%>
include this file in JSP where connection is required

<%@ page language="java" import="java.sql.*"%>
<%@ include file="dbConn.jsp" %>
<%
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from tableName");
%>


Simple JSP Advance Java database connectivity





<%
if(conn!=null)
conn.close();
%>
2. javaBean database connectivity

javaBean database connection is similar to JSP connection file. But all connection code is written in a javaBean and this javaBean can be used in JSP file with JSP useBean tag to make connection with database.

DBConnection.java

package com.db;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnection {

public Connection getDBConnection() throws Exception{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();

String sURL="jdbc:mysql://localhost:3306/DBName";
String sUserName="UserName";
String sPwd="Password";

conn = DriverManager.getConnection(sURL,sUserName,sPwd);
return conn;
}
}
Use this javabean connection in JSP

<%@ page language="java" import="java.sql.*"%>

<%
Connection conn=dbConn.getDBConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from tableName");
%>


JavaBean java database connectivity





<%
if(conn!=null)
conn.close();
%>
3. web.xml and servlet database connectivity

This is little good and advance technique of connectivity with database. We can define all database variables in web.xml and on load of servlet we can open connection in servlet’s init method through javaBean. In this method, ServletConfig object can be used to get variable parameter field name and value. This method is flexible and can change database name, server name, user and password in web.xml file without changing in the source code.

web.xml


DBInit
com.db.DBInit

DriverName
com.mysql.jdbc.Driver


ServerName
localhost


Port
3306


DatabaseName
dbName


UserName
userName


Password
yourPassword

1

Servlet to initialize the values

DBInit.java

package com.db;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import com.db.DBConnection;

public class DBInit extends HttpServlet {

public void init(ServletConfig config) throws ServletException {

String jdbcDriverName=config.getInitParameter("DriverName");
String jdbcServerName=config.getInitParameter("ServerName");
String jdbcPort=config.getInitParameter("Port");
String jdbcDatabaseName=config.getInitParameter("DatabaseName");
String jdbcUserName=config.getInitParameter("UserName");
String jdbcPassword=config.getInitParameter("Password");

ServletContext sc = config.getServletContext();
// set in application scope of web application to access in future

DBConnection dbConn=null;

try{
dbConn=new DBConnection();

dbConn.setSDriverName(jdbcDriverName);
dbConn.setSServerName(jdbcServerName);
dbConn.setSPort(jdbcPort);
dbConn.setSDatabaseName(jdbcDatabaseName);
dbConn.setSUserName(jdbcUserName);
dbConn.setSPassword(jdbcPassword);

sc.setAttribute("dbConn", dbConn);

}
catch(Exception e)
{
e.printStackTrace();
}

}

}
DBConnection.java

package com.db;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnection {

public String sDriverName=null;
public String sServerName=null;
public String sPort=null;
public String sDatabaseName=null;
public String sUserName=null;
public String sPassword=null;

public String getSDriverName() {
return sDriverName;
}

public void setSDriverName(String driverName) {
sDriverName = driverName;
}

public String getSServerName() {
return sServerName;
}

public void setSServerName(String serverName) {
sServerName = serverName;
}

public String getSPort() {
return sPort;
}

public void setSPort(String port) {
sPort = port;
}

public String getSDatabaseName() {
return sDatabaseName;
}

public void setSDatabaseName(String databaseName) {
sDatabaseName = databaseName;
}

public String getSUserName() {
return sUserName;
}

public void setSUserName(String userName) {
sUserName = userName;
}

public String getSPassword() {
return sPassword;
}

public void setSPassword(String password) {
sPassword = password;
}

public Connection getDBConnection() throws Exception{
Connection conn = null;
Class.forName(sDriverName).newInstance();

String sURL ="jdbc:mysql://"+sServerName+":"+sPort+"/"+sDatabaseName;

conn = DriverManager.getConnection(sURL,sUserName, sPassword);
return conn;
}
}
Connection can be used in JSP as shown in this JSP file

<%@ page language="java" import="java.sql.*"%>

<%
Connection conn=dbConn.getDBConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from tableName");
%>


web.xml and Servlet database connectivity


<%
while(rs.next())
{
rs.getString("id");
}
%>


<%
if(conn!=null)
conn.close();
%>
4. properties file database connectivity

When you need more flexible environment of web application and database connectivity, properties file is another good option for database connectivity. You can change database variable name without changing restarting web server and without changing in java source code. In this technique we have to use properties file and define all database variables values in this file. This properties value can be accessed through ResourceBundle of java. Connection code can be written in javaBean or in JSP file to make connection.

This properties file should be copied in WEB-INF/classes folder

connection_config.properties

# Usually com.mysql.jdbc.Driver
driver.name=com.mysql.jdbc.Driver

# Usually localhost
server.name=localhost

# Usually 3306
port.no=3306

# Usually project
database.name=dbName

# Usually you define
user.name=UserName

# Usually you define
user.password=Password
javaBean is used to make connection

DBConnection.java

package com.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ResourceBundle;

public class DBConnection {

public String sDriverName=null;
public String sServerName=null;
public String sPort=null;
public String sDatabaseName=null;
public String sUserName=null;
public String sPassword=null;

public Connection getDBConnection() throws Exception{
Connection conn = null;

ResourceBundle rb=ResourceBundle.getBundle("connection_config");

sDriverName=rb.getString("driver.name");
sServerName=rb.getString("server.name");
sPort=rb.getString("port.no");
sDatabaseName=rb.getString("database.name");
sUserName=rb.getString("user.name");
sPassword=rb.getString("user.password");

Class.forName(sDriverName).newInstance();

String sURL ="jdbc:mysql://"+sServerName+":"+sPort+"/"+sDatabaseName;

conn = DriverManager.getConnection(sURL,sUserName, sPassword);
return conn;
}
}
connection can be used in JSP file

<%@ page language="java" import="java.sql.*"%>

<%
Connection conn=dbConn.getDBConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from tableName");
%>


properties file database connectivity


<%
while(rs.next())
{
rs.getString("id");
}
%>


<%
if(conn!=null)
conn.close();
%>
5. XML file database connectivity

When your application is more focused on XML based pattern. You can use XML file to make database connection in java. XML is getting more popularity and easy to handle use and implement in web application. These results, we can define all database properties in XML node tree. These values can be getting from any XML parser. Connection code can be defined in javaBean and in JSP file. This is more flexible method. We do not have to restart web server when we do change in XML file and no change will be required to do in source code.

dbConnection.xml



com.mysql.jdbc.Driver
localhost
3306
dbName
root


DBConnection.java

package com.db;

import java.sql.Connection;
import java.sql.DriverManager;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class DBConnection {

public String sDriverName=null;
public String sServerName=null;
public String sPort=null;
public String sDatabaseName=null;
public String sUserName=null;
public String sPassword=null;

public Connection getDBConnection() throws Exception{
Connection conn = null;

DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db =dbf.newDocumentBuilder();
Document doc=db.parse("c:\\tomcat\\webapps\\myApp\\dbConnection.xml");

NodeList driverName = doc.getElementsByTagName("driverName");
NodeList serverName = doc.getElementsByTagName("serverName");
NodeList port = doc.getElementsByTagName("port");
NodeList databaseName = doc.getElementsByTagName("databaseName");
NodeList userName = doc.getElementsByTagName("userName");
NodeList pwd = doc.getElementsByTagName("pwd");

Node nDriverName = driverName.item(0);
sDriverName=nDriverName.getFirstChild().getNodeValue().trim();

Node nServerName = serverName.item(0);
sServerName=nServerName.getFirstChild().getNodeValue().trim();

Node nPort = port.item(0);
sPort=nPort.getFirstChild().getNodeValue().trim();

Node nDatabaseName = databaseName.item(0);
sDatabaseName=nDatabaseName.getFirstChild().getNodeValue().trim();

Node nUserName = userName.item(0);
sUserName=nUserName.getFirstChild().getNodeValue().trim();

Node nPwd = pwd.item(0);
sPassword=nPwd.getFirstChild().getNodeValue().trim();

Class.forName(sDriverName).newInstance();

String sURL ="jdbc:mysql://"+sServerName+":"+sPort+"/"+sDatabaseName;

conn = DriverManager.getConnection(sURL,sUserName, sPassword);
return conn;
}
}
Use in JSP

<%@ page language="java" import="java.sql.*"%>

<%
Connection conn=dbConn.getDBConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from tableName");
%>
<%
if(conn!=null)
conn.close();
%>
6. Connection pooling in java database connectivity

Connection pooling gives good performance on database connectivity over all previous database connection. How it increase performance and efficiency of the database connectivity. When we open new connection in database and after using that connection we closed that connection and it returns again to the connection pool for next waiting. The next time when you need a connection with database, it takes connection from connection pool and reuses the previous connection.

This is database Connection Pooling with Tomcat

tomcat/config/context.xml





WEB-INF/web.xml

auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
username="DBUserName"
password="DBPassword"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/dbName?autoReconnect=true"
maxWait="1000"
removeAbandoned="true"
maxActive="30"
maxIdle="10"
removeAbandonedTimeout="60"
logAbandoned="true"/>


Copy mysql-connector-java.jar MySQL jdbc driver in tomcat/lib folder

DBConnection.java

package com.db;

import java.sql.Connection;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class DBConnection {

public static Connection getConnection() throws Exception
{
return getPooledConnection();
}

public static Connection getPooledConnection() throws Exception{
Connection conn = null;

try{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("No Context");

DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/PooledDB");

if (ds != null) {
conn = ds.getConnection();
return conn;
}else{
return null;
}

}catch(Exception e) {
e.printStackTrace();
throw e;
}
}
}
connection can be used in JSP file

<%@ page language="java" import="java.sql.*"%>

<%
Connection conn=dbConn.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from tableName");
%>
<%
if(conn!=null)
conn.close();
%>
On the basis of all connection method, you can use best suitable method in your application to make database connection.

Wednesday, August 19, 2009

Configuring Tomcat5 and Apache2 with Virtual Hosts using mod_jk

Overview

This tutorial explains how I was able to setup a web server in order to support Java Server Pages (JSP) and Servlets using virtually hosted websites. Although this setup worked for this particular environment, I can make no guarantees that it will work for yours, but it should with some tweaking. I’ll explain later on. I have spent a lot of time gathering several resources in order to get this to work. Many portions of these resources have been deprecated and required a few workarounds. It is my intention that this tutorial will help anyone that has attempted to install such a system without success. If you find any inconsistencies within this tutorial, please notify me at the email address above.

Outlook

The ultimate goal is to provide instructions on how to incorporate JSP/Servlet support on the ISPConfig web hosting software. I felt it was necessary to provide this first segment separately for those that do not wish to use the ISPConfig web hosting control panel.

System Details

The system used in this tutorial has the following installed:

Operating System: Debian Sarge (3.1) http://www.debian.org/

Webserver: Apache 2.0.54 http://www.apache.org/

JDK: JDK 5.0 http://java.sun.com/j2se/1.5.0/download.jsp

Servlet Container: Tomcat http://tomcat.apache.org/

Tomcat Connector: Jakarta Tomcat Connector mod_jk (not mod_jk2)

Debian Sarge (3.1)

If you plan on utilizing ISPConfig to host your websites with Debian Sarge, I highly recommend the how-to provided by Falko Timme here: The Perfect Setup - Debian Sarge (3.1) which prepares your system to support ISPConfig. There are “Perfect Setup� tutorials for other distributions as well. Otherwise, you should be able to find many other resources on the internet for installing Debian. This tutorial is specifically based on configuring Apache and Tomcat to work on Debian using the mod_jk connector.

Apache2

Since installing Apache is beyond the scope of this tutorial, I will assume that you already have Apache 2.0.x installed and running. If you need instructions on installing and configuring Apache 2.0.x please refer to the documentation at the Apache website. Again, you can also find instruction here: The Perfect Setup - Debian Sarge (3.1).

Installing JDK (Java Development Kit)

In order to run Tomcat, you will need to install JDK and set the JAVA_HOME environment variable to identify the location of the JDK environment on your system. I have chosen to use JDK 5.0.

  1. You can download JDK 5.0 at http://java.sun.com/j2se/1.5.0/download.jsp.
  1. Click on Download JDK 5.0 Update 6 to go to the download page.
  1. Click Accept to accept the license agreement.
  1. Next choose the Linux self-extracting file. This is the download for the self-extracting binary file rather than the rpm.
  1. Download to your preferred download directory. Change to that directory and make it executable by executing the following command:

chmod +x jdk-1_5_0_06-linux-i586.bin

  1. Now execute the file:

./jdk-1_5_0_06-linux-i586.bin

  1. You should now have a new directory called j2sdk1.5-sun. Now move this directory to the location where it should be run. I chose /usr/lib/.

mv j2sdk1.5-sun /usr/lib

  1. Now create a symbolic link called jdk to JAVA_HOME by the following command. This allows you to easily switch back and forth between different jvms should you ever need to

cd /usr/lib

ln -s j2sdk1.5-sun jdk

  1. Now we need to set the JAVA_HOME environment variable. Add the following at the end of /etc/profile just after export PATH.

JAVA_HOME="/usr/lib/jdk"

export JAVA_HOME

/etc/profile is executed at startup and when a user logs into the system. In order to update the environment you will need to log out and log back in to the system.

  1. Check to make sure JAVA_HOME is defined correctly by executing the command below. This should report the location of the Java SDK which should be /usr/lib/jdk.

echo $JAVA_HOME

  1. Now test Java with the following command. You should be returned with /usr/bin/java. If so, you have successfully completed this section.

which java

Installing Tomcat

In this section you will download and install Apache Tomcat 5.5.15. For this particular setup, there is no need to build the package from source, we will download the binary version.

  1. Download the binary version to your preferred download directory from here: http://tomcat.apache.org/download-55.cgi. Choose the tar.gz from the core section for 5.5.15.
  1. Now change to that directory and extract the files using the following command:

cd /mydownloads (be sure to change to your download directory)

tar xvzf apache-tomcat-5.5.15.tar.gz

  1. You should now have a new directory called apache-tomcat-5.5.15. Now move this directory to the location where it should be installed. Again, I chose /usr/lib/. Note that this location will be referred to as CATALINA_HOME in the Tomcat documentation.

mv apache-tomcat-5.5.15 /usr/lib

  1. Next change to the /usr/lib/ directory.

cd /usr/lib

  1. Now create a symbolic link called apache-tomcat to CATALINA_HOME by the following command.

ln -s apache-tomcat-5.5.15 apache-tomcat

This will save you from having to make changes to startup and shutdown scripts each time you upgrade Tomcat and if you so desire, it also allows you to keep several versions of Tomcat on your system and easily switch amongst them.

  1. You should now be able to start and stop Tomcat from the CATALINA_HOME/bin directory. If you are using another shell other than the bash shell you will nee to add sh to the beginning of the command. You should now be able to test that Tomcat is installed by starting it and opening your browser and entering http://localhost:8080 into your browser. Port 8080 is the default port for Tomcat and can be easily changed in the /usr/lib/apache-tomcat/conf/server.xml file. (We will work with this file later on.) If you plan to access this page remotely, be sure to forward the respective port to your server’s IP address within your router. You should now see the Tomcat welcome page that contains links to Tomcat documentation as well as sample JSP/Servlet scripts. Verify that Tomcat is running by executing some of the examples found on the welcome page.

cd /usr/lib/apache-tomcat/bin

sh startup.sh

To shutdown the server, you will need to execute the following command. Feel free to try it, but for now we will leave Tomcat running.

sh shutdown.sh

Installing and configuring mod_jk

In order to make the connection between Tomcat and Apache, we will need to download and install mod_jk connector. You will find that the Apache documentation recommends that you install the packaged version of mod_jk if it is available for your particular Linux distribution. Many outdated resources recommend installing the mod_jk2 connector, but I have found that it has been deprecated and although mod_jk was developed before mod_jk2, it is still fully supported and is very stable.

Mike Millson gave some good reasoning behind using mod_jk for connecting Tomcat to Apache for Red Hat here: Integrating Tomcat and Apache on Red Hat Linux.

Here is what he had to say:

"At this point, Apache and Tomcat should be working separately in standalone mode. You can run Tomcat in standalone mode as an alternative to Apache. In fact, in some cases, it is said that Tomcat standalone is faster than serving static content from Apache and dynamic content from Tomcat. However, there are the following compelling reasons to use Apache as the front end:

1. You can use Apache to buffer slow connections. Tomcat uses java.io, which uses a thread for each request, so Tomcat can run out of connections as the number of slow requests grows. This could be an issue if your application supports a large number of dial-up users.

2. You can use a connector such as mod_jk to load balance amongst several Tomcat instances.

3. You can take advantage of Apache features such as cgi and PHP.

4. You can take advantage of Apache modules such as mod_rewrite, mod_headers, and mod_expire.

5. You can isolate virtual hosts in their own Tomcat instances.

The increased functionality obtained by using Apache on the front end can outweigh the effort required to install and configure a connector."

At the time of this writing there were no mod_jk packages available from Dedian.org so we will need to build the package from source which will assure it is compiled for you particular installation anyways. I consider this the safer route although it requires more work.

  1. I chose to download the current source from the Apache archives: http://archive.apache.org/dist/jakarta/tomcat-connectors/jk/source/jk-1.2.15/. Download the jakarta-tomcat-connectors-1.2.15-src.tar.gz file to your /usr/src/ directory.
  1. Change to the /usr/src directory.

cd /usr/src

  1. Next, extract the contents to create the /usr/src/jakarta-tomcat-connectors-1.2.15-src directory.

tar xvzf jakarta-tomcat-connectors-1.2.15-src.tar.gz

  1. Change to the /usr/src/jakarta-tomcat-connectors-1.2.15-src/jk/native directory.

cd jakarta-tomcat-connectors-1.2.15-src/jk/native

  1. Now you are ready to create the custom configure file for your system. Execute the following:

./buildconf.sh

This will create a configure file in the /usr/src/jakarta-tomcat-connectors-1.2.15-src/jk/native directory.

  1. Execute the following command in order to configure mod_jk for your system.

Important note: You will need to have apxs2 (APache eXtension tool) installed and configured with Apache. If you do not have it, as was my case, you can download and install the apache2-threaded-dev package (which replaced the former apache-dev package) from www.debian.org. At the time of this writing, the Debian package archive at www.debian.org was down and they referred me to their temporary site until they resolved their issues pdo.debian.net. I found the apache2-threaded-dev package and was able to install it successfully.

Be sure to include the correct location apxs2 on your system in the path of the command.

./configure --with-apxs=/usr/bin/apxs2

  1. Now build the mod_jk with the following:

make

  1. Finally, if you were successful with the previous commands, copy the newly created mod_jk.so to your Apache2 modules directory. My modules were located at /usr/lib/apache2/modules.

cd apache-2.0

cp /usr/src/jakarta-tomcat-connectors-1.2.15-src/jk/native/apache-2.0/mod_jk.so /usr/lib/apache2/modules

You now are ready to move to the next stage which is to begin configuring Apache and Tomcat. You can find more information about the mod_jk connector at http://tomcat.apache.org/connectors-doc/howto/apache.html.

Configure Tomcat

Create the workers.properties file

Important note: Be sure to make a backup copy of your config files before modifying.

The workers.properties file contains the details about how each process is linked to Tomcat by defining workers that communicate through the ajpv13 protocol. Refer to the Workers HowTo for more detail.

  1. First create the workers.properties file in your Apache2 root directory.

touch /etc/apache2/workers.properties

  1. Next, open the workers.properties file and ad the following. You can find many other examples of the workers.properties file on the internet, but this is the one that I created and it seems to work fine with the other portions that have already been configured in this tutorial.

workers.tomcat_home=/usr/lib/apache-tomcat


workers.java_home=/usr/lib/jdk

ps=/

worker.list=worker1

worker.worker1.port=8009
worker.worker1.host=localhost
worker.worker1.type=ajp13
worker.worker1.lbfactor=1

worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=worker1

worker.inprocess.type=jni

worker.inprocess.class_path=$(workers.tomcat_home)$(ps)lib$(ps)tomcat.jar

worker.inprocess.cmd_line=start

worker.inprocess.jvm_lib=$(workers.java_home)$(ps)jre$(ps)lib$(ps)
i386$(ps)classic$(ps)libjvm.so

worker.inprocess.stdout=$(workers.tomcat_home)$(ps)logs$(ps)inprocess.stdout
worker.inprocess.stderr=$(workers.tomcat_home)$(ps)logs$(ps)inprocess.stderr

3. Save and close the file.

  1. Now we need to configure the server.xml file located at /usr/lib/apache-tomcat/conf/. There are endless ways to configure the server.xml file, but I will provide to you how I did it on this server based on the other sections of this tutorial. First make a copy of your original server.xml file and rename it.

  1. Delete the original contents and add the following to the original server.xml file.














type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />










maxThreads="150" minSpareThreads="5" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />



enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />










resourceName="UserDatabase"/>



unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">

directory="logs" prefix="localhost_access_log." suffix=".txt"
pattern="common" resolveHosts="false"/>



unpackWARs="true" autoDeploy="true">



directory="logs" prefix="web1_access_log." suffix=".txt"
pattern="common" resolveHosts="false"/>


modJk="/usr/lib/apache2/modules/mod_jk.so"
workersConfig="/etc/apache2/workers.properties"/>







6.In order to run the Tomcat auto-config you need to assure
that the following line represents the appropriate locations to your mod_jk.so


7.Restart Tomcat to create the auto-config file.

Important note: Make sure Apache2 is not running before restarting Tomcat or else auto-config will not create the config file.


     file and workers.properties

cd /usr/lib/apache-tomcat/bin

sh shutdown.sh

sh startup.sh

  1. Provided you didn’t have any errors restarting Tomcat, you should have a newly created file in /usr/lib/apache-tomcat/conf/auto/ called mod_jk.conf.

  1. Now we need to open the /etc/apache2/apache2.conf file and add the following line at the bottom.

Include /usr/lib/apache-tomcat/conf/auto/mod_jk.conf

Configure Apache

Important note: Be sure to make a copy of your config files before modifying.

  1. Open your Apache2 configuration file and add the following just below the line Include /usr/lib/apache-tomcat/conf/auto/mod_jk.conf.

# Where to find workers.properties

JkWorkersFile /etc/apache2/workers.properties

# Where to put jk logs
JkLogFile /var/log/apache2/mod_jk.log

# Set the jk log level [debug/error/info]
JkLogLevel info

# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"

# Globally deny access to the WEB-INF directory

AllowOverride None
deny from all

  1. This next section assumes you are using virtual hosts. In this system I am running ISPConfig where all of my Vhosts are defined in a separate file.

Otherwise, if you are not using ISPConfig and your Vhosts are defined within your main Apache2 config file, you just need to add this code there within the desired Vhosts that you want to utilize Tomcat (JSP/Servlets).

If you are not running Vhosts, simply add this code at the bottom of your main Apache2 config file.

For those running ISPConfig as I am, you will need to either manually place this code in the master Vhost (localhost) in the /etc/apache2/vhosts/Vhosts_ispconfig.conf or the selected Vhosts that you want to utilize Tomcat (JSP/Servlets) using the ISPConfig control panel—in the Apache Directives window for each website.

This is up to you.

For this server, I have the ISPConfig Vhosts defined in /etc/apache2/vhosts/Vhosts_ispconfig.conf.

You can either place this code after the tag or just before the end tag. ISPConfig documentation recommends that you use the control panel to include any additional virtual host directives.

# Send servlet for context /servlets-examples to worker named worker1

JkMount /*/servlet/* worker1
# Send JSPs for context /jsp-examples to worker named worker1
JkMount /*.jsp worker1

Conclusion

To finalize and test this configuration you will need to copy the Tomcat example JSP files to the virtual host web directory that was defined in the server.xml, apache2.conf, and Vhosts_ispconfig.conf files. In the server.xml file you will notice that I am referencing the jsp-examples directory where jsp-examples is the name of the docbase. This will give you some idea of how the web applications will be setup for your website. You can find more details on the Tomcat website on how to generate your original web applications. In order to define another web application, you would need to define another worker (i.e. worker2) in the workers.properties file, add another host in the server.xml file, and add the same directives (using worker2 of course) to the respective VHost section.

      

unpackWARs="true" autoDeploy="true">

jsp-examples
" debug="0" reloadable="true"/>

directory="logs" prefix="web1_access_log." suffix=".txt"
pattern="common" resolveHosts="false"/>

  1. First copy just the jsp-examples directory to web1.

cp -R /usr/lib/apache-tomcat/webapps/jsp-examples /home/www/web1/web

  1. Next you need to restart Tomcat as described earlier and then restart Apache. Remember to restart Tomcat first so you will regenerate the mod_jk.conf file.

cd /usr/lib/apache-tomcat/bin

sh shutdown.sh

sh startup.sh

/etc/init.d/apache2 restart

  1. Finally test the websites by entering the respective url into your browser:

http://www.domain1.org/jsp-examples/

You should see the same jsp-examples html as you did in the default Tomcat page from earlier.

Congratulations! Your server should now be ready to support JSP/Servlets. Again, if you find any inconsistencies within this tutorial, please contact me so I can make the appropriate corrections.






file.

that the following line represents the appropriate locations to your mod_jk.so
file and workers.properties
file.