Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying a random picture in a JSP file

 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65110
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pras Tiwari
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tips
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic