0

i'm new in a web service. i'm trying to make a simple web service REST on java for a simple login application.. here's my code:

Server side:

package com.testingws.webservices;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path("/login/{username}/{password}/{datetime}")
public class webServicesClass {
    @GET   // this method process GET request from client
    @Produces("application/json")   // sends JSON
    public String getJson( @PathParam("username") String username, @PathParam("password") String password) {  // empno represents the empno sent from client   
        if (username.equalsIgnoreCase("admin") && password.equalsIgnoreCase("admin")){
            return "{'loginstatus':'success'}";
        }
        else{
            return "{'loginstatus':'failed'}";
        }
    } // end of

}

Client side :

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Client Login</title>
<script type="text/javascript">
    function loginProcess(){
        var tempUser = document.getElementById("loginUsername");
        var tempPass = document.getElementById("loginPassword");

        var dateTime = new Date();
        var url = "http://localhost:8181/TestWSProject/authentication/login/" + tempUser.value + "/" + tempPass.value + "/" + dateTime.toUTCString();

        var xmlhttp = new XMLHttpRequest(); //@slaks: i put it here

        xmlhttp.open('GET',url,true);
        xmlhttp.send(null);
        xmlhttp.onreadystatechange = function() {
               if (xmlhttp.readyState == 4) {
                  if ( xmlhttp.status == 200) {
                       var det = eval( "(" +  xmlhttp.responseText + ")");

                       //alert(det.loginstatus);
                       if(det.loginstatus=="success")
                       {
                           setCookie("login", "yes", 1);
                           window.location="main.html";
                       }
                       else
                       {
                           alert("incorrect username or password");
                       }

                 }
                 else
                       alert("Error ->" + xmlhttp.status + xmlhttp.responseText);
              }
        }
    }

    function getCookie(c_name)
    {
      var i,x,y,ARRcookies=document.cookie.split(";");
      for (i=0;i<ARRcookies.length;i++)
      {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==c_name)
        {
          return unescape(y);
        }
      }
    }

    function setCookie(c_name,value,exdays)
    {
      var exdate=new Date();
      exdate.setDate(exdate.getDate() + exdays);
      var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
      document.cookie=c_name + "=" + c_value;
    }

    function checkCookie()
    {
        var loginStatus=getCookie("login");
        //alert(loginStatus);
        if (loginStatus=="yes")
        {
            //alert("Masuk pengecekan") 
            window.location="main.html";
        }
    }
</script>
</head>
<body onload="checkCookie()">
    <h2>LOGIN FORM</h2>
    <BR>
    Username : <input type="text" id="loginUsername"/>
    <BR>
    Password : <input type="password" id="loginPassword"/>
    <BR>
    <input type="button" value="Login" onclick="loginProcess()"/>
</body>
</html>

when i access my client from webContent url (http://localhost/TestWSProject/index.html) that service works perfectly, but if i access my client from stand alone HTML file (file:///D:/+Prog/webservice/TestWSProject/WebContent/index.html) it give me xmlHTTPStatus = 0 and that service is not works.. any solution for this problem?? really thanks..

Michael Frans
  • 613
  • 3
  • 23
  • 49

1 Answers1

0

Some browsers have security restrictions which restrict files from performing certain actions if they are being accessed directly from the file system.

This could be what is causing the error.

DaveE
  • 1,643
  • 2
  • 16
  • 29
  • yes.. i agree with you.. but in my case, i need to call some web service from other stand alone file.. any idea?? thanks.. – Michael Frans Jan 10 '12 at 02:07
  • There is some interesting posts about this topic on this question (http://stackoverflow.com/questions/4208530/xmlhttprequest-origin-null-is-not-allowed-access-control-access-allow-for-file ). This might point you in the right direction. – DaveE Jan 10 '12 at 02:11
  • Thanks for your comment daveE.. that links is inly works when i use google chrome, but in this case, i use an mobile device browser to retreive the web service.. any other suggestion?? really stuck here.. T.T – Michael Frans Jan 10 '12 at 02:26