The Source for Java Technology Collaboration

Home » java.net Forums » GlassFish » GlassFish

Thread: JSF - referencing images outside of deployed .ear

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 7 - Last Post: Dec 13, 2007 1:50 PM by: nohacks
zambizzi

Posts: 229
JSF - referencing images outside of deployed .ear
Posted: Jan 2, 2007 3:17 PM
  Click to reply to this thread Reply

Can someone direct me to some information on how I would reference images that users would upload if I'm deploying a .ear file to the server (i.e. could not use a relative folder inside of web root)?

I've done this w/ Wicket but never w/ JSF - it was a pretty big pain in Wicket.

zambizzi

Posts: 229
Re: JSF - referencing images outside of deployed .ear
Posted: May 2, 2007 2:20 PM   in response to: zambizzi
  Click to reply to this thread Reply

I thought I'd bump this since it's been a few months.

I need the ability to upload images via JSF. I'd store them in C:\images, for example, but how would I call them from the web application to display them on a JSF view?

Thanks!

Jason Lee
RE: Re: JSF - referencing images outside of deployed .ear
Posted: May 2, 2007 2:23 PM   in response to: zambizzi
  Click to reply to this thread Reply

I would guess either a servlet or PhaseListener would have to be
employed there:

Serlvet: http://foo/imageServlet/imageName.jpg
PhaseListener
http://foo/myJsfContext/someSpecialUrlThePlPicksUpOn.jsf?image=imageName
.jpg

Something like that. You might look at Shale Remoting for the PL if you
go that route...

-----
Jason Lee, SCJP
JSF RI Dev Team
Senior Software Engineer
http://www.iec-okc.com


> -----Original Message-----
> From: glassfish@javadesktop.org [mailto:glassfish@javadesktop.org]
> Sent: Wednesday, May 02, 2007 4:21 PM
> To: users@glassfish.dev.java.net
> Subject: Re: JSF - referencing images outside of deployed .ear
>
> I thought I'd bump this since it's been a few months.
>
> I need the ability to upload images via JSF. I'd store them
> in C:\images, for example, but how would I call them from the
> web application to display them on a JSF view?
>
> Thanks!
> [Message sent by forum member 'zambizzi' (zambizzi)]
>
> http://forums.java.net/jive/thread.jspa?messageID=215236
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net


nohacks

Posts: 3
Re: JSF - referencing images outside of deployed .ear
Posted: Dec 13, 2007 1:49 PM   in response to: zambizzi
  Click to reply to this thread Reply

Hi,

Did you ever find a way to display images from a dir outside of your ear?

Thanks
Phil

cayhorstmann

Posts: 228
Re: JSF - referencing images outside of deployed .ear
Posted: May 3, 2007 6:23 AM   in response to: zambizzi
  Click to reply to this thread Reply

It's a pretty big pain in JSF too. You need a servlet that serves up the images. Register the servlet in the usual way and include links such as GetImage?id=myfile.png.

Here is the code from a recent project where I needed to do that.

<pre>
package com.horstmann.qq.web;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetImageServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String id = request.getParameter("id");
String extension = id.substring(id.lastIndexOf('.') + 1);
response.setContentType("image/" + extension);
String imageDirectory = getServletContext().getInitParameter("com.horstmann.qq.imagePath");
FileInputStream in = new FileInputStream(new File(new File(imageDirectory), id));
OutputStream out = response.getOutputStream();
final int BUFFER_SIZE = 1024;
byte[] buffer = new byte[BUFFER_SIZE];
int n = 0;
while (-1 != (n = in.read(buffer))) {
out.write(buffer, 0, n);
}
in.close();
out.close();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
</pre>

I know the <pre> tags don't work--what does one do in this system to format code???

zambizzi

Posts: 229
Re: JSF - referencing images outside of deployed .ear
Posted: May 3, 2007 7:43 AM   in response to: cayhorstmann
  Click to reply to this thread Reply

Ahh, ok, that makes sense - I've done something similar to this using jfreechart in JSP.

Ugh...not what I was hoping to hear...but thanks for the info! I think I might just stick w/ Wicket. I can't get excited about JSF...I keep trying, however.

I've found, through trial-and-error, that the [ code ] [ / code ] blocks work here (remove spaces, obviously).

package com.horstmann.qq.web;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetImageServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String id = request.getParameter("id");
String extension = id.substring(id.lastIndexOf('.') + 1);
response.setContentType("image/" + extension);
String imageDirectory = getServletContext().getInitParameter("com.horstmann.qq.imagePath");
FileInputStream in = new FileInputStream(new File(new File(imageDirectory), id));
OutputStream out = response.getOutputStream();
final int BUFFER_SIZE = 1024;
byte[] buffer = new byte[BUFFER_SIZE];
int n = 0;
while (-1 != (n = in.read(buffer))) {
out.write(buffer, 0, n);
}
in.close();
out.close();
}
 
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}


km

Posts: 681
Re: JSF - referencing images outside of deployed .ear
Posted: May 3, 2007 8:35 AM   in response to: zambizzi
  Click to reply to this thread Reply

Is this really needed?

Why can't we use ServletContext's methods where we load a resource from a name?

That way, your web.xml becomes more portable, because not many vendors (including GF V2) allow you to change your init parameters (in web.xml, for entire web app/each servlet) post deployment.

So, just place your library at a location where you know your server will pick up and make it
available for every application and write applications that way.

Sorry if I have misunderstood ...

Kedar

nohacks

Posts: 3
Re: JSF - referencing images outside of deployed .ear
Posted: Dec 13, 2007 1:50 PM   in response to: km
  Click to reply to this thread Reply

Re: JSF - referencing images outside of deployed .ear
Posted: Dec 13, 2007 1:49 PM in response to: zambizzi
Click to edit this message... Click to reply to this thread Reply

Hi,

Did you ever find a way to display images from a dir outside of your ear?

Thanks
Phil




 XML java.net RSS