You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Yasir <ya...@netlinksoft.net> on 2002/12/30 14:40:24 UTC

Please reply--Important print writer problem.

Hi Friends,
I am receiving the sam eproblem which you have put on the site.I am following MVC architecture and working with IBM WPOrtal server.I have my JSP packaged within the par file(PORTLET ARCHIVE).I have a code in my JSP which writes some zip content to the browser using servletoutput stream The aim is to download the zip file to the client machine my invoking the browsers default download box.This works fine if i have the JSP outside the PAR file but when i have this JSP inside the par it gives the exception.
java.lang.IllegalStateException Writer has already been obtained.
Please let me know the solution ASAP.Its important for me.
pasted below is the piece of code i am using in the jsp

<% 

// Create a object of CreateZip

CreateZip createZip = new CreateZip();


// It stores Report Path.

String reportPath = "C:/www/web-data/fieldconnect/llreports/YASIR.txt";

// Create a object of PrintWriter

PrintWriter printWriter = new PrintWriter(new FileOutputStream(new File(reportPath)));


printWriter.print("WritWriWritWWrWWriting data to text file...Writing data to text file...riting data to text file...Writing data to text file...Writing data to text file...Writing data to text file...iting data to text file...Writing data to text file...riting data to text file...Writing data to text file...ing data to text file...Writing data to text file...ting data to text file...Writing data to text file...ing data to text file...Writing data to text file...Writing data to text file...Writing data to text file...Writing data to text file...Writing data to text file");

printWriter.flush();

printWriter.close();


// It stores path of text file

String path = "C:/www/web-data/fieldconnect/llreports/";

// It stores name of text file

String fileName = "YASIR.txt";


// making zip of text file generated 

createZip.makeZip(fileName,path);

String zipPath = "C:/www/web-data/fieldconnect/llreports/YASIR.zip";

File deleteZipFile = new File(zipPath);


int zipsize = (int)deleteZipFile.length();

byte abyte0[] = new byte[zipsize];

ByteArrayOutputStream ba = new ByteArrayOutputStream(zipsize);

File deleteTxtFile = new File(reportPath);

if (deleteTxtFile.exists()){

deleteTxtFile.delete();

}



//PrintWriter zipWriter = new PrintWriter(new FileOutputStream());

FileInputStream fileInputStream = new FileInputStream(zipPath);


BufferedInputStream bufferedinputstream = new BufferedInputStream(fileInputStream);


int i;

while((i = bufferedinputstream.read(abyte0)) != -1){ 

ba.write(abyte0,0,i);

}


bufferedinputstream.close();

HttpServletResponse sResponse = (HttpServletResponse)((PortletResponseImpl)response).getServletResponse();

sResponse.setContentType("application/zip");

sResponse.setContentLength(ba.size());

sResponse.setHeader("Content-Type","application/octet-stream");

sResponse.setHeader("Content-Disposition","attachment; filename=\"YASIR.zip\"");

if (ba != null) {

ServletOutputStream sout = sResponse.getOutputStream();


ba.writeTo(sout);


ba.flush();

ba.close();

sout.close();

}



if (deleteZipFile.exists()){

deleteZipFile.delete();

}

%>


Thanks,
Yasir.

Re: Please reply--Important print writer problem.

Posted by Bill Barker <wb...@wilshire.com>.
You should not ever attempt to use an OutputStream from a JSP page.  The
entire point of JSP pages is to output text!  JSP pages will always grab the
Writer (per the spec), before your code has a chance to execute.

I'd suggest converting your JSP to a Servlet (not hard, based on the code
below).  You should then be able to use the OutputStream happily.

"Yasir" <ya...@netlinksoft.net> wrote in message
news:02ae01c2b009$144cd8d0$09016b83@NSG1.local...
Hi Friends,
I am receiving the sam eproblem which you have put on the site.I am
following MVC architecture and working with IBM WPOrtal server.I have my JSP
packaged within the par file(PORTLET ARCHIVE).I have a code in my JSP which
writes some zip content to the browser using servletoutput stream The aim is
to download the zip file to the client machine my invoking the browsers
default download box.This works fine if i have the JSP outside the PAR file
but when i have this JSP inside the par it gives the exception.
java.lang.IllegalStateException Writer has already been obtained.
Please let me know the solution ASAP.Its important for me.
pasted below is the piece of code i am using in the jsp

<%

// Create a object of CreateZip

CreateZip createZip = new CreateZip();


// It stores Report Path.

String reportPath = "C:/www/web-data/fieldconnect/llreports/YASIR.txt";

// Create a object of PrintWriter

PrintWriter printWriter = new PrintWriter(new FileOutputStream(new
File(reportPath)));


printWriter.print("WritWriWritWWrWWriting data to text file...Writing data
to text file...riting data to text file...Writing data to text
file...Writing data to text file...Writing data to text file...iting data to
text file...Writing data to text file...riting data to text file...Writing
data to text file...ing data to text file...Writing data to text file...ting
data to text file...Writing data to text file...ing data to text
file...Writing data to text file...Writing data to text file...Writing data
to text file...Writing data to text file...Writing data to text file");

printWriter.flush();

printWriter.close();


// It stores path of text file

String path = "C:/www/web-data/fieldconnect/llreports/";

// It stores name of text file

String fileName = "YASIR.txt";


// making zip of text file generated

createZip.makeZip(fileName,path);

String zipPath = "C:/www/web-data/fieldconnect/llreports/YASIR.zip";

File deleteZipFile = new File(zipPath);


int zipsize = (int)deleteZipFile.length();

byte abyte0[] = new byte[zipsize];

ByteArrayOutputStream ba = new ByteArrayOutputStream(zipsize);

File deleteTxtFile = new File(reportPath);

if (deleteTxtFile.exists()){

deleteTxtFile.delete();

}



//PrintWriter zipWriter = new PrintWriter(new FileOutputStream());

FileInputStream fileInputStream = new FileInputStream(zipPath);


BufferedInputStream bufferedinputstream = new
BufferedInputStream(fileInputStream);


int i;

while((i = bufferedinputstream.read(abyte0)) != -1){

ba.write(abyte0,0,i);

}


bufferedinputstream.close();

HttpServletResponse sResponse =
(HttpServletResponse)((PortletResponseImpl)response).getServletResponse();

sResponse.setContentType("application/zip");

sResponse.setContentLength(ba.size());

sResponse.setHeader("Content-Type","application/octet-stream");

sResponse.setHeader("Content-Disposition","attachment;
filename=\"YASIR.zip\"");

if (ba != null) {

ServletOutputStream sout = sResponse.getOutputStream();


ba.writeTo(sout);


ba.flush();

ba.close();

sout.close();

}



if (deleteZipFile.exists()){

deleteZipFile.delete();

}

%>


Thanks,
Yasir.






--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>