Tomcat配置启动脚本和服务

vim /etc/init.d/tomcat

#!/bin/bash
#
# chkconfig: - 95 15
# description: Tomcat start/stop/status script

#Location of JAVA_HOME (bin files)

export JAVA_HOME=/usr/local/jdk.1.8.0_144

#Add Java binary files to PATH
export PATH=$JAVA_HOME/bin:$PATH

#CATALINA_HOME is the location of the configuration files of this instance of Tomcat
CATALINA_HOME=/usr/local/apache-tomcat-8.5.13

#TOMCAT_USER is the default user of tomcat
TOMCAT_USER=www

#TOMCAT_USAGE is the message if this script is called without any options
TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"

#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
SHUTDOWN_WAIT=20

tomcat_pid() {
echo `ps -ef | grep $CATALINA_HOME | grep -v grep | tr -s " "|cut -d" " -f2`
}

start() {
pid=$(tomcat_pid)
if [ -n "$pid" ];then
echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
else
echo -e "\e[00;32mStarting tomcat\e[00m"
if [ `user_exists $TOMCAT_USER` = "1" ];then
su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
else
$CATALINA_HOME/bin/startup.sh
fi
status
fi
return 0
}

status(){
pid=$(tomcat_pid)
if [ -n "$pid" ];then
echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
else
echo -e "\e[00;31mTomcat is not running\e[00m"
fi
}

加入服务列表

chkconfig --add tomcat

启动停止tomcat

service tomcat start/stop

Tomcat 跨域问题处理方案

1、允许Tomcat所有域访问,需要新建文件clientaccesspolicy.xml和crossdomain.xml加入%TOMCAT_HOME%\webapps\ROOT 目录下

clientaccesspolicy.xml

<?xml version=”1.0″ encoding=”utf-8″ ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers=”*”>
<domain uri=”*”/>
</allow-from>
<grant-to>
<resource path=”/” include-subpaths=”true”/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>

crossdomain.xml

<?xml version=”1.0″?>
<cross-domain-policy>
<allow-access-from domain=”*” />
</cross-domain-policy>

2、maven配置

<dependency>
    <groupId>com.thetransactioncompany</groupId>
    <artifactId>cors-filter</artifactId>
    <version>2.4</version>
</dependency>
<dependency>
    <groupId>com.thetransactioncompany</groupId>
    <artifactId>java-property-utils</artifactId>
    <version>1.9.1</version>
</dependency>
3、ROOT下的web.xml
<!--解决tomcat部署跨域问题  -->
<filter>
    <filter-name>CORS</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
    <init-param>
        <param-name>cors.allowOrigin</param-name>
        <param-value>*</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportedMethods</param-name>
        <param-value>GET, POST, HEAD, PUT, DELETE</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportedHeaders</param-name>
        <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value>
    </init-param>
    <init-param>
        <param-name>cors.exposedHeaders</param-name>
        <param-value>Set-Cookie</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportsCredentials</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CORS</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>