Servlets Servlet-读取post值的inputstream将返回null

Servlets Servlet-读取post值的inputstream将返回null,servlets,post,Servlets,Post,我有一个servlet,如下所示 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream())); try { String requestLine

我有一个servlet,如下所示

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
try {
    String requestLine = br.readLine(); // getting null
    ....
HttpURLConnection httpConnection = null;
PrintWriter pwr = null;
ObjectInputStream ois = null;
try
{
String serverURL = "http://localhost:8080/app/servlet1?id=2";

httpConnection = (HttpURLConnection)(new URL(serverURL).openConnection()); //opening the connection
httpConnection.setRequestMethod("POST");
httpConnection.setDoInput(true);
httpConnection.setDoOutput(true);
httpConnection.setUseCaches(false);

pwr = new PrintWriter(httpConnection.getOutputStream());            
pwr.println("inputValString");              
pwr.flush();        

//getting the streams           
ois = new ObjectInputStream(httpConnection.getInputStream());               
...     
我从客户端调用servlet,如下所示

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
try {
    String requestLine = br.readLine(); // getting null
    ....
HttpURLConnection httpConnection = null;
PrintWriter pwr = null;
ObjectInputStream ois = null;
try
{
String serverURL = "http://localhost:8080/app/servlet1?id=2";

httpConnection = (HttpURLConnection)(new URL(serverURL).openConnection()); //opening the connection
httpConnection.setRequestMethod("POST");
httpConnection.setDoInput(true);
httpConnection.setDoOutput(true);
httpConnection.setUseCaches(false);

pwr = new PrintWriter(httpConnection.getOutputStream());            
pwr.println("inputValString");              
pwr.flush();        

//getting the streams           
ois = new ObjectInputStream(httpConnection.getInputStream());               
...     

但是,当我运行客户机代码时,我总是在servlet中将
requestLine
设置为null。

这通常意味着请求主体已经被预先使用了。也许您事先在同一个servlet中调用了
request.getParameter()
,或者在该servlet之前运行的过滤器?不,相同的代码在其他系统中工作。客户端代码由4-5个线程调用,所有线程逐个失败。不知道如何调试它。如果您对这个“否”很有信心,那么servlet就不是线程安全的。我注释掉了所有过滤器映射,但仍然得到null。我可以把断点放在哪里,这样我就可以提前跟踪任何消耗。是否可以从服务器获取请求的那一刻开始跟踪到servlet调用。任何访问请求体的
HttpServletRequest
getter都将使用它
getParameterXxx()
getReader()
getInputStream()