Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

problem in loading image  RSS feed

 
fawad ahmed
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i can not load image i have puted in the images folder.gives null pointer exception
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.net.*;
public class SetCookie extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
{
res.setContentType("image/gif");
ServletOutputStream sop=res.getOutputStream();
System.out.println(getServletContext().getResourceAsStream("c:/tomcat2/webapps/examples/images/code.gif"));
int ival=is.read();
while(ival!=-1)
{
sop.write(ival);
ival=is.read();
}
}
}
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the javadocs for ServletContext getResource, the URL must begin with "/" and is interpreted relative to the web application context root. I think getResourceAsStream would work with "/images/code.gif" since the context is examples.
You have given a complete path - for that you would need to open a FileInputStream.
Bill
------------------
author of:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!