Discussions

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

  1. Hi,

    ServletContext sc = config.getServletContext() ;
    java.net.URL 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
    Srinivas
  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.io.*;
    import java.util.*;
    import java.net.*;

    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 {
    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head><title>Servlet1</title></head>");
    out.println("<body>");
    out.println("<p>The servlet has received a GET. This is the reply.</p>");
    out.println("</body></html>");
    try {
    String path=getServletContext().getResource("/WEB-INF/classes/testpath/aa.properties").getPath();
    System.out.println(path);
    FileInputStream fin=new FileInputStream(path);
    }
    catch (MalformedURLException ex) {
    ex.printStackTrace();
    }

    }
    //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
    java.io.FileNotFoundException: E:/bea/wlserver6.1/config/mydomain/applications/.
    wlnotdelete/wl_comp33043.war/WEB-INF/classes/testpath/aa.properties (&#31995;&#32479;&#25214;&#19981;&#21040;
    &#25351;&#23450;&#30340;&#36335;&#24452;&#12290;
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:64)
    at testpath.Servlet1.doGet(Servlet1.java:32)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm
    pl.java:265)
    at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm
    pl.java:200)
    at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppSe
    rvletContext.java:2495)
    at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestIm
    pl.java:2204)
    at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:139)
    at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)
    >

    but when i open the directory i find that the correct directory is:
    E:/bea\wlserver6.1/config/mydomain/applications/.wlnotdelete/wl_comp41635.war!/W
    EB-INF/classes/testpath/aa.properties

    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
    Srinivas
  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:-

    enterprise/..
    properties/sql/..
    locale/..
    WEB-INF/..

    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
    Srinivas

  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