Salvatore Servodio
Salvatore Servodio

Reputation: 15

Making a new directory in WEB folder using servlet doesn't work

I am making an eBay style web application for buying and selling comic books using JSP and servlets. I am implementing the administrator's interface to manage the types of comic books a registered user can sell. When the administrator creates a new comic book series, e.g. Iron Man, Spider-Man etc, the servlet will create an instance in the database and then will create a new folder names according to the unique identifier of the new comic book series. So if I create a new comic book series lets say Hulk, and the database assigns the new insertion idCode 25, a new folder named "25" is made, and all future comic book cover images will be stored in this folder for all Hulk comic books.

That being said, my problem is that when everything is done, the servlet says that the directory has been created, but when I go to check I see that it is not the case.

Here is my project directory:

enter image description here

I am using the latest Apache Tomcat with NetBeans IDE 7.2.

Here is the code that creates the new folder:

else if (button.equalsIgnoreCase("Insert")) {
    if (tab.equalsIgnoreCase("Comic")) {
        String nameComic = request.getParameter("txtnameComicInsert");
        if (nameComic != null && nameComic.length() > 0) {
            Comic co = new Comic();
            co.setNomeCollana(nameComic);
            if (!SqlComic.existsComic(co.getComicName()) && SqlComic.insertNewComic(co) > 0) {                       
                sessione.setAttribute("msgVisitaTab", "Inserted successfully!");

                co = SqlCollana.getCollana(co.getNomeCollana());
                System.out.println("ACCEDI DATABASE SERVLET----> CREARE CARTELLA NELLA DIRECTORY: " +
                                    sessione.getServletContext().getRealPath("/") + "Immagini\\" + co.getCodice());

                File dir = new File(sessione.getServletContext().getRealPath("/").toString() + "Immagini\\" + co.getCodice());
                dir.mkdirs();

                if (dir.exists())
                    System.out.println("ACCESS DATABASE SERVLET----> FOLDER CREATED: " + co.getCodice() + " AT LOCATION: " + dir.getAbsolutePath());
                else
                    System.out.println("ACCESSDATABASE SERVLET----> FOLDER NOT CREATED!");
            } else {      
                sessione.setAttribute("msgVisitaTab", "Error DURING INSERTION!");                     
            }
        } else {
            sessione.setAttribute("msgVisitaTab", "FILL ALL FIELDS FOR INSERTION PROCESS TO TAKE PLACE![PROJECT DIRECTORY, all sub folders created must go in /Immagini/][1]");
        }
    }
}

So the code works perfectly, but nothing happens, and I can't seem to understand. What I did notice however is that looking at the property of Immagini folder, it has "read only" protection. I disable it but it keeps getting reactivates somehow.

Any idea what the problem could be?

Upvotes: 0

Views: 1688

Answers (1)

Bimalesh Jha
Bimalesh Jha

Reputation: 1504

Don't create folder inside your web-app as it might be removed (and recreated) when WAR file changes. I suggest below instead:

  • Use a well-defined place on the disk outside server's deployment dir e.g. c:\data\uploads\image\
  • Read this path from web.xml as servlet context parameter.
  • Implement a servlet context listener- onContextInitialized() check and create, if necessary, this folder.
    • to be absolutely sure that folder is ready, create a temp file in this directory and write some random bytes in it. If path does not exist IOException will be thrown with reason. Act appropriately.
  • You will have to give appropriate Runtime Permission to access/modify files by java code in this directory.
  • You can backup this directory (if needed) by external processes.

Hope this works for you.

Upvotes: 1

Related Questions