• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I download a file in a Struts Portlet?

 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In plain Struts it is easy:
response.setContentType("application/pdf");
response.setHeader("Content-disposition","attachment;filename=x.pdf");
java.io.PrintWriter out = arg3.getWriter();
out.print("<output>");
out.print("test raise file dialog example output");
out.print("</output>");
But I don't know how to access the HttpServletResponse object. I tried to cast the PortletResponse object in the execute method, which compiles and runs, but I get exceptions afterwards.
Is there any way to do this in during the render phase of the portlet?
I probably can execute the above code in the JSP, but doing it in the JSP is ugly.

I appreaciate your help,

Alejandro Barrero
 
Alejandro Barrero
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just found out that the code I provided doesn't run in a portlet because portlets only allow mime type text/html (tha's all they display). The solution then is to invoke, from the porlet, a servlet or a JSP that runs outside of the portal. Unfortunately, I have not been able to find a way to do this; In Tomcat, I can run a JSP easily with JavaScript (window.open("MyJsp.jsp"))--It works fine if the jsp is in the default directory. However, this does not work with a portlet. It appears that the portlet server interprets URLs in a different way.

Can somebody tell me how to use window.open("MyJsp.jsp") with a portlet server?

Thanks,

Alejandro Barrero
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that the download from portlet is not feasible directly.
Please use indirect way i.e. a redirect request from jsp to the servlet.

Try this
<%String downloadPrefix = URLHelper.getServerBaseURL(request)+portletResponse.encodeURL("<ServletName>"); %>
window.open("<%=downloadPrefix%>");


where
URLHelper.getServerBaseURL is
public static String getServerBaseURL(HttpServletRequest request){
String strBaseURL = request.getScheme() + "://" + request.getServerName();
strBaseURL += request.getServerPort()>0 ? ":" + request.getServerPort() : "";
return strBaseURL;

}
Good Luck!
 
Vj Borkar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something is weird in last post,
missed following things

[ May 30, 2007: Message edited by: Vijaykumar Borkar ]
 
Alejandro Barrero
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Vijaykumar. You gave me great ideas. The first thing is you have to create a servlet outside of the portlet; in the web.xml it looks like:
<servlet>
<description>
download</description>
<display-name>
Download</display-name>
<servlet-name>download</servlet-name>
<servlet-class>
com.cfmswc.training_dep.servlets.Download</servlet-class>
</servlet>
The next thing is to construct the URL for the server in the JSP:
<%<br /> // Create the base URL for the server.<br /> String serverURL = renderResponse.encodeURL(renderRequest<br /> .getContextPath()<br /> + "");<br /> %>
Finally, create a link:
<a href="<%=serverURL% rel="nofollow">/download?file=pdf/<bean:write name="trainingModule" property="pdf"/>"><img src="<%=serverURL%>/images/Acrobat_Button.gif" alt="Download" height="20" width="20" />
reply
    Bookmark Topic Watch Topic
  • New Topic