Map filled with file names and set as request attribute by servlet appears empty in JSP -
i'm creating blogging system project (academic). have problem need pass map
(linkedhashmap
) jsp
file. browser shows nothing. here's 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 ; < 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 error in code when hard code map
, jsp shows data on it. whats wrong ? path
??
edit: 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 new post" ></input> </form> </div> </body>
the project running on apache tomcat 8.0 , i'm using eclipse luna developing.
the file
constructor needs real path access local directory. should not access using localhost:8080
, instead should access directory using real path below
string path="/home/test/apache/webapp/projectname/youblogger/posts/"
if don't want hard code path can use request.getsession().getservletcontext().getrealpath("/")
real path of webserver directory
Comments
Post a Comment