Search code examples
javaservletsresourcesjettygetresource

Jetty servlet override extension reading file



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?


Solution

  • 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.