1.1.1. JAVA APPLICATION SERVER - TOMCAT 7
a) Cài đặt và cấu hình Tomcat 7
[root@linux ~]#wget http://ftp.riken.jp/net/apache/tomcat/tomcat-7/v7.0.23/bin/apache-tomcat-7.0.23.tar.gz
[root@linux ~]#tar zxvf apache-tomcat-7.0.16.tar.gz
[root@linux ~]#mv apache-tomcat-7.0.16 /usr/tomcat7
[root@linux ~]#useradd -d /usr/tomcat7 tomcat
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
[root@linux ~]#chown -R tomcat. /usr/tomcat7
b) Tạo script INIT
[root@linux ~]# 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@linux ~]#chmod 755 /etc/rc.d/init.d/tomcat7
[root@linux ~]#/etc/rc.d/init.d/tomcat7 start
Starting Tomcat7:
Using CATALINA_BASE: /usr/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@linux ~]#chkconfig --add tomcat7
[root@linux ~]#chkconfig tomcat7 on
c) Truy cập vào trình duyệt web theo địa chỉ Access to "http://(your hostname or IP address):8080/, trong ví dụ này là http://serverlinux.vn:8080/
d) Cấu hình truy cập với port khác port 8080 trong URL.
[root@linux ~]#vi /etc/httpd/conf.d/proxy_ajp.conf
# add at the last line
ProxyPass /tomcat/ ajp://localhost:8009/
#Khởi động lại dịch vụ web
[root@linux ~]#/etc/rc.d/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
e) Truy cập vào trình duyệt theo địa chỉ http://(your hostname or IP address)/tomcat/" với port mới 8009 như ở bước c
f) Tạo bài test servlet hiển thị giờ và ngày hiện tại của hệ thống
[root@linux ~]#mkdir /usr/tomcat7/webapps/ROOT/WEB-INF/classes
[root@linux ~]#chown tomcat. /usr/tomcat7/webapps/ROOT/WEB-INF/classes
[root@linux ~]#cd /usr/tomcat7/webapps/ROOT/WEB-INF/classes
[root@linux 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@linux classes]#javac -classpath /usr/tomcat7/lib/servlet-api.jar daytime.java
[root@linux 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>