Skip to content

在 OSX 上使用 Tomcat

Published: at 00:00
查看版本
java -version

java version "1.7.0_21"
Java(TM) SE Runtime Environment (build 1.7.0_21-b12)
Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)
下载Tomcat
http://tomcat.apache.org/download-70.cgi

wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.57/bin/apache-tomcat-7.0.57.tar.gz
解压安装
tar zxvf apache-tomcat-7.0.57.tar.gz -C /opt/

rm -rf /Library/Tomcat

ln -s /opt/apache-tomcat-7.0.57 /Library/Tomcat

chown -R nate /Library/Tomcat

chmod +x /Library/Tomcat/bin/*.sh
启动
/Library/Tomcat/bin/startup.sh
停止
/Library/Tomcat/bin/shutdown.sh
编写测试文件
package me.yhz;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
路径配置
编写 web/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://		xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
	<servlet>
    	<servlet-name>ServletTest</servlet-name>
    	<servlet-class>me.yhz.Test</servlet-class>
	</servlet>

	<servlet-mapping>
    	<servlet-name>ServletTest</servlet-name>
    	<url-pattern>/index</url-pattern>
	</servlet-mapping>
</web-app>
配置Tomcat
vim /opt/apache-tomcat-7.0.57/conf/server.xml

在<Host></Host>之间加入

<Context  path="/test"  docBase="/Users/nate/java/test/web" debug="0" reloadable="true" crossContext="true"/>

访问方式:
http://127.0.0.1:8080/test/index