Enabling CORS in tomcat

If you need to enable CORS in tomcat ( or alternatively just serve your response in JSON-P format) you will need version 7.0.41 or greater.

Simply add the following in your web.xml file

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

This will ensure that the client browser honors the response that is returned from your app server.
You only need this if the client domain is different from the server which is returning your response.

localhost is not allowed by Access-Control-Allow-Origin

Recently I ran into the following error while working on my node/express app & client side ajax/jsonp using jquery.

 http://localhost is not allowed by Access-Control-Allow-Origin.

This is a typical CORS error – one domain to another via JSONP ( you can run into this on the same domain different ports – localhost:80 to localhost:3001 etc)

Just add the following in your node/express app

response.header(“Access-Control-Allow-Origin”, “*”);

 

If you are running into this on Chrome browser then you will also need to add the following

 

response.header(“Access-Control-Allow-Headers”, “Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With”);

response.header(“Access-Control-Allow-Methods”, “GET, POST, PUT”);

 

 

Brief Instructions – DisplayTagAjaxed

I have gotten numerous emails on the setup/installation/how-to’s on this.
The files of interest will be the displayTagAjax.js file (mainly changeLinks
function) and index.jsp

Note the div wrapped around the displaytag taglibrary(<div id=”ajxDspId”>…). This div id is referenced in displayTagAjax.js as well. If you need to change this id, then change it in both the places (index.jsp and displayTagAjax.js)

Everything else is magic 🙂

What really happens is that once displayTag library renders the page, changeLinks is fired which rewrites the anchor links.

Follow-up to displayTagAjax’ed

For those who need the application war file and source code, Please download it from my website

DisplayTag + JQuery = DisplayTagAjaxed

I have seen posts/blogs/articles about ajaxing displayTag. I first put my hands on ajaxTags. It seemed to work fine but once the application was ported to Weblogic 8.1 SP6 it stopped working. This is mainly because of the conflict with standard.jar.

I looked at AjaxAnywhere and although I didn’t use it, I found the implementation grammar long.

All I wanted was something that will not want me to make modifications to the applications deployment descriptions or include any jar files etc.

So I decided to write my own and no not any wrappers around the displayTag taglibrary but by simply using JQuery. 100% pure JS.

The thought process was simple, the pagination/sorting should not reload the page but just call a JS function which will do the magical ajax call, fetch the data and replace the html contents..

A live demo can be seen at here

Posts to follow on documentation and how-to’s. If you are in a hurry..just do a view-source and grab the JS files. If anyone needs the sample application source and/or war file, email me at write2vijay at gmail dot com