0

I'm creating a blogging system for a project (academic). I have a problem where I need to pass a map (LinkedHashMap) to a jsp file. But browser shows Nothing. Here's my code:

public void doService (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String path = "localhost:8080/YouBlogger/Posts/";
    File dir = new File(path);
    File [] files  = dir.listFiles();
    LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();

    Arrays.sort(files, new Comparator<Object>(){
        public int compare(Object o1, Object o2) {
            return compare( (File)o1, (File)o2);
        }
        private int compare( File f1, File f2){
            long result = f2.lastModified() - f1.lastModified();
            if( result > 0 ){
                return 1;
            } else if( result < 0 ){
                return -1;
            } else {
                return 0;
            }
        }
    });

    for(int i=0 ; i < 10 ; i++){
        map.put(files[i].getName(), files[i].getPath());
    }
    request.setAttribute("map", map);
    RequestDispatcher dispatcher = request.getRequestDispatcher("/welcome.jsp");
    dispatcher.forward(request, response);
}

I'm 100% sure that the error is in this code as when i hard code the map, jsp shows the data on it. Whats wrong with this ? Probably the path ??

Edit: This is my jsp code:

<body>
    <div id = "Header">         
        <h1>You Blogger</h1>
    </div>
    <div id = "data">
        <c:forEach var="country" items="${map}">
            ${country.key} + ${country.value}
            <br/>
        </c:forEach>
        <form action="new_post" method = "POST">
            <input type = "submit" value = "Add A New Post" ></input>
        </form>
    </div>
</body>

The project is running on apache tomcat 8.0 and I'm using eclipse Luna for developing.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ThisaruG
  • 3,222
  • 7
  • 38
  • 60

2 Answers2

1

The File constructor needs a real path to access the local directory. So you should not access it using localhost:8080, instead you should access the directory using the real path like below

 String path="/home/test/apache/webapp/projectname/YouBlogger/Posts/"

If you don't want to hard code the path you can use the request.getSession().getServletContext().getRealPath("/") to get the real path of the webserver directory

vjy
  • 1,184
  • 1
  • 10
  • 24
  • 1
    @BalusC the method `request.getSession().getServletContext().getRealPath` is used in many places of our existing project code to read file for app configuration. I am sure you must have a valid reason to avoid this method call, can you explain the reason for avoiding it ? – vjy May 12 '15 at 09:45
  • @BalusC thanks for the link, In our live setup WAR file will be always expanded, so we are able to read the file properly. – vjy May 12 '15 at 10:45
  • @BalusC Can you show me how to do so ? I tried something like this: `fileRd = new FileReader(getServletContext().getResourcePaths("/Posts/") + request.getParameter("file"));` But it returns `null` – ThisaruG May 12 '15 at 12:46
  • @BalusC But I want to read the data in the file, line by line. How to read it ? – ThisaruG May 12 '15 at 12:54
-1

I think that the problem lies on line 2 and 3 of your code:

String path = "localhost:8080/YouBlogger/Posts/";
File dir = new File(path);

What this will do is to look into directory c:/localhost:8080/YouBlogger/Posts/ and list files there, and as this directory probably does not exist on your drive c: it will return nothing. If you want to list files on a remote machine you need to use some HTTP Client or put a path to existing directory there.

atom
  • 1
  • 1
  • I'm running this on a server (apache tomcat). And the parameter for the `File` constructor should be an **absolute** path. – ThisaruG May 11 '15 at 23:05