This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
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

printing images in jsp

 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi folks

i want to display some images in the jsp. by taking

the images from DataBase i already inserted the image into

the dataBase as a BLOB and i have retrived the image from DataBase

as a byte array

now i dont know to convert the byte array to image

iam using struts and hibernate

eagerly waiting for your replies


regards
amie
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As with any other HTML page, your JSP must use an <img> tag to reference the image. The src attribute for that tag should be the URL of a servlet (not a JSP) that reads and sends the image data from the db, and sets the response headers accordingly.

This has been asked before on a number of occasions, so search the Servlets forum for previous discussions.
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi bloggers

this is my code

<%
response.setContentType("image/gif");
DocumentContent dc=(DocumentContent)request.getAttribute("documentContent");
byte arr[]=dc.getContent();
%>
<table>
<tr>
<td>
<img src="<%= arr %>" width="50" height="50">
<input type="text" name="Name" />
</td>

it dosent print any image but its showing a small box in bowser

iam using struts and hibernate iam getting the binary data

in a byte array and assigned to the src attribute of img tag

please help to trace out the problem
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you see Bear's post?

The 'src' attribute of an <img /> tag needs to be a URL.
The browser will use that URL to make a request to the server for the image file.

If you want to serve images from a database, you'll need to create a servlet that can stream the blob information to the browser.

You can't mix binary (image) data with text (HTML) in the same response.
Browsers make separate requests for all of the images in a web page.



bloggers?
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes ben i uderstood my mistake

now i tried in this manner

i used a seperate jsp to print the image

iam having 2 jsp pages editcoursetool.jsp and getImage.jsp

the code in editcoursetool.jsp is as follows

<% response.setContentType("text/html");
DocumentContent dc=(DocumentContent)request.getAttribute("documentContent");
request.setAttribute("dc",dc);
%>

<img src="getImage.jsp" border="0" >

this dc object has a byte array property that contains the image in

binary

the code in getImage.jsp is as follows

<%
response.setContentType("image/jpeg");
ServletOutputStream os= response.getOutputStream();
DocumentContent dc2=(DocumentContent)request.getAttribute("dc");
os.write(dc2.getContent());
response.setContentType("text/html");
out.print(" print ");
%>

when i tried this i got an error like this

can you point out my mistage


javax.servlet.jsp.JspException: ServletException in '/jsp/coursetool/editcoursetool.jsp': null
at org.apache.struts.taglib.tiles.InsertTag$InsertHandler.doEndTag(InsertTag.java:923)
at org.apache.struts.taglib.tiles.InsertTag.doEndTag(InsertTag.java:462)
at org.apache.jsp._layout._jspService(_layout.java:384)
at com.ibm.ws.webcontainer.jsp.runtime.HttpJspBase.service(HttpJspBase.java:89)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.ibm.ws.webcontainer.jsp.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:344)
at com.ibm.ws.webcontainer.jsp.servlet.JspServlet.serviceJspFile(JspServlet.java:683)
at com.ibm.ws.webcontainer.jsp.servlet.JspServlet.service(JspServlet.java:781)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174)
at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116)
at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42)
at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:1014)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:592)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:204)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1062)
at org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:263)
at org.apache.struts.tiles.TilesRequestProcessor.processTilesDefinition(TilesRequestProcessor.java:239)
at org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:302)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:229)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174)
at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116)
at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42)
at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:76)
at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:292)
at net.sf.acegisecurity.ui.switchuser.SwitchUserProcessingFilter.doFilter(SwitchUserProcessingFilter.java:224)
at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at net.sf.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:108)
at net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter.doFilter(SecurityEnforcementFilter.java:197)
at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at net.sf.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:143)
at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at net.sf.acegisecurity.ui.rememberme.RememberMeProcessingFilter.doFilter(RememberMeProcessingFilter.java:154)
at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at net.sf.acegisecurity.ui.basicauth.BasicProcessingFilter.doFilter(BasicProcessingFilter.java:214)
at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at net.sf.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:324)
at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at net.sf.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:220)
at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at net.sf.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:173)
at net.sf.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:120)
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:132)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:71)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:1010)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:592)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:204)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java:125)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:286)
at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71)
at com.ibm.ws.webcontainer.cache.invocation.CacheableInvocationContext.invoke(CacheableInvocationContext.java:116)
at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:186)
at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:334)
at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56)
at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:615)
at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:449)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:912)
[ November 06, 2006: Message edited by: Amirtharaj Chinnaraj ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't read my reply, did you?
 
Stefan Evans
Bartender
Pie
Posts: 1670
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Bear said, this code needs to be in a Servlet NOT a JSP.
The reason: JSPs are designed for producing text.
As such they already call response.getWriter(). Calling response.getOutputStream() after calling getWriter() is not guaranteed to work. Also JSPs output carriage returns in places where you prefer they not do, and thus corrupt anything you try to send via the outputstream.
It can technically be done buts it is very fragile.

Most of what you have in that getImage.jsp is java code anyway. Not hard to turn into a servlet.

Also note that this will be two seperate requests.
One request for the JSP.
One request for the image.
As thus, you cannot share attributes via the request scope (hence your null pointer exception). You will have to make it a session attribute, or pass it in some other manner.

Cheers,
evnafets
[ November 06, 2006: Message edited by: Stefan Evans ]
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first of all i would like to thank you

for your kind reply Evans.

i have found this techinique of printing images in jsp

from this url

http://p2p.wrox.com/topic.asp?TOPIC_ID=17616&SearchTerms=displaying,image,jsp

so i tried that .since i am using struts i dont like like to write a

servlet and map it in the web.xml so tried of only using jsp

what my final question is that it is never possible to do this without

using a servlet

is their some other way using struts to do this task

advance thanks
regards
amir
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing about Struts that forces you to use a JSP for this.
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bear what i want to ask you is how to write a pure servlet in Action

class .is it possible ,can you send me the procedure for doing this

operation using DispatchAction if you have some sample please send me

advance thanks

regards
amir
[ November 06, 2006: Message edited by: Amirtharaj Chinnaraj ]
 
Stefan Evans
Bartender
Pie
Posts: 1670
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anything you can do in a servlet, you can do in a Struts action.

In this case, to write a struts action to return an image, it would be exactly the same as for the servlet code. The only difference is that you return null from the execute method to tell Struts you have handled the response.

1 - retrieve file from disk with java.io classes
2 - write the data out to the servlet outputstream (response.getOuputStream())
3 - return null from Struts execute method to indicate that you have handled the response and Struts doesn't need to.
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes evans i tried what you told i got an error

like java.lang.IllegalStateException: OutputStream already obtained

this is my code

public ActionForward edit(ActionMapping mapping,ActionForm form,
HttpServletRequest req,HttpServletResponse res)throws IOException
{
String toolName=req.getParameter("name");
req.setAttribute("toolName",toolName);
BaseDynaValidatorForm myform =(BaseDynaValidatorForm)form;
CourseTool courseTool1=(CourseTool)courseToolService.edit(toolName);
getActivityState(req).setValue("courseTool",courseTool1);
this.printImage(mapping,form,req,res);
ArrayList groupList = new ArrayList();
groupList.add(new LabelValueBean("public","PUBLIC"));
groupList.add(new LabelValueBean("techingassintant","TEACHING_ASSISTANT"));
groupList.add(new LabelValueBean("proffessor","PROFFESSOR"));
myform.set("courseTool",courseTool1);
myform.set("groupList",groupList);
return mapping.findForward("edittoolform");
}

public ActionForward printImage(ActionMapping mapping,ActionForm form,
HttpServletRequest req,HttpServletResponse res)throws IOException
{
res.setContentType("image/jpg");
String toolName =(String)req.getAttribute("toolName");
HashMap nameImageMap =(HashMap)getActivityState(req).getValue("nameImageMap");
DocumentContent dc=(DocumentContent)nameImageMap.get(toolName);
byte a[]=dc.getContent();
OutputStream os=res.getOutputStream();
os.write(a);
return null;
}


edittoolform in edit method is going to render my image you can see their

i have used a seperate method printImage to print image. apart from that

i dodnt no how to map the <img src= ? > in the jsp in which i have to

display the image .wether i that i have to give my action path



like <img src="coursetool"/>

<action path="/coursetool" type="org.springframework.web.struts.DelegatingActionProxy" name="coursetoolform" scope="request" parameter="method" validate="false">
<forward name="showcoursetoolform" path="coursetoolform"/>
<forward name="edittoolform" path="edittoolform" />

</action>


iam looking for your replies
regards
amir
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At this point this has become Struts-specific enough to warrant moving to the Struts forum.
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't quite understand how the action code that you posted ties in with the action mapping that you have given. I have done something similar to this in a past life. The src attribute of your img tag needs to point to the full action URL (with the .do extension). It should be the same URL that you can type into the browser address bar and see the image by itself. You might want to use the page attribute of the html:img tag or use the html:rewrite tag to produce the URL.

- Brent
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic