my dog learned polymorphism*
The moose likes JSP and the fly likes Displaying a random picture in a JSP file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Displaying a random picture in a JSP file" Watch "Displaying a random picture in a JSP file" New topic
Author

Displaying a random picture in a JSP file

David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
I have a JSP page and would like to display a random picture each time someone visits the page.

I created a java class to do this and put it in /WEB-APPS/classses/myAPP/web. Right now the file with the pictures are in httpdocs, and hopefully soon in the same directory as my JSP file. See my question on that subject.

How do I use File to get the file names without an ugly path like ../../../../../httpdocs/img/?


"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

The folder containing your images must reside outside of the WEB-INF folder in order to be accessible from the browser.

If you are using a primitive web app structure where you are addressing your JSPs directly without any interveining servlet controllers or a front controller, just using the image file name will find it in the same folder.

Otherwise, you should be using server-relative addressing rather than page-relative addressing, where each url begins with the web app's context path and proceeeds from there.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pras Tiwari
Ranch Hand

Joined: Nov 07, 2005
Posts: 186
I think you can use <%=request.getContextPath()%>/your_image_dir_under_webcontents/image.jpg inside your jsp page. Or you can use request.getContextPath()/your_image_dir_under_webcontents/image.jpg inside your servlet.


********Deserve Before You Desire********
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
Thanks for the tips
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
I tried Prashant Tiwari's suggestion and it didn't work. It still tries to look under httpdocs.

The file structure looks like this:

.. /webapps/ROOT/index.jsp

The path I am trying to get to the pictures and css file:

.. /ROOT/img/random/

.. /ROOT/css/

Do I need to create a web.xml file and if so what should it look like to recognize these paths?

Once this is complete, would the JSP file automatically know where to look for the .java file? I put it in /webapps/ROOT/WEB-INF/classes/index/model/RandomPicture.class, where the class is in the index.model package. I think this is the correct place to put .class and servlet files.

Then if I understand correctly, assuming that I can get Tomcat to recognize the correct path inside Tomcat, in RandomPicture, the path would just be /img/random/
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by David McCombs:
I tried Prashant Tiwari's suggestion and it didn't work. It still tries to look under httpdocs.
The file structure looks like this:
.. /webapps/ROOT/index.jsp
The path I am trying to get to the pictures and css file:
.. /ROOT/img/random/
.. /ROOT/css/

Is this a hosted server?
If so, are they connecting Tomcat with the Apache webserver?
If so, the connector configuration files probably don't have mappings for url-patterns other than "servlet/*" and "*.jsp".

If this is the case, you'll either need to get them to add more mappings for you or you'll need to create a servlet that can stream the images to the browser.
See: http://simple.souther.us (SimpleStream) for an example app that does this.


Do I need to create a web.xml file and if so what should it look like to recognize these paths?

It is always a good idea to create one but, you don't actually need one for Tomcat to recognize your folder as a webapp.


Once this is complete, would the JSP file automatically know where to look for the .java file? I put it in /webapps/ROOT/WEB-INF/classes/index/model/RandomPicture.class, where the class is in the index.model package. I think this is the correct place to put .class and servlet files.

Yes, and yes, it is.


Then if I understand correctly, assuming that I can get Tomcat to recognize the correct path inside Tomcat, in RandomPicture, the path would just be /img/random/

That URL is missing the contextPath.
It will work for you as long as your app is the root app in the webserver.
The best way to build relative URLs is to dynamically create the contextPath portion by reading it from the request object:



This allows you to create test/dev copies of your app or to rename your app without needing to find every link and update the context path.
[ December 14, 2006: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by David McCombs:

... Do I need to create a web.xml file and if so what should it look like ... ? ...


More on this...

The easiest way to insure that you have a valid web.xml file is to download the version of Tomcat (or whatever server you're using) and copy the web.xml file under the ROOT app that ships with it.
Alter it as needed.

There is a link to the servet and JSP specs in my signature.
The Servlet spec covers deployment descriptors (web.xml) in full with easy
to read diagrams, and samples.
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
Thank you, I will go through all that and see how it goes. I really appreciate it.

Yes they have Tomcat connected to Apache. I talked to my host and they claim that if they changed the mappings then the php programs I run, like phpbb will no longer work properly without moving it to a subdomain. Maybe that is the best solution, what do you think? I would love to ditch them but it is tough finding a host that runs Tomcat and allows connecting to a port through ServerSocket.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying a random picture in a JSP file