Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how image path should be passed to servlet

 
Neelima Paramsetty
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic