wood burning stoves 2.0*
The moose likes Servlets and the fly likes How to Transfer File Streams from One Servlet to another Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to Transfer File Streams from One Servlet to another Servlet" Watch "How to Transfer File Streams from One Servlet to another Servlet" New topic
Author

How to Transfer File Streams from One Servlet to another Servlet

HenaPriyadarsini Kaki
Greenhorn

Joined: Aug 30, 2008
Posts: 2
Hi,

I have a problem with transfering file Streams from one Servlet to another
servlet. Is there any Solution for this???

Using TomcatServer.

In my code I have 2 servlets.

In my firstServlet Im writing the FileInputstream into response object and forwarding it.

In my SecondServlet Im writing the Inputstream into FileOutputStream object.

but Iam unable to forward the request from first servlet using RequestDispatcher's forward(req,res) method (getting exception as response is already commited before forwarding)


public class UploadServlet extends HttpServlet

{

public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
System.out.println("in UploadServlet");
try
{

FileInputStream fis=new FileInputStream("C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/sample1.txt");
int i=0;
OutputStream os=response.getOutputStream();

while(i!=-1)
{
i=fis.read();
os.write (i);

}
os.flush();
RequestDispatcher rd;
rd=request.getRequestDispatcher("/FileUpload1");
rd.forward(request, response);

}
catch(Exception ee)
{
System.out.println("error in UploadServlet :"+ee.getMessage());
}
}

}


public class UploadServlet1 extends HttpServlet

{

public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{

try
{

FileOutputStream fos=new FileOutputStream("C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/uploads/sample1.txt");

ServletInputStream instream = request.getInputStream();
int i=0;
System.out.println("in UploadServlet1");
while(i!=-1)
{
i=instream.read();
System.out.println("i is"+i);

fos.write(i);
}




}
catch(Exception ee)
{
System.out.println("error in UploadServlet1:"+ee.getMessage());
}
}
}
--Plese Help me-------



Thanks in Advance...........
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
I think you need to back off a bit and re-design your architecture since the present one has let you into a mess.

Bill
Bupjae Lee
Ranch Hand

Joined: May 14, 2007
Posts: 107
Maybe you think that the response object of UploadServlet becomes the request object of UploadServlet1 when you use RequestDispatcher, but it is wrong.

When you use RequestDispatcher, both servlet share same request and response object, and you can't forward after you already flushed response.

Try using request attribute for passing stream object.
(Note that this may not work if you're using server clustering.)
[ August 30, 2008: Message edited by: Bupjae Lee ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Transfer File Streams from One Servlet to another Servlet