Java Servlets and CORS


Last few weeks, I was working on a project that required me to use Java Servlets. Servlet is a class that manages HTTP requests and responses. I ran into CORS  (Cross Origin Resourse Sharing) problem when I was working on the project. CORS allowed JavaScript applications to make AJAX requests to another domain when these domains are different. My backend where I use Java Servlets was working because I checked multiple times in the Postmaster. In the frontend, when I used fetch function I was not getting any data. Developer tools pointing to the CORS and Access Control Request Methods. Java Servlets are relatively new to me, and I need to research to make my application working. Here are the solutions that I found:


1. Implement Filter by creating a concrete class. Filter is invoked at the preprocessing and postprocessing of a request. 




2. Add filter into web.xml file


3. Install the extension to the browser where we can add whitelisted websites in order to skip the CORS.

The first and second methods are more reliable because it only applies a filter on the program that has been created by us while using an extension to the browser can cause some security issues.

Comments

Popular posts from this blog

Release 0.6

Hacktoberfest - Issue #1, Pull Request #1

Hacktoberfest