有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在servlet中获取数据?

我使用servlet和tomcat 6.0创建了一个Web服务。我创建了示例java应用程序来调用web服务,并使其工作正常。我需要在调用web服务时发送一些数据。我在java应用程序中创建了如下内容

StringEntity zStringEntityL = new StringEntity(zAPIInputStringP.toString());
zStringEntityL.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
        "application/json"));
HttpParams aHttpParamsL = new BasicHttpParams();
HttpProtocolParams.setVersion(aHttpParamsL, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(aHttpParamsL, HTTP.UTF_8);

SchemeRegistry aSchemeRegistryL = new SchemeRegistry();
aSchemeRegistryL.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

ClientConnectionManager ccm = new ThreadSafeClientConnManager(aHttpParamsL, aSchemeRegistryL);
DefaultHttpClient client = new DefaultHttpClient(ccm, aHttpParamsL);
HttpPost aHttpPostL = new HttpPost(URL + zAPIName);
aHttpPostL.setHeader("Authorization", "Basic");



aHttpPostL.setEntity(zStringEntityL);
HttpResponse aHttpResponseL;
aHttpResponseL = client.execute(aHttpPostL); 

这里的“zAPIInputStringP”是我的JSON格式数据
在webservice中,我需要如何获取这些数据?我在eclispe中检查了调试模式,我找不到它

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
    //How to get data?
}

请帮帮我


共 (1) 个答案

  1. # 1 楼答案

    当您通过post方法将数据发送到servlet时,可以通过输入流获取数据。以下是您的post方法的外观

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    String zAPIInputStringP = "";
    
    BufferedReader in = new BufferedReader(new InputStreamReader(
                    request.getInputStream()));
    String line = in.readLine();
    while (line != null) {
        zAPIInputStringP += line;
        line = in.readLine();
    }
    
    
    }
    

    JSON字符串包含在zAPIInputStringP