我正在使用XMLHttpRequest将js文件中的变量发送到同一项目中的java文件。
我的问题和我的问题是:我如何知道我的网址? 这是我的js文件代码
xhttp = new XMLHttpRequest();
var handlerFunction = getReadyStateHandler(xhttp, getValue);
xhttp.onreadystatechange = handlerFunction;
xhttp.open("POST",/* Location of my java file */,true);
xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhttp.send(identMsg);
function getValue (body) {
var valueBody = body.getElementByTagName("body")[0];
}
function getReadyStateHandler(xhttp, responseXmlHandler) {
return function(){
if (xhttp.readyState == 4) {
if(xhttp.status == 200) {
responseXmlHandler(xhttp.responseXML);
} else {alert("Http error: " +xhttp.status);}
}
}
}
和java代码
public void doPost (HttpServletRequest xhttp, HttpServletResponse res) throws IOException {
String body = null;
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
try {
InputStream inputStream = xhttp.getInputStream();
if (inputStream != null) {
bufferedReader = xhttp.getReader();
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer,0, bytesRead);
}
} else {
stringBuilder.append("");
}
} catch(IOException ex) {
throw ex;
} finally {
if(bufferedReader != null) {
try {
bufferedReader.close();
}catch (IOException ex2){
throw ex2;
}
}
}
body = stringBuilder.toString();
res.setContentType("application/xml");
res.getWriter().write(body);
}
缺少什么?
编辑:我需要在js方面获取URL。