//package com.vistech.internet;
import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;

public class ListImagesServlet extends HttpServlet{
   public void doGet(HttpServletRequest req, HttpServletResponse res)
                  throws ServletException{
      try {
        res.setContentType("text/html");
        String dir = req.getPathTranslated();
        String fileList[] = listFiles(dir);

        PrintWriter out = res.getWriter();
        out.println("<HTML>");
        out.println("<HEAD><TITLE>File list</TITLE></HEAD> ");
        out.println("<FORM ACTION=\"/examples/servlet/SimpleImageLoaderServlet\" method=\"Get\">");
        out.println("<BODY>");
        out.println("<TABLE border=1 align=\"left\">");
        out.println("<TR><TH>ID</TH><TH>Image</TH></TR>");
        HttpSession session = req.getSession(true);
        if(session != null) session.putValue("imagepath", dir);

        for(int i=0;i<fileList.length;i++) {
            out.println("<TR><TD>");
            out.println(i);
            out.println("</TD>");
            out.println("<TD>");
            out.println("<INPUT TYPE=\"RADIO\" name=\"images\" ");
            out.println("value=\""+fileList[i]+"\" >"+fileList[i]);
            out.println("</TD>");
            out.println("</TR>");
        }
        out.println("</TABLE>");
        out.println("<INPUT TYPE=\"SUBMIT\" VALUE=\"DISPLAY\">");
        out.println("<FORM>");
        out.println("</BODY></HTML>");
      } catch(Exception e) {System.out.println(e);}
   }
   public synchronized String[] listFiles(String dirstr) {
      String fl[] = null;
      try {
         File dir = new File(dirstr);
         fl = dir.list();
      } catch (Exception e){System.out.println(e);}
      return fl;
   }
}
