Scientific Linux 6
Sponsored Link

JAVA Application Server - Tomcat 7
2011/04/26
  Install Tomcat 7 to configure JAVA Application Server. JAVA Environment and Web Server is also required.

[1] Install and configure Tomcat 7. Get latest Tomcat from here with wget.
[root@www ~]#
wget http://ftp.riken.jp/net/apache/tomcat/tomcat-7/v7.0.12/bin/apache-tomcat-7.0.12.tar.gz

[root@www ~]#
tar zxvf apache-tomcat-7.0.12.tar.gz

[root@www ~]#
mv apache-tomcat-7.0.12 /usr/tomcat7

[root@www ~]#
useradd -d /usr/tomcat7 tomcat

useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
[root@www ~]#
chown -R tomcat. /usr/tomcat7
[2] Create INIT script.
[root@www ~]#
vi /etc/rc.d/init.d/tomcat7


#!/bin/bash

# Tomcat7: Start/Stop Tomcat 7
#
# chkconfig: - 90 10
# description: Tomcat is a Java application Server.

. /etc/init.d/functions
. /etc/sysconfig/network

CATALINA_HOME=/usr/tomcat7
TOMCAT_USER=tomcat
LOCKFILE=/var/lock/subsys/tomcat

RETVAL=0
start(){
   echo "Starting Tomcat7: "
   su - $TOMCAT_USER -c "$CATALINA_HOME/bin/startup.sh"
   RETVAL=$?
   echo
   [ $RETVAL -eq 0 ] && touch $LOCKFILE
   return $RETVAL
}

stop(){
   echo "Shutting down Tomcat7: "
   $CATALINA_HOME/bin/shutdown.sh
   RETVAL=$?
   echo
   [ $RETVAL -eq 0 ] && rm -f $LOCKFILE
   return $RETVAL
}

case "$1" in
    start)
      start
      ;;
   stop)
      stop
      ;;
   restart)
      stop
      start
      ;;
   status)
      status tomcat
      ;;
   *)
      echo $"Usage: $0 {start|stop|restart|status}"
      exit 1
      ;;
esac
exit $?


[root@www ~]#
chmod 755 /etc/rc.d/init.d/tomcat7

[root@www ~]#
/etc/rc.d/init.d/tomcat7 start

Starting Tomcat7:
Using CATALINA_BASE:
/usr/tomcat7

Using CATALINA_HOME:
/usr/tomcat7

Using CATALINA_TMPDIR:
/usr/tomcat7/temp

Using JRE_HOME:
/usr/java/default

Using CLASSPATH:
/usr/tomcat7/bin/bootstrap.jar:/usr/tomcat7/bin/tomcat-juli.jar


[root@www ~]#
chkconfig --add tomcat7

[root@www ~]#
chkconfig tomcat7 on
[3] Access to "http://(your hostname or IP address):8080/" with Web browser and make sure following tomcat site is shown normally.
 
[4] Configure to be able to access without 8080 in URL.
[root@www ~]#
vi /etc/httpd/conf.d/proxy_ajp.conf


# add at the last line

ProxyPass /tomcat/ ajp://localhost:8009/


[root@www ~]#
/etc/rc.d/init.d/httpd restart

Stopping httpd:
[  OK  ]

Starting httpd:
[  OK  ]
[5] Access to "http://(your hostname or IP address)/tomcat/" with web browser and verify tomcat site like [3] section is shown normally.

[6] Create a test servlet that shows current day and time and make sure if it works normally.
[root@www ~]#
mkdir /usr/tomcat7/webapps/ROOT/WEB-INF/classes

[root@www ~]#
chown tomcat. /usr/tomcat7/webapps/ROOT/WEB-INF/classes

[root@www ~]#
cd /usr/tomcat7/webapps/ROOT/WEB-INF/classes

[root@www classes]#
vi daytime.java


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Calendar;

public class daytime extends HttpServlet {

   public void doGet(HttpServletRequest request
      ,HttpServletResponse response)
   throws IOException, ServletException{

      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      Calendar cal = Calendar.getInstance();
      out.println("<html>");
      out.println("<head>");
      out.println("<title>DayTime</title>");
      out.println("</head>");
      out.println("<body>");
      out.println("<div style=\"font-size: 40px; text-align: center; font-weight: bold\">");
      out.println(cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE) + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE));

      out.println("</div>");
      out.println("</body>");
      out.println("</html>");
   }
}


[root@www classes]#
javac -classpath /usr/tomcat7/lib/servlet-api.jar daytime.java

[root@www classes]#
vi /usr/tomcat7/webapps/ROOT/WEB-INF/web.xml


# add follows between <web-app> - </web-app>

<servlet>
   <servlet-name>daytime</servlet-name>
   <servlet-class>daytime</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>daytime</servlet-name>
   <url-pattern>/daytime</url-pattern>
</servlet-mapping>

[7] Access to "http://(your hostname or IP address)/tomcat/daytime" and make sure following page is shown normally.
 
Matched Content