Search code examples
javajspjstlfile-handlingscriptlet

How to convert scriptlets into jstl tags


I tried turning these scriptlets into jstl tags with no success, is it not doable with these lines of codes, and if it can how so? thanks

<% 
        //String file = application.getRealPath("C:/science/"); 

        File f = new File("C:/uploads");
        String [] fileNames = f.list();
        File [] fileObjects= f.listFiles();
    %>
    <UL>
    <%
        for (int i = 0; i < fileObjects.length; i++) {
            if(!fileObjects[i].isDirectory()){

    %>
    <LI>
        <A HREF="DownloadServlet?value=<%= fileNames[i] %>"><%= fileNames[i] %><u> Download</u></A>
      <%= fileNames[i] %>
      &nbsp;&nbsp;&nbsp;&nbsp;
      (<%= Long.toString(fileObjects[i].length()) %> bytes long)

    <%
            }
        }
    %>

Solution

  • All that code you have in the scriptles you have to do it in the java code and pass it to the jsp. I don't know if you are using any framework to do this but this is easy in frameworks like Spring MVC.

    In your case you should create in java a bean with the properties you need such as isDirectory, length, name of the file, etc. Then you create a list of these beans and pass it to the jsp. Finally, you just iterate these list of beans in the jsp with a forEach loop of JSTL (http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/forEach.html). You can google about it and you'll find lots of examples about how to do this.

    Doing this you don't need to use scriptlets.