首页 > 代码库 > tomcat跨域请求

tomcat跨域请求

tomcat A请求tomcat B中的数据(本人是在同一台机器上2个tomcat端口不同,在不同机器上有时间会测得)

博主用的是ajax请求 在A 请求B中数据,用下面的方法可以

 

在被请求的tomcat B 应用的web.xml中加入如下代码:

在tomcat B 应用中加入如下jar:

cors-filter-1.7.jar;  java-property-utils-1.9.jar

  <!-- 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> </filter-mapping> <!-- 请求过滤器结束 --> <!-- tomcat跨域请求结束 -->

为了安全考虑可以在过滤器中写上请求的路径

多个过滤器写法:

    <filter-mapping>
        <filter-name>CORS</filter-name>
        <url-pattern>/XXX</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>CORS</filter-name>
        <url-pattern>/XXX</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>CORS</filter-name>
        <url-pattern>/XXX</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>CORS</filter-name>
        <url-pattern>/XXX</url-pattern>
    </filter-mapping>

代码借鉴:http://www.cnblogs.com/chiyouguli/p/4283349.html 

 

 

ps:本文为博主原创,转载请注明出处:http://www.cnblogs.com/Y-zhiwei/。

tomcat跨域请求