Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Servlets and the fly likes readLine() method return -1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "readLine() method return -1" Watch "readLine() method return -1" New topic

readLine() method return -1

Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
Hi Everybody,

I am submitting a form in jsp file to a servlet :-

Partial jsp file contents are :-

<form name="searchexcel" method="POST" ENCTYPE="multipart/form-data">
<input type=file name="filename" >

partial code in servlet as follows :-

ServletInputStream sis = request.getInputStream();
byte[] b = new byte[1024];
int x=0;
while((x=sis.readLine(b,0,1024))>-1) {


readline method is returning -1 at first attempt itself. It will be a great help if Can any one inform what could be the possible reasons for it.

William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
With servlet response you have a choice - you can either read the body of the request with request.getInputStream() -OR- you can use the response getParameter method to get form parameters. This is because the servlet engine has to read the request body input stream in order to find and parse the form parameters for you. The return of -1 indicates that the InputStream is already at the end of data.
The JavaDocs for ServletRequest.getParameter() discusses this.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
as your form type is multipart/form-data. you will get null, if use request.getParameter(String str).
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
You might be interested in the MultipartRequest and associated classes available in the O'Reilly Servlets package.

I agree. Here's the link:
subject: readLine() method return -1
It's not a secret anymore!