Display image in JSP Portlet
I create a JSP portlet. But The portlet can't display image(gif file, jpg file). I have modified the provider.xml and the following line is added:
<imageURL>URL_Path</imageURL>
But, the image still cannot be displayed.
How can I display image in JSP portlet?
Leo Cheung,
You could try the following :
1. Add a virtual directory path Alias 'imgf' in the Apache configuration file httpd.conf to load the image file. Add the following line under the alias section :
Alias /imgf/ "<your directory>\images/"
2. Place your gif/jpg files (eg., work.gif) in the images directory.
3. Use the IMG tag of HTML :
<IMG src="/imgf/work.gif" border=0 width=80 height=80> in the JSP file at the location where you need to display the image.
Hope this helps
Pushkala
Similar Messages
-
I have to display image on jsp page with some text output. This image is already saved at a location parallel to web-inf and is generated dynamically using a servlet. I have used img tag html to display the image. Other outputs are taking their values from database.
First problem is that image will be taking time to display in comparision of other outouts from database. I have to refresh the page to get imageon my page.
Second is that if I save image in a folder parallel to web-inf in my project then it will not be displaying the image.
Can I use any jsp functionality to display image with other outputs from database. I have used "*include*". but it shows only that image and other outputs.Best way is to use a servlet for this.
<img src="path/to/imageservlet?id=someidentifier">
<!-- or -->
<img src="path/to/imageservlet/someidentifier">In the servlet's doGet() just write code which gets an InputStream of the image (either directly generated, or just read from the local disk file system, if necessary with help of ServletContext#getRealPath(), or even from the DB by ResultSet#getBinaryStream()) and writes it to the OutputStream of the response. That's basically all. Don't forget to buffer the streams properly to speedup performance and to save memory.
You can find here a basic example: [http://balusc.blogspot.com/2007/04/imageservlet.html]. -
Problem in displaying image on jsp page
I want to display an image on jsp page.I copied the image the image in WebContent folder.
I am able to see the image on the design pane when using the following code:-
<img src="/image.gif" height="50" width="50"> but when i run it in the browser nothing gets diplsyed.
also when i use the image container and select the source option that image is not displayed in the WebContents file..plz help.Hi,
Seems that the Problem is there with the way how you specifuied the Image relative Path:
You have specified the below: The opath of image starts with (/) forward slash:
*<img src="/image.gif" height="50" width="50">*
It means to access this Image WebLogic Container will form a Path like this:
http://localhost:7001/image.gif
BUT may be the image is available inside your Context root: So Change the <Img> tag like following in your JSP:
*<img src="image.gif" height="50" width="50">*
(*NOTE:* Never Start your src path with a Preceesing /)
Now WLE will consider the path like below ..if your Applications Context root is "TestApp"
http://localhost:7001/TestApp/image.gif
I am Assuming that Inside the TestApp Application "image.gif" And "your.jsp" jSP pages are Co-Located (Means available in the Same Directory).
Thanks
Jay SenSharma
http://jaysensharma.wordpress.com (WebLogic Magical Stuff) -
Displaying Image In JSP for Mozilla Browser
Hai all
How to display the .gif image in JSP file using Mozilla browserdo you really think you give enougth information???
the answer to your question is using the html tag <img src='image_url'> -
Problem for displaying image in jsp
I like to retrieve and display image from database in jsp file. Now, image has been retrieved and store in session. But it cannot be displayed in jsp. Who can help me? Thanks a lot!
Codes like below:
<%
byte[] b = (byte[])session.getAttribute("sessionName");
System.out.println(b.length); //the length can be printed at console successfully
if(b != null) {
response.setContentType("image/*");
OutputStream toClient=response.getOutputStream();
toClient.write(b);
toClient.flush();
toClient.close();
%>JSP is designed to return text/html documents.
As such it automatically calls getWriter() to return a character stream. (places it in the implicit variable "out"
Seeing as you want to return bytes, instead of character data, the JSP technology is not applicable
Calling getOutputStream after getWriter has been called results in an IllegalStateException
So calling getOutputStream in a JSP will ALWAYS throw an exception.
Cheers,
evnafets -
ADF BC:Displaying Image On JSP page
Hai All
I'm using ADF BC and JDev 10.1.3.
I want to show an image on my jsp page from database.
For this I have written an servlet with the following code
response.setContentType("image/gif");
OutputStream os = response.getOutputStream();
String amDef = "package.AppModuleName";
String config = "Configuration Name";
ApplicationModule am = Configuration.createRootApplicationModule(amDef, config);
ViewObjectImpl vo =
(ViewObjectImpl)am.findViewObject("ViewObject"); // get view object (the same as used in the table)
System.out.println("vo:" + vo);
vo.executeQuery();
Row row = vo.first();
BlobDomain image = (BlobDomain)row.getAttribute("field");
//System.out.println("image:" + image);
InputStream is = image.getInputStream();
// copy blob to output
byte[] buffer = new byte[10 * 1024];
int nread;
while ((nread = is.read(buffer)) != -1)
os.write(buffer, 0, nread);
os.close();
Configuration.releaseRootApplicationModule(am, false);
This servlet working perfectly.The image is displayed on the page while running
the servlet alone.
My problem is the image is not got displyed on the jsp page
I tried following code to call the servlet
<af:objectImage source="ImageServlet"/>
<img src="ImageServlet" width="140px" height="50px" align="right">
Where ImageServlet is the url-pattern in the web.xml for the servlet
Both method are not working
Any body please help me.......
what I'm missing..............
Is there any other way to display an image on JSP page using backing bean
method.
Thanks
AnsHI,
See: http://kuba.zilp.pl/?id=241
Kuba -
Display image in jsp by retrieving it from DB2
I insert image in to db2 table using the code given below
PreparedStatement preparedStatement =
connection.prepareStatement("INSERT INTO BOOKCOVERS VALUES(?,?)");
File imageFile = new File("c:\\redbookcover.jpg");
InputStream inputStream = new FileInputStream(imageFile);
preparedStatement.setString(1," 0738425826");
preparedStatement.setBinaryStream(2,inputStream,(int)(imageFile.length()));
preparedStatement.executeUpdate();
Now i need to retrieve the image and display in jsp page!!One way would be to write a Servlet... http://yourpage/ImageServlet/?id=0738425826
Servlet:
- get url parameter id
- get the inputstream of the object from the db
- read and directly write the data to the servletoutputstream
regards
slowfly -
Hi,
I'm having problems allowing users to upload images within my j2ee application and then displaying them again. Does anybody have any tips?
I don't really want to store the image in the db (just the image name), but I can't figure out where to put the image once it has been uploaded so that the JSP page can find it again.
All of my current images are inside my .war file so I can't simply add the uploaded image to these. I'm using JBoss 4.0 if it matters....
Your help is greatly appreciated,
Stu.Hi.
Probably not the best solution, but I've solved this by creating one war file that only holds images. It only has an empty jsp index file.
I use a Unix workstation, so I've creted a symlink, (shortcut) from the folder I uppload images to, to the empty project. Then I can call the images from the jsp, and deploy new versions of my main application without the images.... -
Hi all,
I created a Jsp page and I stored my images in the dist-->images folder and by using the following code
<% String image = componentRequest.getWebResourcePath() + "/images/image.gif"; %>
<img src="<%=image%>">
I try to display the images in that page.
Yes I am successful in displaying one image.
But my problem here is to display more than two images which are present in that page.
Can any body help me regarding this.
Your help will be highly rewarded with points.
Regards,
Ramana.Hi Ramana,
Set the image border to 1 and check atleast your borders get displayed. If it displays the border, then the problem may be with your image path.
Before deploying for each modification in your JSP, check the preview of the page using "Preview" tab.
Are you getting any exception?
Check your whole JSP page once again (check all the scriplets).
Regards,
Uma
Message was edited by: Uma Maheswari -
How to dynamically display images in JSPs
This took a little while to figure out so I thought I'd share. After
doing some research I was led to the following approach on how to load
images from an Oracle database into a JSP:
The "main" JSP:
<HTML>
<head>
<title>Image Test</title>
</head>
<body>
<center>
hello
<P>
<img border=0 src="getImage.jsp?filename=2cents.GIF">
<P>
<img border=0 src="getImage.jsp?filename=dollar.gif">
<P>
world
</body>
</HTML>
And this is the image getter:
<% try {
response.setContentType("image/gif");
String filename = (String) request.getParameter("filename");
java.sql.Connection conn =
java.sql.DriverManager.getConnection("jdbc:weblogic:pool:orapool"); //
connect to db
java.sql.Statement stmt = conn.createStatement();
String sql = "select image from testimage where filename = '" +
filename + "'";
java.sql.ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
byte [] image = rs.getBytes(1);
java.io.OutputStream os = response.getOutputStream();
os.write(image);
os.flush();
os.close();
conn.close();
catch (Exception x) { System.out.println(x); }
%>
The thing to note is that there are no <%@ page import="..." %> or <%@
page contentType="..." %> tags - just the single scriptlet. It
seems that for every "<%@" the weblogic compiler sees it puts
out.print("\r\n"); statements in the generated java source.(???) I
don't know much about how browsers work but I think that once it sees
flat ascii come at it it treats everything that follows as text/plain
which is incorrect for the binary stream that's being sent. Another
work around was to set out = null; but that's kind of ugly and might
produce server errors. The real fix is to write a bean to handle images
which I'll work on next (does anybody have any hints on how to do
that?)
-Erik.
PS, thank you to the people that posted info in this group that helped
me track down this problem. -
Erik,
No need to write a bean, just write a serverlet...
you can use this function (however you might want to include some
improved error handeling)
Oh, FYI to prevent caching by the browser you might want to pass a parameter
into the creation of the image like timeseconds or (in my case) variable
text which
was included into the image produced. (You need not even use the variable in
the code, the server is not bright enough to tell)
<IMG SRC=".....MyImageProducer?file=my.jpeg×ecs=12345134">
Jay L. Toops
public boolean httpJpegStreamWriter(BufferedImage ib, HttpServletResponse
response) {
try {
javax.servlet.ServletOutputStream myout = response.getOutputStream();
byte b[] = new byte[1024];
response.setContentType("image/jpeg");
//File file = new File("./myserver/public_html", "testjlt.jpg");
//FileOutputStream fos = new FileOutputStream(file);
ByteArrayOutputStream fos = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(ib);
param.setQuality(1.0f,false);
encoder.setJPEGEncodeParam(param);
encoder.encode(ib);
b = fos.toByteArray();
response.setContentLength(b.length);
myout.write(b, 0, b.length);
myout.flush();
myout.close();
} catch (Exception e) {
System.out.println("MY httpJpegStreamWriter Exception\n is :"+
e.toString());
return(false);
return(true);
Erik Lindquist wrote:
> This took a little while to figure out so I thought I'd share. After
> doing some research I was led to the following approach on how to load
> images from an Oracle database into a JSP:
>
> The "main" JSP:
>
> <HTML>
> <head>
> <title>Image Test</title>
> </head>
> <body>
> <center>
> hello
> <P>
> <img border=0 src="getImage.jsp?filename=2cents.GIF">
> <P>
> <img border=0 src="getImage.jsp?filename=dollar.gif">
> <P>
> world
> </body>
> </HTML>
>
> And this is the image getter:
>
> <% try {
> response.setContentType("image/gif");
> String filename = (String) request.getParameter("filename");
> java.sql.Connection conn =
> java.sql.DriverManager.getConnection("jdbc:weblogic:pool:orapool"); //
> connect to db
> java.sql.Statement stmt = conn.createStatement();
> String sql = "select image from testimage where filename = '" +
> filename + "'";
> java.sql.ResultSet rs = stmt.executeQuery(sql);
> if (rs.next()) {
> byte [] image = rs.getBytes(1);
> java.io.OutputStream os = response.getOutputStream();
> os.write(image);
> os.flush();
> os.close();
> }
> conn.close();
> }
> catch (Exception x) { System.out.println(x); }
> %>
>
> The thing to note is that there are no <%@ page import="..." %> or <%@
> page contentType="..." %> tags - just the single scriptlet. It
> seems that for every "<%@" the weblogic compiler sees it puts
> out.print("\r\n"); statements in the generated java source.(???) I
> don't know much about how browsers work but I think that once it sees
> flat ascii come at it it treats everything that follows as text/plain
> which is incorrect for the binary stream that's being sent. Another
> work around was to set out = null; but that's kind of ugly and might
> produce server errors. The real fix is to write a bean to handle images
> which I'll work on next (does anybody have any hints on how to do
> that?)
>
> -Erik.
>
> PS, thank you to the people that posted info in this group that helped
> me track down this problem. -
-
Hi there.. I have a problem with displaying an image from table with blob data type.
Here's what have I done:
I have this page to display an image from requestScope.test (I've set this value in data action).
But the problem is, the image is only displayed once. Seems that after it read the eof of stream, It'll not move the pointer to the beginning. The image will show up again if view object retrieve new data from the database.
Here's my code:
------------------------------------------------------begin of code----------------------------------------------------
<%@ page import="java.io.InputStream,oracle.jbo.client.Configuration,oracle.jbo.ApplicationModule"%>
<%
response.setContentType("image/jpeg");
response.addHeader("Content-Disposition","filename=img.jpg");
InputStream sImage = ((oracle.jbo.domain.BlobDomain)request.getAttribute("test")).getInputStream();
byte[] bytearray = new byte[4096];
int size = 0;
while((size=sImage.read(bytearray))!= -1 ){
response.getOutputStream().write(bytearray,0,size);
sImage.close();
%>
------------------------------------------------------end of code----------------------------------------------------
Any suggestion?
Thx,
AndreSorry, I've just read the java doc for oracle.jbo.domain.BlobDomain and find method toByteArray(), and this method can solve my problem. Here's the new code:
----------------------------------------begin of code-----------------------------------------------
<%
response.setContentType("image/jpeg");
response.addHeader("Content-Disposition","filename=img.jpg");
byte[] bytearray = ((oracle.jbo.domain.BlobDomain)request.getAttribute("test")).toByteArray();
response.getOutputStream().write(bytearray, 0, bytearray.length);
%>
----------------------------------------begin of code-----------------------------------------------
But I still intereset with the first solution, why it always find eof after it displayed. I've already closed the stream.
Thx,
Andre -
Hi,
I am setting image byte array to servlet output stream after setting content type as "image/jpg".
Problem 1:
if I set contentType as "image/jpeg" or "image/gif" it is showing some image on page and for some image red cross mark comes in upper corner.
Problem 2 :
For "image/jpg", File Download window pops up and asks me for saving the file. I don't want to save it.
Below is the sequence of code in my action class.
response.resetBuffer();
response.setContentType("image/jpeg");
servletOutputStream.write(barray);
servletOutputStream.flush();
servletOutputStream.close();
this image should be displayed on new opened window from where i have called above action.Problem 1:
if I set contentType as "image/jpeg" or "image/gif" it is showing some image on page and for some image red cross mark comes in upper corner.Well, what is the correct contentType? You can't just set it on whim, it needs to be the correct type. Which is probably why the image that are actually that type show up, while others don't.
Take a look at these to understand how to improve your code:
- [http://balusc.blogspot.com/2007/04/imageservlet.html]
- [http://www.exampledepot.com/egs/javax.servlet/GetImage.html]
For example, you should set your content length. -
How to display uploaded image in jsp page.
Hello,
I am using struts 1.2.9 and and have uploaded image on the server. Now what I want to do display the image in jsp page after clicking on one link in jsp. I have tried many thing to display image in jsp page. But I am getting an error during displaying image in jsp. I have displayed absolute path in servlet. and used InputStream and outputstream to display image in jsp page.
Can any one help.
Thanks in advance
Manveer SinghFollow this. This topic is very popular recently on the forum.
-
How do you display images relative to a Portlet JSP page?
For Welblogic Portal 7.0, I want to display a simple image (.gif) on a JSP page.
The problem is that I need to use a 'hard coded' url to the image such as the following:
<img src="portlets/test/images/test.gif" border="0" alt="Test 1">
- OR -
<img src="<webflow:createResourceURL resource="/portlets/test/images/test.gif" />"
border="0" alt="Test 2">
This makes the portlet less portable.
My goal is to display an image relative to the current JSP Portlet page (in the test
directory).
Does anyone know how to accomplish this?
In WL Portal 4.0 I used:
<img src="<%=fixupRelativeURL(pathFromRequest(request) + "images/test.gif", request)%>"
width=24 height=22 border=0 alt="test">
This does not work anymore as fixupRelativeURL does not seem to exist in the 7.0
API.
Your help would be greatly appreciated!
Thanks,
MattExactly what I was looking for!
Thank you Subbu.
Subbu Allamaraju <subbuATBeaDOTCom> wrote:
Matt,
There is an indirect way to achieve this.
(a) In your portlet JSP, get the PortletState. There is an example in
/framework/portlet.jsp.
(b) Get the content URL for this portlet by calling
portletState.getUrl(Portlet.URL_CONTENT).
Depending on where your image is (with respect to the content URL), you
can construct a new URL to the image. For example if your content is at
at /portlets/myportlet/foo.jsp and your image is in at
/portlets/myportlet/images/myimage.gif, you can reconctruct the latter
from the first one.
Once you do this, prepend the constructed URL with the web app context
root (request.getContextPath()).
Hope this helps.
Subbu
matt wrote:
For Welblogic Portal 7.0, I want to display a simple image (.gif) on aJSP page.
The problem is that I need to use a 'hard coded' url to the image suchas the following:
<img src="portlets/test/images/test.gif" border="0" alt="Test 1">
- OR -
<img src="<webflow:createResourceURL resource="/portlets/test/images/test.gif"/>"
border="0" alt="Test 2">
This makes the portlet less portable.
My goal is to display an image relative to the current JSP Portlet page(in the test
directory).
Does anyone know how to accomplish this?
In WL Portal 4.0 I used:
<img src="<%=fixupRelativeURL(pathFromRequest(request) + "images/test.gif",request)%>"
width=24 height=22 border=0 alt="test">
This does not work anymore as fixupRelativeURL does not seem to existin the 7.0
API.
Your help would be greatly appreciated!
Thanks,
Matt -
Problem with displaying BLOB images on JSP page using a servlet
hi. I have a big problem with displaying BLOB images using JSP. I have a servlet that connects to the oracle database, gets a BLOB image , reads it, and then displays it using a BinaryStream. The problem is , this works only when i directly call that servlet, that is http://localhost:8080/ImageServlet. It doesn't work when i try to use that servlet to display my image on my JSP page (my JSP page displays only a broken-image icon ) I tried several coding approaches with my servlet (used both Blob and BLOB objects), and they work just fine as long as i display images explicitly using only the servlet.
Here's what i use : ORACLE 10g XE , Eclipse 3.1.2, Tomcat 5.5.16 , JDK 1.5
here is one of my image servlet's working versions (the essential part of it) :
BLOB blob=null;
rset=st.executeQuery("SELECT * FROM IMAGES WHERE ID=1");
while (rset.next())
blob=((OracleResultSet)rset).getBLOB(2);
response.reset();
response.setContentType("image/jpeg");
response.addHeader("Content-Disposition","filename=42.jpeg");
ServletOutputStream ostr=response.getOutputStream();
InputStream istr=blob.getBinaryStream(1L);
int size=blob.getBufferSize();
int len=-1;
byte[] buff = new byte[size];
while ((len=istr.read( buff ))!=-1 ) {
ostr.write(buff,0,len);
response.flushBuffer();
ostr.close(); and my JSP page code :
<img src="/ImageServlet" border="0" > If you could just tell me what i'm doing wrong here , or if you could show me your own solutions to that problem , i would be very greatful ,cos i'm realy stuck here , and i'm rather pressed for time too. Hope someone can help.I turns out that it wasn't that big of a problem after all. All i had to do was to take the above code and place it into another JSP page instead of into a servlet like i did before. Then i just used that page as a source for my IMG tag in my first JSP. It works perfectly well. Why this doesn't work for servlets i still don't know, but it's not a problem form me anymore . Ofcourse if someone knows the answer , go ahead and write. I would still appriceatte it.
here's the magic tag : <img src="ImageJSP.jsp" border="0" > enjoy : )
Maybe you are looking for
-
No message tone or icon after software update
I did a software update yesterday to the 10.2.1.2102 and now when i receive a text my phone doesnt notify me of this. No tone goes off and no icon appears. HELP!
-
Printing to PDF (custom page size) from AutoCAD Won't Work Anymore
The DWG to PDF driver provided with AutoCAD 2013 will not save my artwork to a high enough resolution for me to use. See attached screen shots of the PDF's printed by AutoCAD's DWG to PDF driver. The only PDF print driver that I have found that will
-
RoleMapper with an external LDAP
Dear friends, We use an external LDAP to store information related to users, groups and roles. We have managed to configure an out of box LDAP Authenticator within our realm for authentication. We wanted some guidance on configuring or writing RoleMa
-
IMac refuses to boot properly - hardware problem?
Hi all, I'm having some problems with my iMac (purchased last October) and was hoping some kind soul could shed some light. Here's where I'm at now. - The iMac doesn't boot. When I press the power button, it chimes, but then nothing happens and I jus
-
Hi All, We have just installed LSMW 1.8 on 4.6D. It display German excepted login language is English. Any comment? Thanks Wilson