This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes how image path should be passed to servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how image path should be passed to servlet" Watch "how image path should be passed to servlet" New topic
Author

how image path should be passed to servlet

Neelima Paramsetty
Greenhorn

Joined: Jun 17, 2003
Posts: 16
Hi,
i am following the Orelliey publication for java servlet .in that image should be taken from the URL.
the code is like this
public class imgoverriding extends HttpServlet
{
Frame f1=null;
Graphics g=null;
public void init(ServletConfig con) throws ServletException
{
super.init(con);
f1=new Frame();
f1.addNotify();


}

public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("img/gif");
ServletOutputStream out=res.getOutputStream();
try{
String source = req.getPathTranslated();
if (source == null) {
throw new ServletException("Extra path information " +
"must point to an image");
}

Image img=Toolkit.getDefaultToolkit().getImage(source);
MediaTracker mt=new MediaTracker(f1);
try{
mt.waitForAll();
}
catch(InterruptedException e)
{
getServletContext().log(e,"interrupted during the image downloading");

}
int w=img.getWidth(f1);
int h=img.getHeight(f1);
Image img1=f1.createImage(w,h);
g=img1.getGraphics();
g.drawImage(img,0,0,f1);
g.setFont(new Font("Monospaced",Font.ITALIC,30));
g.drawString("Neelima",0,0);
GifEncoder encoder = new GifEncoder(img1, out);
// GifEnCoder gif2=new GifEncoder(img1,out);
encoder.encode();
}
finally
{
if(g!=null) g.dispose();
if(f1!=null) f1.removeNotify();
}
}
}
the image is taken throgh the method req.getPathTranslated().image is in my localsystem.how to send that image to getPathTranslated().
thanks in advance,
Neelima
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Its always good practise to write your code inside code tag like..

I agree, font size sucks but still its better than losing your formatting.
Though I dont know anything abt Image class but I am just guessing that your image file should be in the same folder where your servlet is residing and then you should append your image file name to source.
Something like this should work..
source = source + "/" + "imageFileName.gif" ;
This should be done before this statement
Image img=Toolkit.getDefaultToolkit().getImage(source);
I think best place will be in the else block of this

HTH
try and tell the result


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Ravish Kumar:
Though I dont know anything abt Image class but I am just guessing that your image file should be in the same folder where your servlet is residing and then you should append your image file name to source.
Something like this should work..
source = source + "/" + "imageFileName.gif" ;

I disagree. Placing the image in the same folder as the servlet is no guarantee of anything.
My prefernce for finding resources associated with web applications is to make the resource available on the web app classpath and use the web app ClassLoader to find it. This way it is not file system dependent and is therefore portable.
Place the image in the web-inf/classes directory and try

Dave
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by David O'Meara:
[QB]I disagree. Placing the image in the same folder as the servlet is no guarantee of anything.

I agree with you.
I said this with ref to the given code.
This was the easist way to load the image with the given code where one wants to make use of req.getPathTranslated();
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how image path should be passed to servlet
 
Similar Threads
Applet(with images) running problem at Client
Retrive Images in Binary from Database and write to a gif or jpg
Image
loading image
single dimension array to multi dimension