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.