Tomcat基本安装测试
/==============================基本安装使用=========================================>/
Tomcat A 设置
安装jdk
http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html下载JDK
1.1安装jdk
~]# rpm -ivh jdk-7u67-linux-x64.rpm
Preparing... ########################################### [100%]
1:jdk ########################################### [100%]
Unpacking JAR files...
rt.jar...
jsse.jar...
charsets.jar...
tools.jar...
localedata.jar...
jfxrt.jar...
1.2 配置Java环境
~]# vi /etc/profile.d/java.sh
1..3 加载java环境
~]# source /etc/profile.d/java.sh
~]# java -version
java version "1.7.0_67"
Java(TM) SE Runtime Environment (build1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build24.65-b04, mixed mode)
安装tomcat
2.1下载tomcat
http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.65/bin/apache-tomcat-7.0.57.tar.gz
2.2 解压安装
~]# tar xf apache-tomcat-7.0.57.tar.gz -C/usr/local/
~]# cd /usr/local/
创建连接
]# ln -sv apache-tomcat-7.0.57 tomcat
`tomcat‘ -> `apache-tomcat-7.0.57‘
2.3 创建用户修改属主
]# useradd -M -d /usr/local/tomcat tomcat ]# chown -R tomcat /usr/local/tomcat* |
2.4 创建启动脚本
]# vim /etc/rc.d/init.d/tomcat #!/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/local/tomcat 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 } RETVAL=0 start(){ echo "Starting Tomcat: " su - $TOMCAT_USER -c "$CATALINA_HOME/bin/startup.sh" RETVAL=$? echo [ $RETVAL -eq 0 ] && touch $LOCKFILE return $RETVAL } stop(){ echo "Shutting down Tomcat: " $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 ;; *) echo $"Usage: $0 {start|stop|restart}" exit 1 ;; esac exit $? |
2.5 修改属性
]# chmod 755 /etc/rc.d/init.d/tomcat
2.6 测试启动脚本
l]# /etc/rc.d/init.d/tomcat start Starting Tomcat: Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/default Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. |
2.7 添加到服务列表
]# chkconfig --add tomcat
2.8 开机自动启动
]# chkconfig tomcat on
]# chkconfig--list tomcat
tomcat 0:off 1:off 2:on 3:on 4:on 5:on 6:off
3.测试
3.1 测试主页
3.2 创建自定义的网页文件
]# mkdir -pv /usr/local/tomcat/webapps/ROOT/WEB-INF/classes mkdir: created directory `/usr/local/tomcat/webapps/ROOT/WEB-INF/classes‘ 修改属主 ]# chown tomcat . /usr/local/tomcat/webapps/ROOT/WEB-INF/classes ]# cd /usr/local/tomcat/webapps/ROOT/WEB-INF/classes 创建自定义的主页面 ]#vim 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>\n<head>\n<title>DayTime-TomcatA</title>\n</head>\n<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>\n</body>\n</html>"); } } |
]# javac -classpath /usr/local/tomcat/lib/servlet-api.jar daytime.java |
3.3 添加自定义的web内容
]# vim /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml [[email protected] classes]# cat /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml <?xml version="1.0" encoding="ISO-8859-1"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <display-name>Welcome to Tomcat</display-name> <description> Welcome to Tomcat </description> <!-- start--> <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> <!-- end--> </web-app> |
3.4测试
|
Tomcat B 设置
1.安装 jdk
~]# rpm -ivh jdk-7u67-linux-x64.rpm Preparing... ########################################### [100%] 1:jdk ########################################### [100%] Unpacking JAR files... rt.jar... jsse.jar... charsets.jar... tools.jar... localedata.jar... jfxrt.jar... |
1.2 配置Java环境
~]# vim /etc/profile.d/java.sh export JAVA_HOME=/usr/java/default export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar |
1.3 加载Java环境
~]# source /etc/profile.d/java.sh ~]# java -version java version "1.7.0_67" Java(TM) SE Runtime Environment (build 1.7.0_67-b01) Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode) |
2 安装Tomcat
2.1下载tomcat
2.2 解压安装
[[email protected] ~]# tar xf apache-tomcat-7.0.57.tar.gz -C /usr/local/ [[email protected] ~]# cd /usr/local/ 创建软连接 [[email protected] local]# ln -sv apache-tomcat-7.0.57 tomcat `tomcat‘ -> `apache-tomcat-7.0.57‘ |
2.3 创建用户修改属主
[[email protected] local]# useradd -M -d /usr/local/tomcat tomcat [[email protected] local]# chown -R tomcat /usr/local/tomcat* |
2.4 创建启动脚本
]# vim /etc/rc.d/init.d/tomcat #!/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/local/tomcat 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 } RETVAL=0 start(){ echo "Starting Tomcat: " su - $TOMCAT_USER -c "$CATALINA_HOME/bin/startup.sh" RETVAL=$? echo [ $RETVAL -eq 0 ] && touch $LOCKFILE return $RETVAL } stop(){ echo "Shutting down Tomcat: " $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 ;; *) echo $"Usage: $0 {start|stop|restart}" exit 1 ;; esac exit $? |
2.5 修改属性
]# chmod 755 /etc/rc.d/init.d/tomcat
2.6测试启动脚本
]# /etc/rc.d/init.d/tomcat start Starting Tomcat: Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/default Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. |
2.7 添加到启动服务列表
]# chkconfig --add tomcat
2.8 开机自动启动
]# chkconfig tomcat on ]# chkconfig --list tomcat tomcat 0:off 1:off 2:on 3:on 4:on 5:on 6:off |
3. 测试
3.1 测试主页
|
3.2 创建自定义网页文件
]# mkdir -pv /usr/local/tomcat/webapps/ROOT/WEB-INF/classes mkdir: created directory `/usr/local/tomcat/webapps/ROOT/WEB-INF/classes‘ 修改属主 ]# chown tomcat . /usr/local/tomcat/webapps/ROOT/WEB-INF/classes 创建自定义主页 ]# cd /usr/local/tomcat/webapps/ROOT/WEB-INF/classes ]#vim 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>\n<head>\n<title>DayTime-TomcatB</title>\n</head>\n<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>\n</body>\n</html>"); } } |
编译 ]# javac -classpath /usr/local/tomcat/lib/servlet-api.jar daytime.java |
3.3 添加自定义web内容
]# vim /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml <?xml version="1.0" encoding="ISO-8859-1"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <display-name>Welcome to Tomcat</display-name> <description> Welcome to Tomcat </description> <!-- Start 以下为添加的内容--> <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> <!-- End 结束分割线********--> </web-app> |
3.4 测试
|
Tomcat A B配置文件
http://down.51cto.com/data/2113380