my dog learned polymorphism
The moose likes Servlets and the fly likes probelms with response.getOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "probelms with response.getOutputStream" Watch "probelms with response.getOutputStream" New topic
Author

probelms with response.getOutputStream

nandkishor rao
Ranch Hand

Joined: May 24, 2006
Posts: 53
Hi,
My servlet is reading a zip file and writing it to reponse
using ServletOutputStream. But while the Servlet gets triggered its giving me this exception and the funny thing is i am not calling getWriter() on response anywhere. If any of you having any suggestions please reply.

java.lang.IllegalStateException: strict servlet API: cannot call getOutputStream() after getWriter()at weblogic.servlet.internal.ServletResponseImpl.getOutputStream(ServletResponseImpl.java:253)
at com.bt.oss.trengtd.AfmServlet.doGet(AfmServlet.java:64)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:225)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:127)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:272)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:165)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3153)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:1973)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1880)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1310)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:207)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:179)
John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230
It would be helpful to post your servlet code.
saqib sarwar
Ranch Hand

Joined: Mar 30, 2007
Posts: 77
I suggest check few things.

1. setContentType for response before writing.
2. better collect whole file in a byte array and then write it at once to response.
3. check your code that it should not call getWriter and getOutputStream methods both in same method.


SCJP5 95%, SCWCD 85%<br />Knowledge is the Life of Mind
nandkishor rao
Ranch Hand

Joined: May 24, 2006
Posts: 53
Below is the Servlet code please let me know if there are any suggestions

public class AfmServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 7467039855104622536L;
// private static final Log log = LogFactory.getLog(AfmServlet.class);
/**
* @param request The request recieved from the client
* @param response The response to the client
* @throws ServletException
* @throws IOException
*/

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html");
//Get the request parameters
String nni = request.getParameter("nni");
String msg = nni+" nni received";
response.sendError(HttpServletResponse.SC_OK, msg);
response.setHeader("nni", "0123456");
response.setHeader("resultCount","5");
// log.debug("Response Headers set");
// File afm_0123456 = null;
ServletOutputStream out = null;
try {
// File f = new File("C:/Documents and Settings/148034/Desktop/new.doc");

String baseName = "afm_0123456.zip";

String fileLocation = findFile(baseName);
File f = new File(fileLocation);
FileInputStream istr = new FileInputStream(fileLocation);
GZIPInputStream ip = new GZIPInputStream(istr);
// log.debug("FileInputStream");
BufferedInputStream bstr = new BufferedInputStream( ip ); // promote
// log.debug("BufferedInputStream");
int size = (int) f.length(); // get the file size (in bytes)
byte[] data = new byte[size]; // allocate byte array of right size
bstr.read( data, 0, size ); // read into byte array
// log.debug("DATA READ");
bstr.close();
// log.debug("BufferedInputStream CLOSED");
response.setContentType("text/html");
//
out = response.getOutputStream();
// log.debug("getOutputStream");
out.write(data);
// log.debug("WRITTEN DATA");
out.flush();
// log.debug("FLUSHED DATA");
out.close();
// afm_0123456 = new File( "afm_0123456.zip" );
// if (afm_0123456.exists()) {
// response.setContentType("text/html");
// RandomAccessFile raf = new RandomAccessFile( afm_0123456, "r" );
// response.setContentLength((int)raf.length());
// out = response.getOutputStream();
// byte [] loader = new byte [ (int) raf.length() ];
// while ( (raf.read( loader )) > 0 ) {
// out.write( loader );
// }
// }else {
// System.out.println("Aborting: File doesn't exists, " + afm_0123456 + ", " + afm_0123456 + File.separator + afm_0123456 +
// "abspath: " + afm_0123456.getAbsolutePath() );
// }
// log.debug("GOING TO DISPATCH IT TO SERVLET");
RequestDispatcher requestdispatcher = request.getRequestDispatcher("/RcuAfmCallbackServlet");
requestdispatcher.forward(request,response);
} catch (IOException ioe) {
// log.debug("Unable to open file "); ioe.printStackTrace();
} finally {
if (out != null) {
out.flush();
out.close();
}
}

}

/**
* @param request The request recieved from the client
* @param response The response to the client
* @throws ServletException
* @throws IOException
*/

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//Call the doGet() method
doGet(request, response);
}
private String findFile(String baseName) {
String absoluteFilename = Utilities.getFileAsPathOrUrl(baseName);
if (absoluteFilename.equals(baseName)) {
absoluteFilename = "C:/btp/Transfer_Engineering_Diagnostics/Construction/Java/project-web/WEB-INF/src/com/bt/oss/trengtd/" + baseName;
}
return absoluteFilename;
}
}
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2300
As we cannot call getParameter and getInputStream in the same program , this might be because of the same thing .We cannot call getWriter and getOutputstream in the same program.


Rahul Bhattacharjee
LinkedIn - Blog
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: probelms with response.getOutputStream
 
Similar Threads
java.net.SocketException: Broken pipe
Does anyone know what this error means?
java.lang.IllegalStateException: Response already committed
java.lang.IllegalStateException: Response already committed
Limit on Data/Key-Value Pairs in Form Submission