request.getParameter()在DoPost(HttpServlet)中为空

时间:2016-07-02 23:40:07

标签: java servlets httpclient

我正在尝试使用httpClient发送JSON对象,但在DoPost中,“HttpServletRequest请求”为空,它没有参数。

另外,我尝试使用Postman chrome app向同一个类发出请求,并且它还有空的“HttpServletRequest请求”。

这里是如何提出请求:

public void CamposPost(){
    try{
    HttpClient   httpClient    = HttpClientBuilder.create().build();
    HttpPost     post          = new HttpPost(this.url);
    StringEntity postingString = new StringEntity(this.JSONObject.toString());
    post.setHeader("Content-type", "application/json");
    post.setEntity(postingString);

    HttpResponse  response = httpClient.execute(post);
    HttpEntity entity = response.getEntity();
    String responseString = EntityUtils.toString(entity, "UTF-8");
    this.respond=responseString;
    }
    catch(Exception ex){
        ex.printStackTrace();
    }
}

以下代码是DoPost代码

package com.itc.polux.request;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class post extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      List<String> parameterNames = new ArrayList<String>(request.getParameterMap().keySet());
      //request.getParameterNames();
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println(request.getParameter("json"));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

      List<String> parameterNames = new ArrayList<String>(request.getParameterMap().keySet());
      //request.getParameterNames();
      PrintWriter out = response.getWriter();
      response.setContentType("text/html");
      out.println(request.getParameter("json"));
}
}

0 个答案:

没有答案