0

I have a servlet as below.

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
    ....

and I invoke the servlet from my client as below.

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());               
...     

But, when I run the client code I always get requestLine as null in the servlet.

itsraja
  • 1,640
  • 4
  • 32
  • 48
  • It usually means the request body is already consumed beforehand. Perhaps you called `request.getParameter()` beforehand in same servlet or a filter running before the servlet? – BalusC Apr 15 '16 at 12:49
  • No, the same code works in other systems. The client code is invoked by 4-5 threads and all threads fail one by one. No idea how to debug this. – itsraja Apr 16 '16 at 07:35
  • If you're so confident with that "No", then the servlet is not threadsafe. – BalusC Apr 16 '16 at 07:39
  • I commented out all filter-mappings but still I get null. Where can I put breakpoint so that I can trace any consumption beforehand. Is it possible to trace from the moment the server gets request upto the servlet invocation. – itsraja Apr 18 '16 at 08:06
  • Any of `HttpServletRequest` getters which access request body will consume it. `getParameterXxx()`, `getReader()` and `getInputStream()`. – BalusC Apr 18 '16 at 08:10
  • I encounter this issue also( http://stackoverflow.com/questions/2126607/official-reasons-for-software-caused-connection-abort-socket-write-error ) in the application but for different port. Will it have any effect on this issue? But both server and client are in same machine. – itsraja Apr 19 '16 at 12:53
  • With that issue also I'm unable to do anything. :( – itsraja Apr 19 '16 at 13:05

0 Answers0