0


I'm trying to make a servlet on Jetty that overrides a file extension but that still needs to read the file being accessed.
I've been trying with resources but I could achieve nothing yet. I've tryed this code so far and, as you'll see, the resources are there but I somehow can't access them:

package valarionch.lab0.webapp.todo;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
@WebServlet(urlPatterns = { "*.ToDo" })
public class ToDoHandler extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    resp.setContentType("text/html");
    String s = req.getParameter("s");
    boolean small = (s != null && s.equals("1"));
    PrintWriter out = resp.getWriter();

    if (!small) {
        out.println("<html><head><title>ToDo list</title></head>"
                + "<body>");
    }

    for (String res : getServletContext().getResourcePaths("/")) {
        System.out.println("Resource: " + res);
        System.out.println("ResourceURL: " + getServletContext().getResource(res));
        System.out.println("ResourceStream: " + getServletContext().getResourceAsStream(res));
    }


    InputStream input = getServletContext().getResourceAsStream(req.getRequestURI());
    System.out.println(input);

    ToDoFormatter.parse(input, out, req.getParameter("q"));

    if (!small) {
        out.println("</body></html>");
    }
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    doGet(req, resp);
}
}

this code prints this:

Resource: /META-INF/
ResourceURL: null
ResourceStream: null
Resource: /WEB-INF/
ResourceURL: null
ResourceStream: null
Resource: /index.html
ResourceURL: null
ResourceStream: null
Resource: /ToDoList.ToDo
ResourceURL: null
ResourceStream: null
null

I tryed with the next code too but also didn't worked:

getClass().getClassLoader().getResource(".").toString()+"../.."+req.getRequestURI()

so getClass().getClassLoader().getResource(".").toString() goes to WEB-INF/classess and +"../.."+req.getRequestURI() picks the actual file.

Am I missing something about how resources work? Is there another way to read the file?

valarion
  • 45
  • 3

1 Answers1

0

You can use getServletContext().getRealPath() for such task. Let's imagine that you have the file myText.txt in the webapps folder:

   @SuppressWarnings("serial")
   @WebServlet(urlPatterns = { "*.ToDo" })
   public class UseGetRealPath extends HttpServlet {
    public void doGet( HttpServletRequest req, HttpServletResponse res )
      throws ServletException, IOException {
     String todoFile = getServletContext().getRealPath("/myText.txt");
     FileReader fr = new FileReader( todoFile );
     for( int c = fr.read(); c != -1; c = fr.read() ) {
      System.out.print( (char) c );
     }
     fr.close();
     res.getWriter().println( "check the console!" );
    }
   }

The code will open the file and dump its content in the console.

  • That didn't work either, so it made me think that the problem had to be somewhere else, so I checked again and that's where i realized... the server was being executed from a non-ascii path, so I moved the project and now it works like a charm. – valarion Oct 06 '14 at 18:15
  • Anyway, don't use `../..` (dot-segments) in your web projects. If you need to access code outside the `/WEB-INF/class` use paths relative to the root of your project. – Francisco J. Lopez-Pellicer Oct 07 '14 at 07:17