Java solves cross-domain issues in front and back of the table

Source: Internet
Author: User

This article mainly introduces the use of cors to achieve Javaweb cross-domain request problem method, very good, with reference value, the need for friends can refer to the following

Before solving cross-domain problems with JSONP, we now use cors to implement cross-domain requests to solve Java cross-domain issues:

The main code is as follows

Package com.hy.fliter;

Import java.io.IOException;

Import Javax.servlet.Filter;

Import Javax.servlet.FilterChain;

Import Javax.servlet.FilterConfig;

Import javax.servlet.ServletException;

Import Javax.servlet.ServletRequest;

Import Javax.servlet.ServletResponse;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.commons.httpclient.HttpStatus;

/**

* Created by Wangshuai on 2016/7/30.

*/

public class Corsfilter implements Filter {

@Override

public void init (Filterconfig filterconfig) throws Servletexception {}

@Override

public void DoFilter (ServletRequest servletrequest, Servletresponse servletresponse, Filterchain Filterchain) throws IOException, Servletexception {

HttpServletResponse response = (httpservletresponse) servletresponse;

HttpServletRequest request = (httpservletrequest) servletrequest;

Specify allow other domain names to be accessed

Response.setheader ("Access-control-allow-origin", "*");

Response type

Response.setheader ("Access-control-allow-methods", "POST, GET, delete, OPTIONS, delete");

Response Header Settings

Response.setheader ("Access-control-allow-headers", "Content-type, X-requested-with, X-custom-header, Haiyi-access-token ");

if ("Options". Equals (Request.getmethod ())) {

Response.setstatus (httpstatus.sc_no_content);

}

Filterchain.dofilter (ServletRequest, servletresponse);

}

@Override

public void Destroy () {}

}

The Web. XML code is configured as follows

<filter>

<filter-name>cors</filter-name>

<filter-class>com.hy.fliter.CorsFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>cors</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

Java solves cross-domain issues in front and back of the table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.