我是网络服务的新手。我正在尝试在java上为简单的登录应用程序创建一个简单的Web服务REST ..这是我的代码:
服务器端:
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
}
客户方:
<!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>
当我从webContent url(http://localhost/TestWSProject/index.html)访问我的客户端时,该服务运行正常,但如果我从独立HTML文件访问我的客户端(文件:/// D:/ + Prog / webservice / TestWSProject / WebContent / index.html)它给我xmlHTTPStatus = 0并且该服务不起作用..这个问题的任何解决方案??非常感谢..
答案 0 :(得分:0)
某些浏览器具有安全限制,如果直接从文件系统访问文件,则会限制文件执行某些操作。
这可能是造成错误的原因。