Servlet 接收空字符串

Posted

技术标签:

【中文标题】Servlet 接收空字符串【英文标题】:Servlet receives empty strings 【发布时间】:2013-09-02 02:34:05 【问题描述】:

我遇到了一个非常奇怪的错误。

基本上它是关于我从 android 应用程序(如“421”)向部署在 GlassFish 服务器上的 Java Servlet 发送格式化为字符串的数字,但是当我调试时,我发现 servlet 接收到一个包含 3 个空字符的字符串,基本上它会收到一个字符串,其中包含的空字符数是 android 中发送字符串的长度。

服务器端

package Applications;

import Applications.AdminConsole;
import java.io.IOException;
import java.io.OutputStreamWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="/RQSRV", urlPatterns="/RQSRV")
public class RQSRV extends HttpServlet 
    private static final long serialVersionUID = 1L;


public RQSRV() 
    super();



protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 

    response.getOutputStream().println("Hurray !! This Servlet Works");



protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 

    response.getOutputStream().println("Servlet Accesed");
    try 
        int length = request.getContentLength();
        byte[] input = new byte[length];
        ServletInputStream sin = request.getInputStream();
        int c, count = 0 ;
        while ((c = sin.read(input, count, input.length-count)) != -1) 
            count +=c;
        
        sin.close();

        String receivedString = new String(input);
        response.setStatus(HttpServletResponse.SC_OK);
        OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());

        Integer doubledValue = Integer.parseInt(receivedString) * 2;

        writer.write(doubledValue.toString());
        writer.flush();
        writer.close();
        AdminConsole cs=new AdminConsole();
        cs.setEnabled(true);
        cs.setVisible(true);


     catch (IOException e) 


        try
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().print(e.getMessage());
            response.getWriter().close();
         catch (IOException ioe) 
        
      
    


安卓端

    package com.app.myapp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class DoubleMeActivity extends Activity implements OnClickListener 

    EditText inputValue=null;
    Integer doubledValue =0;
    Button doubleMe;

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculate);

        inputValue = (EditText) findViewById(R.id.inputNum);
        doubleMe = (Button) findViewById(R.id.doubleme);

        doubleMe.setOnClickListener(this);
    

    @Override
    public void onClick(View v) 

        switch (v.getId())
        case R.id.doubleme:

              new Thread(new Runnable() 
                    public void run() 

                        try
                            URL url = new URL("http://192.168.1.104:8080/Requestor/RQSRV");
                            URLConnection connection = url.openConnection();

                            String inputString = inputValue.getText().toString();
                            inputString = URLEncoder.encode(inputString, "UTF-8");

                            Log.d("inputString", inputString);

                            connection.setDoOutput(true);
                            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
                            out.write(inputString);
                            out.close();

                            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

                            String returnString="";
                            doubledValue =0;

                            while ((returnString = in.readLine()) != null)
                            
                                doubledValue= Integer.parseInt(returnString);
                            
                            in.close();


                            runOnUiThread(new Runnable() 
                                 public void run() 

                                     inputValue.setText(doubledValue.toString());

                                
                            );

                            catch(Exception e)
                            
                                Log.d("Exception",e.toString());
                            

                    
                  ).start();

            break;
            
        

    

希望你能帮忙

【问题讨论】:

所以请求正文包含三个空格字符? 我看到你定义了 byte[] input = new byte[length];但是你在哪里设置字节??? 是的,和发送的字符串长度一样 @StefanBeike inside this 'while ((c = sin.read(input, count, input.length-count)) != -1)' 我把阅读部分编辑成这个 sin.read(input, 0, length);但结果相同 【参考方案1】:

使用嗅探器,例如:wireshark,抓取网络包,查看发送的真实数据,是一种真正有效的查找根本原因的方法。

【讨论】:

以上是关于Servlet 接收空字符串的主要内容,如果未能解决你的问题,请参考以下文章

如何在不接收空字符串的情况下获取主板 ID?

利用冒泡排序实现一串字符串从小到大的排序

python怎么input输出空字符串

shell 如何判断是不是为一串连续字符

spring3.0 怎么把json里面的null值替换成空字符串

递归方式判断某个字串是否是回文