Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Colin Freas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic