servletContext.getResource("/") method is giving null ???


Web tier: servlets, JSP, Web frameworks: servletContext.getResource("/") method is giving null ???

  1. Hi,

    ServletContext sc = config.getServletContext() ; url = sc.getResource("/");

    The above code is in init() method of my servlet. Here problem is that 'url' is coming 'null', i mean if try to give url.toString(),
    i am getting NullPointerException.

    I am testing this code in Weblogic 6.1.
    and my servlet is packed in a .war file.

    And i can't use getRealPath() method, as this method doesn't work if servlet is packed in .war file.

    please help me

    thanks in advance
  2. hello
    i also write a program to test the "getResource(....)" method,see my code:

    package testpath;

    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;

    public class Servlet1 extends HttpServlet {
    static final private String CONTENT_TYPE = "text/html; charset=GBK";
    //Initialize global variables
    public void init() throws ServletException {

    //Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("<p>The servlet has received a GET. This is the reply.</p>");
    try {
    String path=getServletContext().getResource("/WEB-INF/classes/testpath/").getPath();
    FileInputStream fin=new FileInputStream(path);
    catch (MalformedURLException ex) {

    //Clean up resources
    public void destroy() {

    when i debug it within the developing environment(jbuilder7+weblogic6.1),it works well,but when i pack it as war file and deploy it to weblogic server,it output as follow:

    <2002-8-30 &#19979;&#21320;01&#26102;49&#20998;59&#31186;> <Error> <HTTP> <[WebAppServletContext(5996995,testp
    ath,/testpath)] Servlet failed with IOException E:/bea/wlserver6.1/config/mydomain/applications/.
    wlnotdelete/wl_comp33043.war/WEB-INF/classes/testpath/ (&#31995;&#32479;&#25214;&#19981;&#21040;
    at Method)
    at testpath.Servlet1.doGet(
    at javax.servlet.http.HttpServlet.service(
    at javax.servlet.http.HttpServlet.service(
    at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm
    at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm
    at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppSe
    at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestIm
    at weblogic.kernel.ExecuteThread.execute(

    but when i open the directory i find that the correct directory is:

    the weblogic add a "!" to the path,if i remove the "!",it still can't find the path,but when i use "getResourceAsStream(path)",it works well,why?

    i think it's a bug of weblogic?

    who have the similar experience?

  3. HI All,
    Still i am not able to get real path.
    I have tried by using getResource("index.htm") method, but could not succeded, because, if i use getResouce("/index.htm") method, the path is prefixed by 'zip:', suffixed by '!/index.htm'. Once i get that path sring, i removed, 'zip:', and '!/index.htm' from path string, and tried. When i try to concat path string with a existing file name, and try, still in am getting, 'FileNotFoundException'.

    If anybody suggest me, they will be appreciated.
    Thanks in Advance
  4. Hi,

    Actually i don't want the resource, i want the real path, i mean root document.
    I my EAR file, i have only one WAR file. In my WAR file the following are the folders:-


    I my application 'real path' is used in no.of times. Path is used in the following way in our application:-

    File emailTemplatesFolder = new File( path + "/enterprise/"+ enterpriseCode+"/EmailTemplates");

    If i use getRealPath() method, it works fine, in use Oracle9ias, because, EAR file will be extracted. Where as in Weblogic 6.1, EAR file willn't be extracted, so that getRealPath() is giving 'null', that is reason why i am seeking for alternative.

    Thanks in Advance

  5. for those who visit this page[ Go to top ]

    weblogic has the option to deploy the war file as unpackaged ,then use get real path