Java cross-domain processing
Version One:
Package com.api.filters;
Import java.io.IOException;
Import Java.io.PrintStream;
Import Java.io.PrintWriter;
Import Java.io.StringWriter;
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.HttpServletResponse;
Private Filterconfig filterconfig = null;
Public Crossfilter () {super (); @Override public void init (Filterconfig filterconfig) throws servletexception {this.filterconfig = fil
Terconfig;
if (filterconfig!= null) {if (debug) {log ("crossfilter:initializing filter"); @Override public String toString () {if (Filterconfig = null) {return("Crossfilter ()");
StringBuffer sb = new StringBuffer ("Crossfilter (");
Sb.append (Filterconfig);
Sb.append (")");
Return (sb.tostring ());
@Override public void Dofilter (ServletRequest request, servletresponse response, Filterchain chain)
Throws IOException, Servletexception {if (debug) {log ("Crossfilter:dofilter ()"); } if (response instanceof HttpServletResponse) {httpservletresponse Alteredresponse = (httpservletr
Esponse) response); I need to find a way to make sure this only gets called on//200-300 HTTP responses//todo:s
EE above comment addheadersfor200response (alteredresponse);
Dobeforeprocessing (request, response);
Throwable problem = null;
try {chain.dofilter (request, response); The catch (Throwable t) {//If an exception is thrown somewhereDown the filter chain,//We still want to execute our processing, and then//Rethrow the P
Roblem after that.
problem = t;
T.printstacktrace ();
Doafterprocessing (request, response);
If There was a problem, we want to rethrow it if it is//a known type, otherwise log it. if (problem!= null) {if (problem instanceof Servletexception) {throw (servletexception) pro
Blem;
} if (problem instanceof IOException) {throw (IOException) problem;
} sendprocessingerror (problem, response); @Override public void Destroy () {} private void dobeforeprocessing (ServletRequest request, Se Rvletresponse response) throws IOException, servletexception {if (debug) {log ("Crossfilt
Er:dobeforeprocessing "); }} private void DoafterprocEssing (ServletRequest request, servletresponse response) throws IOException, servletexception {if (DE
Bug) {log ("crossfilter:doafterprocessing"); }} private void Addheadersfor200response (HttpServletResponse response) {//Todo:externalize the allow-
Origin Response.AddHeader ("Access-control-allow-origin", "*");
Response.AddHeader ("Access-control-allow-methods", "POST, Get, OPTIONS, put, DELETE, head"); Response.AddHeader ("Access-control-allow-headers", "X-pingother, Origin, X-requested-with, Content-type, A"
Ccept ");
Response.AddHeader ("Access-control-max-age", "1728000"); } private void Sendprocessingerror (Throwable t, servletresponse response) {String StackTrace = Getstacktrace
(t); if (stacktrace!= null &&!stacktrace.equals ("")) {try {response.setcontenttype ("Tex
T/html "); PrintStream PS = new PrintsTream (Response.getoutputstream ());
PrintWriter pw = new PrintWriter (PS); Pw.print ("
version Two: (Easy version)
public class Crossfilter implements Filter {public
void Dofilter (ServletRequest req, servletresponse Res, Filterchai N chain) throws IOException, servletexception {httpservletresponse
response = (httpservletresponse) res;
Response.setheader ("Access-control-allow-origin", "*");
Response.setheader ("Access-control-allow-methods", "POST, Get, OPTIONS, DELETE");
Response.setheader ("Access-control-max-age", "3600");
Response.setheader ("Access-control-allow-headers", "X-requested-with");
Chain.dofilter (req, res);
public void init (Filterconfig filterconfig) {} public
void Destroy () {}
}
Finally, Web.xml
<filter>
<filter-name>crossFilter</filter-name>
<filter-class> com.api.filters.crossfilter</filter-class>
</filter>
<filter-mapping>
< filter-name>crossfilter</filter-name>
<url-pattern>/*</url-pattern>
</ Filter-mapping>
Reference:
http://www.cnblogs.com/chen-lhx/p/6170687.html
http://blog.csdn.net/luosijin123/article/details/48002375
http://www.cnblogs.com/fx2008/p/4024971.html