aspose file tools*
The moose likes Servlets and the fly likes read/write files from Tomcat web.xml? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "read/write files from Tomcat web.xml?" Watch "read/write files from Tomcat web.xml?" New topic
Author

read/write files from Tomcat web.xml?

Kate Head
Ranch Hand

Joined: Oct 31, 2002
Posts: 67
Hi there,
I'm not sure if this is the right forum, so please move it to the right one if it's not, but I'm trying to read an Integer object from a file, convert to an integer, increment by 1 and then write back as Integer object to the file. I was trying to use baseTag/orderNumData, where baseTag is my context for Tomcat, but I got a "error in closing outfile; NullPointerException" on closing the file.
Any ideas/suggestions - please move this if this is not the correct forum.
Confused!
Kate!!
(apologies to Jason Adam - I've got stuck with my .jsps!)
Kate Head
Ranch Hand

Joined: Oct 31, 2002
Posts: 67
javascript: x()PS baseTag and/or Context = http://localhost:8080/khead/ in case you were wondering...
All the best,
Kate!!
Bob Kerfoot
Ranch Hand

Joined: Oct 01, 2000
Posts: 47
Kate,
What happened when you opened or tried to read from or write to the file?
Was an exception thrown at any of these points?
Also, what kind of input/output streams are you using?
Bob Kerfoot
SCJP 1.2
SCWCD
Kate Head
Ranch Hand

Joined: Oct 31, 2002
Posts: 67
Hi there Bob,
Here's the method I'm using:
"public static void startOrders() {
FileInputStream fis = null;
ObjectInputStream objInputStr = null;
Integer OrderNum = null;
String fileName = baseURL + "/orderNumData";
try {
fis = new FileInputStream(fileName);
objInputStr = new ObjectInputStream(fis);
OrderNum = (Integer)objInputStr.readObject();
}
catch (Exception eX) {
if (OrderNum == null) { // probably first time through...
OrderNum = Integer.valueOf("0");
}
}
finally {
try {
if (objInputStr != null) {
fis.close ();
} // end if
}
catch (IOException ioe) {
exception = "startOrders: error on closing infile; " + ioe;
}
} // end finally
int checkNum = OrderNum.intValue();
orderNum = ++checkNum;
OrderNum = new Integer(orderNum);
FileOutputStream fos = null;
ObjectOutputStream objOutputStr = null;
try {
fos = new FileOutputStream(fileName);
objOutputStr = new ObjectOutputStream(fos);
objOutputStr.writeObject(OrderNum);
objOutputStr.flush();
}
catch (Exception eX) {
exception = "startOrders: error in writing file; " + eX;
}
finally {
try {
fos.close();
}
catch (Exception eX) {
exception = "startOrders: error in closing outfile; " + eX;
}
}
}"
where baseTag has come from the web.xml file (http://localhost:8080/khead)
The error I get is "startOrders: error in closing outfile; java.lang.NullPointerException". The file does not exist to start with and should be created when the file is written, but this would appear not to be happening!
Any suggestions (apart from give up now!)?
All the best (from the UK - it's raining after weeks of sun),
Kate!!
Bob Kerfoot
Ranch Hand

Joined: Oct 01, 2000
Posts: 47
Kate,
I think it may be a problem with your path resolution. I ran your code on my machine with a bad path "/orderNumData" and was also unable to create the new file. However, after using
String fileName = getServletContext().getRealPath("/orderNumData");
all worked as expected and I created the new file "orderNumData" in my application context root directory.
$CATALINA_HOME/webapps/applicationContextRoot/orderNumData
You can adjust your file location below the context root by adding directory info as needed. For example,
String fileName = getServletContext().getRealPath("/myDir/orderNumData");
will create the new file as
$CATALINA_HOME/webapps/yourApplicationContextRoot/myDir/orderNumData
Let me know if this takes care of the problem.
Bob Kerfoot
Kate Head
Ranch Hand

Joined: Oct 31, 2002
Posts: 67
Bob, you're brilliant - problem solved and my ordernumbers are working beautifully now.
Many thanks.
Kate!!
Bob Kerfoot
Ranch Hand

Joined: Oct 01, 2000
Posts: 47
Glad I could help.
Bob Kerfoot
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15662
    
  15

Don't expect that to work everywhere and everytime. Between implementation dependencies and the fact that webapps can reside in one or more JAR files (which not all appservers may unpack), you may blow up without warning.
The best way to get a directory path for a webapp to use is to pass an absolute path in from outside. This can be done in a resource file.
PLEASE NOTE: A Web Sever is not a file server
A URL may look like a file path, and it often acts like one, but it's not a file path.


Customer surveys are for companies who didn't pay proper attention to begin with.
Bob Kerfoot
Ranch Hand

Joined: Oct 01, 2000
Posts: 47
Tim is correct in that
getServletContext().getRealPath(java.lang.String path)
will return null if the webapp is deployed in a WAR file.
However, as always, specific knowledge of the deployment environment is required whether you hard code the absolute path in the deployment descriptor and/or the program or use getServletContext().getRealPath("/resource");
Specifically, if you change your directory structure or redeploy the webapp to a new machine with different drive letters then your file processing can break as well.
Therefore, as always, there is no magic bullet. Specific knowledge of and testing for your actual deployment environment equals quality production deployments.
Bob Kerfoot
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read/write files from Tomcat web.xml?
 
Similar Threads
Access GUI from Tomcat localhost
Resource Jar's
Problem with csv file
Byte Array returned from ByteArrayOutputStream.
Linux / Tomcat / Samba / Properties question