Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Transfer File Streams from One Servlet to another Servlet

 
HenaPriyadarsini Kaki
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic