This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes image not displayed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "image not displayed" Watch "image not displayed" New topic
Author

image not displayed

tushar panda
Ranch Hand

Joined: Feb 16, 2009
Posts: 133
i wrote code for displaying image according to the referer header

cimage.html


servletcode


i wrote this code to display the image by checking the referer page .
my image is in "WEB-INF\classes" folder .

the output page did not display image, on clicking properties over image position i get this

the Location says it goes to "http://localhost:8080/servlet/image "

but actually it should go to "http://localhost:8080/servlet/tomcat.gif" or "http://localhost:8080/servlet/jrun.gif"

what went wrong

Do Right , Fear No Man
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61450
    
  67

tushar panda wrote:the Location says it goes to "http://localhost:8080/servlet/image "


Why does this surprise you, when that's exactly what you told it:

out.println("<IMG SRC='/servlet/image'>");


?

but actually it should go to "http://localhost:8080/servlet/tomcat.gif" or "http://localhost:8080/servlet/jrun.gif"

Then why isn't that what you're specifying as the URL of the image in the <img> tag?

And what's with the "servlet" prefix to the image files? Do you have a folder named "servlet"?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
tushar panda
Ranch Hand

Joined: Feb 16, 2009
Posts: 133
Bear Bibeault wrote: Why does this surprise you, when that's exactly what you told it:


i wrote it so that the "image " will give me "tomcat.gif" or "jrun.gif" according to the condition , if that is wrong , please post what will be changed in that line or in the code .

Bear Bibeault wrote:Then why isn't that what you're specifying as the URL of the image in the <img> tag?


how can i put the image name directly in the image tab , i have made it a variable so that either "tomcat.gif" or "jrun.gif" will replace the image according to the condition .

Bear Bibeault wrote:And what's with the "servlet" prefix to the image files? Do you have a folder named "servlet"?


the prefix is to go inside \WEB-INF\classes folder because that's the place where i have my servlet & both images .

sir ,
one more thing should i add this line "res.setContentType("image/gif");" below this line "res.setContentType("text/html");"
because some mime(picture files) is used , i am not sure but may be.

---
TUSHAR
Shruthi Sam
Ranch Hand

Joined: Jul 09, 2008
Posts: 73
Hi,
Change the below line in your code

out.println("<IMG SRC='/servlet/image'>");


to the following and try again


out.println("<IMG SRC='/servlet/"+image+"'>");
tushar panda
Ranch Hand

Joined: Feb 16, 2009
Posts: 133
Shruthi Sam wrote:Hi,
Change the below line in your code

out.println("");


to the following and try again


out.println("");


not getting result , did that work for you .
Chuan Shao
Greenhorn

Joined: Aug 12, 2009
Posts: 5
1. Don't put your images under /WEB-INF, otherwise, the browser cannot access the image directory through hyperlink.

2. If you print <img src='/servlet/tomcat.gif' /> on page, you are telling the browser to fetch /${YourWebAppRoot}/servlet/tomcat.gif
tushar panda
Ranch Hand

Joined: Feb 16, 2009
Posts: 133
Chuan Shao wrote:1. Don't put your images under /WEB-INF, otherwise, the browser cannot access the image directory through hyperlink.

2. If you print <img src='/servlet/tomcat.gif' /> on page, you are telling the browser to fetch /${YourWebAppRoot}/servlet/tomcat.gif


i cannot understand ${YourWebAppRoot} . does it mean ROOT folder or a folder in root folder .

thanks brother got it
Chuan Shao
Greenhorn

Joined: Aug 12, 2009
Posts: 5
tushar panda wrote:
Chuan Shao wrote:1. Don't put your images under /WEB-INF, otherwise, the browser cannot access the image directory through hyperlink.

2. If you print <img src='/servlet/tomcat.gif' /> on page, you are telling the browser to fetch /${YourWebAppRoot}/servlet/tomcat.gif


i cannot understand ${YourWebAppRoot} . does it mean ROOT folder or a folder in root folder .

thanks brother got it


yes, to make things easy, you can take ${YourWebAppRoot} as ROOT folder in tomcat.
tushar panda
Ranch Hand

Joined: Feb 16, 2009
Posts: 133
Chuan Shao wrote:
yes, to make things easy, you can take ${YourWebAppRoot} as ROOT folder in tomcat.


if i create new folder under webapps , does i have to create a new WEB-INF folder inside it , or i can still access the default ROOT's WEB-INF folder .
Chuan Shao
Greenhorn

Joined: Aug 12, 2009
Posts: 5
tushar panda wrote:
if i create new folder under webapps , does i have to create a new WEB-INF folder inside it , or i can still access the default ROOT's WEB-INF folder .


The answer is yes, if you create a new folder under webapps, you create a new web application, which needs its own deployment descriptor(web.xml) under its own WEB-INF directory.
tushar panda
Ranch Hand

Joined: Feb 16, 2009
Posts: 133
one more thing :
does WEB-INF store only servlet files and nothing else

Shruthi Sam
Ranch Hand

Joined: Jul 09, 2008
Posts: 73
The structure of a Web Application will be like this..

WebApplicationName/

Place your static files, such as HTML files and JSP files in this directory (or a subdirectory). This directory is the document root of your Web Application.

/WEB-INF/web.xml

The Web Application deployment descriptor that configures the Web Application.

/WEB-INF/classes

Contains servlets and utility classes.

/WEB-INF/lib

Contains .jar files used by the Web Application, including JSP tag libraries.
Chuan Shao
Greenhorn

Joined: Aug 12, 2009
Posts: 5
tushar panda wrote:one more thing :
does WEB-INF store only servlet files and nothing else



Information from the Java Servlet Specification 2.4:
---

The contents of the WEB-INF directory are:
• The /WEB-INF/web.xml deployment descriptor.
• The /WEB-INF/classes/ directory for servlet and utility classes. The classes
in this directory must be available to the application class loader.
• The /WEB-INF/lib/*.jar area for Java ARchive files. These files contain servlets,
beans, and other utility classes useful to the Web application. The Web application
class loader must be able to load classes from any of these archive
files.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: image not displayed