Strange Java Servlet Problem

Discussion in 'Mac Programming' started by Cromulent, Jan 20, 2010.

  1. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #1
    I have a very strange problem with a Java servlet. Here is the code:

    Code:
    	protected void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException
    	{
    		processRequest(request, response);
    		PrintWriter out = response.getWriter();
    		out.println(response.getHeader("line_one"));
    		out.println(response.getHeader("line_two"));
    		out.println(response.getHeader("line_three"));
    		out.println(response.getHeader("line_four"));
    		out.close();
    	}
    
    	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException
    	{
    		response.setContentType("text/html;charset=UTF-8");
    		
    		line_number = Integer.parseInt(request.getHeader("line_number"));
    		
    		file_reader.setLineNumber(line_number);
    		response.addHeader("line_one", file_reader.readLine());
    		
    		file_reader.setLineNumber(line_number + 1);
    		response.addHeader("line_two", file_reader.readLine());
    		
    		file_reader.setLineNumber(line_number + 700);
    		response.addHeader("line_three", file_reader.readLine());
    		
    		file_reader.setLineNumber(line_number + 701);
    		response.addHeader("line_four", file_reader.readLine());
    	}
    
    Basically it takes an integer sent from the iPhone via NSURLConnection in an NSURLRequest and returns 4 lines from a text file. The lines should be the line number of the integer received, integer + 1, integer + 700 and integer + 701. Yet when I send the request the servlet sends lines 1, 2, 3 and 4 back. If I stop the iPhone app and restart it the servlet then sends lines 5, 6, 7 and 8 back. Yet I was sure that the setLineNumber() method was meant to explicitly set the line number in the file input before reading it.

    I have hard coded the number 1 into the iPhone app for testing so that is not a problem and checked that the servlet is receiving the correct number on the server side. Does anyone have any idea what could be happening?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  3. Cromulent thread starter macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #3
    A LineNumberReader.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    From the documentation "Note however, that setLineNumber(int) does not actually change the current position in the stream; it only changes the value that will be returned by getLineNumber()".

    This class (which I must admit I've never looked at before) just tracks the line numbers to stop you doing so yourself. If you want to skip lines you still have to sequentially read each one till you get to the one you want...
     
  5. Cromulent thread starter macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #5
    /facepalm

    Well I'll be...

    This is the problem with self documenting code. You get in the habit of reading the code rather than the documentation. Thanks for pointing that out to me I completely missed that.

    I was originally going to write the line number reader myself but was quite impressed when I saw (what I thought) was a standard class to do it.
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    If it had been me writing the java docs for that class I'd have put that note with the method as well as at the top of the class. I imagine you are not the first person to skip right to the method and then wonder why it's not doing what you expect.
     

Share This Page