File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes How do I use a servlet instead of jsp as the view for returning binary image data? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How do I use a servlet instead of jsp as the view for returning binary image data?" Watch "How do I use a servlet instead of jsp as the view for returning binary image data?" New topic
Author

How do I use a servlet instead of jsp as the view for returning binary image data?

Colin Freas
Greenhorn

Joined: Aug 11, 2009
Posts: 6

Referring to section 3.5.1 of this page: http://struts.apache.org/1.x/userGuide/building_view.html

That talks about using a servlet to render binary image data.

Right now, I have my binary image data being written in a JSP, which is sort of fine.

But I'd rather do this in a servlet, because the JSP markup is useless here.

How do I set that up? Is there some magic in struts.xml? Do I need to play with the controller?

All help appreciated,
Colin Freas

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

I didn't go through that page that you pointed out, but you can use a servlet as a view just like you use a JSP. Just use the appropriate mapping in the struts-config.xml



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Normally you'd just write to the response, and return a null in Struts 1, or a null or ActionSupport.NONE in Struts 2 (it's handy to specify which version you're using :)
Colin Freas
Greenhorn

Joined: Aug 11, 2009
Posts: 6

Thanks for the replies, gents. btw: Struts 1.

So, I'm trying to figure out the Struts-iest way of doing what I'm doing.

I have a page with some charts on it. I have the action, the form, and the jsp for the page containing the charts. The charts themselves are images and html image maps generated by JFreeChart. So, I need the image map markup included in the jsp, and I need the binary image data in the response to the request from the <img> src attributes.

For argument's sake, let's say I don't want to have the forward to the servlet as Ankit suggest. I see how this is a completely viable solution, but this is the only point in the application where that technique would be used, which could make for maintenance problems later.

So, thinking about this, I wondered if the following makes sense, vis-a-vis the Struts MVC paradigm. I put the methods that generate the binary image data into the form class. The chart action then calls those methods, and overrides the normal response, when a parameter is set in the request.

So... does that make sense? Is that the Struts-iest way to do this?

-Colin
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I wouldn't put any image generation code into the ActionForm, that strikes me as bizarre. ActionForms are a DTO between the web page and Struts. (Some people use them to pass non-form data, I don't--YMMV). I'd put the chart generation code into a service object, inject it into an action, and use that action as the URL for the image tag.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I use a servlet instead of jsp as the view for returning binary image data?