Using Cors to implement Javaweb cross domain request problems _java

Source: Internet
Author: User

Before resolving cross-domain problems with JSONP, you are now using Cors to implement Cross-domain requests to resolve Java Cross-domain problems:

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 Servletexcepti On {} @Override public void Dofilter (ServletRequest servletrequest, Servletresponse servletresponse, Filterchain FilterC
Hain) throws IOException, servletexception {httpservletresponse response = (httpservletresponse) servletresponse;
HttpServletRequest request = (httpservletrequest) servletrequest;
Specifies that other domain names are allowed to access 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 >

The above is a small set to introduce the use of cors to achieve Javaweb cross-domain request of the method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.