In the previous posts, I showed you how to use jsonp to bypass browser restrictions that prevent a webpage on one server (apache) from accessing resources on another (tomcat). jsonp works will but it has restrictions of its own, such as, it only allows GET. So you can’t use the full language of REST (POST, PUT, DELETE). And really, is it still REST then?
There’s a hack to get around that, but compounding hacks on top hacks is rarely a good idea unless you have a lot of old coffee grind you need to use up.
Another solution is to use the webpage’s server as a proxy server. Apache lets you do that with two modules: mod_jd and mod_proxy.
I won’t get too much into them, but here’s a short comparison I found:
- No need for a separate module compilation and maintenance. mod_proxy, mod_proxy_http, mod_proxy_ajp and mod_proxy_balancer comes as part of standard Apache 2.2+ distribution
- Ability to use http https or AJP protocols, even within the same balancer.
- mod_proxy_ajp does not support large 8K+ packet sizes.
- Basic load balancer
- Does not support Domain model clustering
- Advanced load balancer
- Advanced node failure detection
- Support for large AJP packet sizes
- Need to build and maintain a separate module
I decided to use mod_proxy because the module comes built into apache, which means less configuration and less possibility of error.
Here’s how simple it was to set up.
I added one line to the httpd.conf file under the main directive:
ProxyPass /pass/ http://localhost:8080/
var matchUri = "http://localhost:8080/kodingnotes/rest/foo/read/jsonp";
var matchUri = "http://localhost/pass/kodingnotes/rest/foo/read";
Now, I can use the original (non-jsonp) jQuery call and apache will forward the request to the non-JXONWithPadding wrapper java method.