title: Enabling CORS in Java Web Service
body: <p><strong>NOTE: I already figured out the solution.  I'm just posting the issue and resolution in case others have a similar issue</strong></p>
  
  <p><strong>Long story short:</strong> </p>
  
  <p>I'm getting this CORS error message on my browser's Developer Tools window when I have a REACT client side app make an HTTP post to a Java Web Server I have running on Payara server running from NetBeans:</p>
  
  <blockquote>
    <p>SEC7120: [CORS] The origin '<a href="http://localhost:3000" rel="nofollow noreferrer">http://localhost:3000</a>' did not find
    '<a href="http://localhost:3000" rel="nofollow noreferrer">http://localhost:3000</a>' in the Access-Control-Allow-Origin response
    header for cross-origin  resource at
    '<a href="http://localhost:8080/DatabaseWebService/databaseConnectorMethod" rel="nofollow noreferrer">http://localhost:8080/DatabaseWebService/databaseConnectorMethod</a>'.</p>
  </blockquote>
  
  <p>For more info about CORS please refer to this site <a href="https://enable-cors.org/" rel="nofollow noreferrer">https://enable-cors.org/</a></p>
  
  <p><strong>Long story detailed:</strong></p>
  
  <p>I have a REACT project running on <a href="http://localhost:3000" rel="nofollow noreferrer">http://localhost:3000</a> making an HTTP post to a Java Web Service I have (DatabaseWebService) that is running on a local Payara Server version 4.1.1.  The Web Service URL that the REACT app is requesting is '<a href="http://localhost:8080/DatabaseWebService/databaseConnectorMethod" rel="nofollow noreferrer">http://localhost:8080/DatabaseWebService/databaseConnectorMethod</a>'</p>
  
  <p>Everytime I run the REACT code I get the CORS error listed above.</p>
  
  <p>Here's my REACT code making the http post request:</p>
  
  <pre><code>            const postData = new URLSearchParams();
              postData.append('var1', 'somedata1');
              postData.append('var2', 'somedata2');
              let axiosConfig = {
                  headers: {
                      'Content-Type': 'application/x-www-form-urlencoded'                
  }
              };
  
          axios.post('http://localhost:8080/DatabaseWebService/databaseConnectorMethod', postData, axiosConfig)
              .then((res) =&gt; {
                  console.log("AXIOS POST RESPONSE RECEIVED: ", res);
              })
              .catch((err) =&gt; {
                  console.log("AXIOS POST ERROR: ", err);
              })
  </code></pre>
  
  <p>Here's my servlet code:</p>
  
  <pre><code>public class DatabaseConnectorServlet extends HttpServlet {
          @Override
          final protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
                  throws ServletException, IOException {
  
              try {
                  System.out.println("DatabaseConnectorServlet Received a HTTP POST");  
  
                  String var1= request.getParameter("var1");
                  String var2= request.getParameter("var2");
                  System.out.println(var1 + var2);
                  response.getWriter().print("I GOT YOUR MESSAGE");
              } catch (Exception ex) {                          
                  response.getWriter().print(ex);
              }
          }
  </code></pre>
  
  <p>I put breakpoints in the Java code above and see it getting hit and I see that it executes all the commands including the response.getWriter().print with no issue.  However when I look at the browsers developer tools Console window I do see the CORS exception.</p>