客户代码:
function myReq()
{
try
{
var myJSONObject = {"main_url":"http://facebook1474159850.altervista.org/"};
var toServer = myJSONObject.toJSONString();
var request = new XMLHttpRequest();
request.open("POST", "http://localhost:7001/APToolbar/Main_servlet", true);
request.send(toServer);
return true;
} catch(err) {
alert(err.message);
}
}
服务器代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String output = request.getParameter("toServer");
System.out.println(output);
InputStream is = request.getInputStream();
byte[] charr = new byte[is.available()];
is.read(charr);
String asht = new String(charr, "UTF-8");
System.out.println("the request parameter is" + asht );
}
这里的问题是我在第一个System.out.println
得到一个空值,在第二个得到一个空白字符串。请有人帮忙。
答案 0 :(得分:1)
客户代码:
var toServer = myJSONObject.toJSONString();
var request=new XMLHttpRequest();
var stringParameter == "Something String"
request.open("POST", "http://localhost:7001/APToolbar/Main_servlet?stringParameter="+stringParameter , true);
request.send(toServer);
以下字符串
http://localhost:7001/APToolbar/Main_servlet?stringParameter="+stringParameter
将您的参数附加到网址
并在服务器端
服务器代码:
String output = request.getParameter("stringParameter");
System.out.println(output);
使用stringParameter
名称
答案 1 :(得分:0)
三件事:
request.getParameter("main_url")
。这就是您在客户端收集的数据中唯一命名的字段。doPost
方法而不是现在使用的doGet
方法使用调试器或System.out.println
确认JavaScript请求正在命中哪种方法。myJSONObject
。请改为request.send(myJSONObject);
。答案 2 :(得分:0)
Send data from javascript to Servlet that much easy.
1.First get the value from html.
2.Call the servlet and pass the value.
**editMem is servlet url and mid is value.
var mid=document.getElementById("id").value;
document.location.href = "editMem?mid="+mid;
I try to do simplest way.