0

I have a servlet that uses another class for reading .XML file, here is the servlet simple doGet() :

private checkDBStatus;
public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      checkDBStatus = new checkDBStatus();
      String returnText = checkDBStatus.getText();
      // Set response content type
      response.setContentType("text/html");

      // Actual logic goes here.
      PrintWriter out = response.getWriter();
      out.println(returnText);
  }

The file path in checkDBStatus is XMLS/DB.xml, when running on a Java class like Program(main) checkDBStatus works fine with the right path, but when a Get request is handled in servlet class I get FileNotFoundException with file path \IBM\SDP\XMLS\DB.xml (this directory was not found in my file system as well, I have IBM RAD). Anyone had encounter this problem? Here is the code that reads thr .xml:

File fXmlFile = new File("XMLS/DB.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile); // the rest is just parsing...
Itsik Mauyhas
  • 3,824
  • 14
  • 69
  • 114
  • 1
    Can you post the code where it reads the xml file? You schould trying to debug checkDBStatus where it try to read the xml file.. – sgpalit Feb 24 '16 at 12:08
  • Posting the code inside getText. – Itsik Mauyhas Feb 24 '16 at 12:10
  • XMLS/DB.xml is a **relative** file path. So it looks for a file XMLS/DB.xml from the **current** firectory. The current directory is the directory from which the java command used to stert your web server was executed. Using a relative file path is a bad idea. Use an absolute file path if the data should really be loaded from the file system, or embed it with your app, and use Class.getResourceAsStream() or ServletContext.getResourceAsStream(). – JB Nizet Feb 24 '16 at 12:14
  • is DB.xml in your project resources directory in folder XMLS? If yes you schould use here getResource, so you can get the full path to create the File – sgpalit Feb 24 '16 at 12:15
  • is your `XMLS` directory in webcontent Folder or any where else? is your project uses maven based build or any other? – Vikrant Kashyap Feb 24 '16 at 12:17
  • @JBNizet - trying and posting the result, sgpalit - yes its on the project directory, Vikrant Kashyap - No, its on the project directory and I dont use Maven. – Itsik Mauyhas Feb 24 '16 at 12:21

0 Answers0