title: JAVA JSON Restfull WebService No 'Access-Control-Allow-Origin' header is present on the requested resource
body: <p>I have a JAVA RESTful webservice which will return JSON string and it was written in Java. My problem is when I send request to that webservice with below URL</p>
  
  <pre><code>http://localhost:8080/WebServiceXYZ/Users/insert
  </code></pre>
  
  <p>it's giving me the below error message</p>
  
  <pre><code>XMLHttpRequest cannot load http://localhost:8080/WebServiceXYZ/Users/insert/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 500.
  </code></pre>
  
  <p>Here is my Code</p>
  
  <pre><code>package com.lb.jersey;
  
  import javax.ws.rs.POST;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.MediaType;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  @Path("/Users")
  public class RegistrationService
  {
      @POST
      @Produces(MediaType.APPLICATION_JSON)
      @Path("/insert")
      public String InsertCredentials (String json) throws JSONException
      {
          java.util.Date dt = new java.util.Date();
          java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          String currentTime = sdf.format(dt);
  
          String phone = null;
          JSONObject returnJson = new JSONObject();
          try
          {
              JSONObject obj = new JSONObject(json);
              JSONObject result1 = obj.getJSONObject("Credentials");
              phone = result1.getString("phone");
              DBConnection conn = new DBConnection();
  
              int checkUserID = conn.GetUserIDByPhone(phone);
              if(checkUserID &lt;= 0)
              {
                  DBConnection.InsertorUpdateUsers(phone, currentTime);
              }
  
              int userID = conn.GetUserIDByPhone(phone);
              int otp = (int) Math.round(Math.random()*1000);
  
              DBConnection.InsertorUpdateCredentials(userID, otp, currentTime);
  
              JSONObject createObj = new JSONObject();
              createObj.put("phone", phone);
              createObj.put("otp", otp);
              createObj.put("reqDateTime", currentTime);
              returnJson.put("Credentials", createObj);
  
              System.out.println(returnJson);
          }
          catch (Exception e)
          {
          }
          return returnJson.toString();
      }
  }
  </code></pre>
  
  <p>My web.xml code</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"&gt;
    &lt;display-name&gt;WebServiceXYZ&lt;/display-name&gt;
    &lt;servlet&gt;
      &lt;servlet-name&gt;Jersey REST Service&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.sun.jersey.server.impl.container.servlet.ServletAdaptor&lt;/servlet-class&gt;
      &lt;init-param&gt;
        &lt;param-name&gt;com.sun.jersey.config.property.packages&lt;/param-name&gt;
        &lt;param-value&gt;com.lb.jersey&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Jersey REST Service&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>I already read many articles but no progress, so please let me know, How can I handle this issue?</p>