how to send a string to a servlet from javascript using xmlhttprequest(如何使用 xmlhttprequest 从 javascript 将字符串发送到 servlet)
问题描述
客户端代码:
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
中得到一个空值,在第二个中得到一个空白字符串.请有人帮忙.
Problem here is i am getting a null value in first System.out.println
and a blank string in second one. Please somebody help.
推荐答案
客户端代码:
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
在 url 中添加参数
append your parameter in url
在服务器端
服务器代码:
String output = request.getParameter("stringParameter");
System.out.println(output);
使用stringParameter
名称访问参数
这篇关于如何使用 xmlhttprequest 从 javascript 将字符串发送到 servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 xmlhttprequest 从 javascript 将字符串发送到 servlet


基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01