Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用XMLHttpRequest.send(字符串)中的信息_Java_Javascript_Ajax - Fatal编程技术网

Java 如何使用XMLHttpRequest.send(字符串)中的信息

Java 如何使用XMLHttpRequest.send(字符串)中的信息,java,javascript,ajax,Java,Javascript,Ajax,我正在用.send()部分中的字符串发送POST请求,该部分是“id=jeff&command=test” 如何通过HttpServletRequest对象在Java中解析和使用这些值 我已尝试将内容类型更改为text/html和application/x-www-form-urlencoded。 我的java服务器是嵌入式jetty btw var text = form.command.value; var xmlhttp = new XMLHttpRequest(); xmlht

我正在用
.send()
部分中的字符串发送POST请求,该部分是
“id=jeff&command=test”

如何通过
HttpServletRequest
对象在Java中解析和使用这些值

我已尝试将内容类型更改为
text/html
application/x-www-form-urlencoded
。 我的java服务器是嵌入式jetty btw

var text = form.command.value;
var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange=function()
    {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
            xmlhttp.responseText
          }
        }

    xmlhttp.open("POST", 'go', true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("id=jeff&command=" + text);
这是我的Java代码

public class GoHandler extends HttpServlet 
{
Commands c = Commands.getInstance();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
{
    BufferedReader reader = request.getReader();
    String str = "";
    while ((str = reader.readLine()) != null)
    {
        System.out.println(str);
    }

    String p = request.getParameter("id");
    String input = request.getParameter("command"); 
    System.out.print(p);
这是我从浏览器发出请求时的输出

id=jeff&command=test(来自缓冲读取器)

null(这是我的字符串p,它应该是id)

这是Chrome的工具箱

Request URL:http://localhost:8080/go
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:20
Content-Type:application/xml
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko)     Chrome/22.0.1229.79 Safari/537.4
Request Payload
id=jeff&command=test
Response Headersview source
Content-Length:66
Content-Type:application/json;charset=utf-8
Server:Jetty(8.1.7.v20120910)
我的服务器响应

{"flavor":"You are in the goHandler"}

您可以将其作为缓冲读取器获取。这将给出完整的字符串。您可以手动解析它。如果分隔符为“\n”,则

编辑:

你能试试这两个http头中的一个吗?!这是您的更新代码

var text = form.command.value;
var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange=function()
{
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
        alert(xmlhttp.responseText);
      }
    }
var params = "id=jeff&command=" + encodeURIComponent(text);
xmlhttp.open("POST", "go", true);
xmlhttp.send(params);
使用encodeURIComponent()将URI转换为有效的ASCII


更新了JavsScript,因为默认内容类型为“application/x-www-form-urlencoded”,所以将其删除。现在尝试访问servlet中的所有参数。

“这是可行的。但是没有内置的方法来获取这些值吗?”

不要通过读取输入流来获取参数,因为一旦您读取了输入流,您在输入流末尾到达,并且本机方法
getParameter()
不会重置输入流,这就是它返回null的原因。 直接使用
getParameter()
获取值

使用
InputStream
getParameter()
不要同时使用

@Override
protected void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException
{`    `

String p=request.getParameter("id");
String input=request.getParameter("command");
System.out.println(p);

显示您的Ajax调用,以便我们确保请求设置正确。在服务器端,什么是
httpServletRequest.getParameter(“id”)
return?String p=request.getParameter(“id”);系统输出打印(p);这就是为什么不使用jQuery之类的标准JavaScript库?!:)这些问题在原生JS中很常见,框架提供了简单的方法来解决这些问题,同时也解决了跨浏览器问题。比如说,你上面的JS代码在旧的IE浏览器中不起作用(bcoz XMLHttpRequest不存在)。我的问题不在于javascript,而在于服务器以其应有的方式获取属性(使用getPeoperty(string))post请求最终将从android代码发送,因此javascript和浏览器不会成为问题。我只是用它来测试服务器请求。这很有效。但是没有内置的方法来获取这些值吗?我在请求中添加了代码。但是我的输出没有改变。Chrome的调试工具现在说它是表单数据,但是在我的程序中,我的字符串仍然是空的,这与上面的答案一致。请查收。
@Override
protected void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException
{`    `

String p=request.getParameter("id");
String input=request.getParameter("command");
System.out.println(p);