You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Lind Jürgen <Ju...@iteratec.de> on 2001/01/10 17:41:06 UTC

How to return file content?

Hi there,

since this is my first posting to this list I should first of all make
clear that I think that struts is a really valuable tool that can save you
lots of time in developing a web application. I really love it!

Now the problem I am currently facing is the following: I have a form page
that contains a button which allows the user to download a pdf version of
the data currently visible on the page. After clicking this button, a
backgrounbd process for the pdf generation is started and I would like
to return the output file generated by the process to the user while the
page in the web browser remains the same as before.

Here is a code snipplet that shows what I have tried...

//==============================================================
  public final class ViewAction extends Action{

    public ActionForward perform(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response)
      throws IOException, ServletException {

    /* missing code here */
		:
		:
		:    
    if( cmd.equals( "pdf" ) ){
      HttpSession session = request.getSession();
      pdfGenerator g = new pdfGenerator( (RechnungBean)session.getAttribute(
Constants.RECHNUNG_KEY ));
      g.generate();

      ServletOutputStream out = response.getOutputStream();
      response.setContentType( "application/pdf" );
      try{
        this.returnFile(
"c:\\www\\public_html\\webapps\\odin-tracer\\tmp\\pdfoutput.pdf", out );
      }
      catch( FileNotFoundException e ){
        e.printStackTrace();
      }
    }

    return new ActionForward( mapping.getInput() );
  }
//==============================================================

The key problem seem to be that I must tell the struts framework not to
re-send the current input form page... Any ideas on how to solve this
problem?

Best regards,

Jürgen

--
Dr. Jürgen Lind
iteratec GmbH                Fon: +49 (0)89 614551-44
Inselkammerstrasse 4         Fax: +49 (0)89 614551-10
82008 Unterhaching           Web: www.iteratec.de

Re: How to return file content?

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Hello Jürgen,

I'm glad you like Struts.  And I think you are going to like the nice simple
answer to this question as well.

Normally, an Action returns an ActionForward that says where to forward control
to.  However, if you return null instead, you are saying "I have already created
the response, so no forward is necessary."  That will be the case in your PDF
generation scenario.

Craig


Lind Jürgen wrote:

> Hi there,
>
> since this is my first posting to this list I should first of all make
> clear that I think that struts is a really valuable tool that can save you
> lots of time in developing a web application. I really love it!
>
> Now the problem I am currently facing is the following: I have a form page
> that contains a button which allows the user to download a pdf version of
> the data currently visible on the page. After clicking this button, a
> backgrounbd process for the pdf generation is started and I would like
> to return the output file generated by the process to the user while the
> page in the web browser remains the same as before.
>
> Here is a code snipplet that shows what I have tried...
>
> //==============================================================
>   public final class ViewAction extends Action{
>
>     public ActionForward perform(ActionMapping mapping,
>                                ActionForm form,
>                                HttpServletRequest request,
>                                HttpServletResponse response)
>       throws IOException, ServletException {
>
>     /* missing code here */
>                 :
>                 :
>                 :
>     if( cmd.equals( "pdf" ) ){
>       HttpSession session = request.getSession();
>       pdfGenerator g = new pdfGenerator( (RechnungBean)session.getAttribute(
> Constants.RECHNUNG_KEY ));
>       g.generate();
>
>       ServletOutputStream out = response.getOutputStream();
>       response.setContentType( "application/pdf" );
>       try{
>         this.returnFile(
> "c:\\www\\public_html\\webapps\\odin-tracer\\tmp\\pdfoutput.pdf", out );
>       }
>       catch( FileNotFoundException e ){
>         e.printStackTrace();
>       }
>     }
>
>     return new ActionForward( mapping.getInput() );
>   }
> //==============================================================
>
> The key problem seem to be that I must tell the struts framework not to
> re-send the current input form page... Any ideas on how to solve this
> problem?
>
> Best regards,
>
> Jürgen
>
> --
> Dr. Jürgen Lind
> iteratec GmbH                Fon: +49 (0)89 614551-44
> Inselkammerstrasse 4         Fax: +49 (0)89 614551-10
> 82008 Unterhaching           Web: www.iteratec.de