Reputation: 15
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:
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
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:
onContextInitialized()
check and create, if necessary, this folder.
Hope this works for you.
Upvotes: 1