• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.IllegalStateException: getWriter() has already been called for this response

 
Mounika Alla
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn`t understand why i got java.lang.IllegalStateException when i didn`t called getWriter() method anywhere in my program... Please help me

code:
PrintWriter pw=response.getWriter();
response.setContentType("text/html");
Connection con=null;
int id=Integer.parseInt(request.getParameter("id"));
int length=0;
String filepath = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/fileupload", "root", "root");
PreparedStatement ps=con.prepareStatement("select file from filetable where id="+id);
ResultSet rs=(ResultSet) ps.executeQuery();
System.out.println("resultset: "+rs);
while(rs.next())
{
filepath=rs.getString(1);
}
File f=new File(filepath);
OutputStream os=response.getOutputStream();
ServletContext context=getServletConfig().getServletContext();
String mimeType=context.getMimeType(filepath);
response.setContentType((mimeType!=null)?mimeType:"application/octet-stream");
response.setContentLength((int) f.length());
response.setHeader("Content-Disposition", "attachment;filename="+filepath);
byte[] buf=new byte[1024];
DataInputStream dis=new DataInputStream(new FileInputStream(f));
if((dis!=null)&&((length=dis.read(buf))!=-1))
{
os.write(buf, 0, length);
System.out.println("download successful");
}
dis.close();
os.flush();
os.close();

}
catch(Exception e)
{
e.printStackTrace();
}


Error i got:

resultset: com.mysql.jdbc.ResultSet@19981aa
java.lang.IllegalStateException: getWriter() has already been called for this response
at org.apache.catalina.connector.Response.getOutputStream(Response.java:580)
at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:183)
at com.cis.FileDownldServlet.doPost(FileDownldServlet.java:69)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
 
Krishna Kanth
Ranch Hand
Posts: 49
1
Chrome Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch :-).

please use code tags while posting code.
You should really move data logic off Servlet probably to DataAccessLayer.

I see that you called response.getWriter(); in first line of code you posted above. You can not use both writer, outputstream together. you can call same method multiple times though.

http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletResponse.html
 
Mounika Alla
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Krishna Kanth..I just overlooked it...

Now its working...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!