<%@page contentType="text/html"%>
<%@page import="java.util.*,java.io.*"%>

<%!  int s; %> 
<%!  int f; %> 


<%
File dir = new File( "D:\\SUMIT GARG\\Excel" );
String[] files = dir.list();
Hashtable hs=new Hashtable();


for(int i=0;i<s.length;i++)
                    {
                        File v1= new File("D:\\SUMIT GARG\\Excel" +s[i]);
                            if(v1.isFile())

{
hs.put(f.lastModified(),file);
}
}


Vector v=new Vector(hs.keySet());
Collections.sort(v);
if(v.size()>10)
v.setSize(10);
Iterator it=v.iterator();
%>


<%
while(it.hasNext())
{
Long l=(Long)it.next();
String fname=(String)hs.get(l);
%>

<%=fname%> <%=new Date(l)%> 

<%
}
%>

plz correct the following code plzzzzzzzzzzzzzzzzz

Dani AI

Generated

This is JSP scriptlet code, not JavaScript — guessed correctly. is also right that putting this kind of logic in scriptlets is discouraged. The posted snippet has several concrete bugs: wrong/unused variables (s and f), building paths without a separator, using Hashtable keyed by lastModified() (keys can collide), calling dir.list() instead of listFiles() and never checking for null. Below is a minimal corrected scriptlet to quickly produce the 10 most-recent files; treat it as a stopgap fix and move this logic out of the JSP when possible.

<%
java.io.File dir = new java.io.File("D:\\SUMIT GARG\\Excel");
java.io.File[] files = dir.listFiles();
if (files != null) {
    java.util.Arrays.sort(files, new java.util.Comparator<java.io.File>() {
        public int compare(java.io.File a, java.io.File b) {
            long diff = b.lastModified() - a.lastModified();
            if (diff < 0) return -1;
            if (diff > 0) return 1;
            return a.getName().compareToIgnoreCase(b.getName());
        }
    });
    int max = Math.min(files.length, 10);
    for (int i = 0; i < max; i++) {
        java.io.File f = files[i];
        if (f.isFile()) {
            out.print(f.getName() + " " + new java.util.Date(f.lastModified()) + "<br/>");
        }
    }
}
%>

Notes and cautions: always check listFiles() for null (directory might not exist or lack permissions). Prefer new File(dir, name) when combining paths to avoid missing separators. The scriptlet above is quick-and-dirty; better options are to prepare a sorted List<File> in a servlet or controller and forward it as a request attribute, then render with JSTL/EL (no scriptlets). Use formatted dates (SimpleDateFormat or java.time) for output and escape filenames to avoid XSS. If multiple files can have the same timestamp, add a tie-breaker (name) as shown.

Recommended Answers

All 2 Replies

I'm going to feel really stupid if I'm wrong but is this even Javascript? It looks like ASP or something.

I'm going to feel really stupid if I'm wrong but is this even Javascript? It looks like ASP or something.

I dont think u are cause it definitely does not look anything like javascript, it is java scriptlets, which should not be used at all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.