java properties file problem

M

Marcin Go³dyn

Hello
I have an application which save own state to a properties files after user
logoff. It works when I did it on my computer on tomcat included in eclipse
IDE but it didnt work(dont save file) when i try deploy application on other
computer on ordinary tomcat. Tomcat doesnt let overwrite files or what??
Pls help
thx in advance
Marcin Goldyn
 
R

Robert Klemme

Marcin said:
Hello
I have an application which save own state to a properties files
after user logoff. It works when I did it on my computer on tomcat
included in eclipse IDE but it didnt work(dont save file) when i try
deploy application on other computer on ordinary tomcat. Tomcat
doesnt let overwrite files or what?? Pls help
thx in advance
Marcin Goldyn

Normally web applications do not have access to the file system. It's a
security feature. If you can (i.e. are allowed to) change the config of
Tomcat, do so to allow file system access. It might also be a problem
with user permissions if the OS user that runs Tomcat.

Kind regards

robert
 
M

Marcin Goldyn

yes but i thouht that webapps have access to file system in tomcat
directory. I`m not an expert in tomcat server administration. Can u write me
what should i do to make it works??
thx in advance
Marcin Goldyn
 
B

Bryce

yes but i thouht that webapps have access to file system in tomcat
directory. I`m not an expert in tomcat server administration. Can u write me
what should i do to make it works??
thx in advance
Marcin Goldyn

What have you written to date that works?

What's the error you are getting?


Note: When posting a question on usenet, its best to:

1. Describe the problem
2. Show us what you did (i.e., complete compilable code).
3. Tell us the error you are getting.
4. Tell us what you are expecting to happen.

You will find if you are explicit like this, you will get better
responses. Otherwise, you have to keep going back and forth asking and
answering questions.
 
M

Marcin Goldyn

What have you written to date that works?
package praca.app;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public final class LogoffAction extends Action
{
/** funkcja ta wykonuje akcje zamkniecia sesji
* @param mapping akcja
* @param form przypisany do klasy ActionForm
* @param request zapytanie
* @param response odpowiedz
*/
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

//pobranie obiektu z sesji
HttpSession session = request.getSession();
LogonForm user = (LogonForm)
session.getAttribute(Constants.USER_KEY);

//logowanie akcji do servera aplikacji
if (user != null) {

if (servlet.getDebug() >= Constants.DEBUG) {
StringBuffer message =
new StringBuffer("LogoffAction: Uzytkownik '");
message.append(user.getUsername());
message.append("' wylogowal sie z sesji numer: ");
message.append(session.getId()+" adres IP klienta:
"+request.getRemoteAddr());
servlet.log(message.toString());
}
}

else {

if (servlet.getDebug() >= Constants.DEBUG) {
StringBuffer message =
new StringBuffer("LogoffAction: Uzytkownik '");
message.append(session.getId());
servlet.log(message.toString());
}
}
//zapisanie danych z obiektow sesji do plikow typu properties
Properties properties = new Properties();
Properties properties2 = new Properties();
Hashtable ht =
((PrawaAll)session.getAttribute("PrawaAll")).getPrawa();
Hashtable ht2 = ((Users)session.getAttribute("Users")).getUsers();
Enumeration emu = ht.keys();
while(emu.hasMoreElements())
{
String iterator = (String) emu.nextElement();
properties.put(iterator,ht.get(iterator));

}
emu = ht2.keys();
while(emu.hasMoreElements())
{
String iterator = (String) emu.nextElement();
properties2.put(iterator,ht2.get(iterator));

}
try {
properties.store(new FileOutputStream("access.properties"),
null);
properties2.store(new FileOutputStream("users.properties"),
null);
} catch (Exception e) {
System.out.println(e.getMessage());
}
//usuwanie obiektow z sesji
session.removeAttribute("PrawaAll");
session.removeAttribute("Users");
session.removeAttribute(Constants.USER_KEY);
session.invalidate();


return (mapping.findForward(Constants.SUCCESS));

}

}


this code works on built in eclipse tomcat but dont work on ordinary tomcat
server
What's the error you are getting?

i dont get any errors in tomcat logs neither in localhost log nor stdout log
Note: When posting a question on usenet, its best to:

1. Describe the problem

I have an application which save own state to a properties files after user
logoff. It works when I did it on my computer on tomcat included in eclipse
IDE but it didnt work(dont save file) when i try deploy application on other
computer on ordinary tomcat. Tomcat doesnt let overwrite files or what??
Pls help
2. Show us what you did (i.e., complete compilable code).

class above
3. Tell us the error you are getting.

like i wrote above i dont get any
4. Tell us what you are expecting to happen.

i want to save a file or overwrite a file in tomcat webapp directory
 
A

Andrew Thompson

package praca.app; ....
try {
properties.store(new FileOutputStream("access.properties"),
null);

Where is this file being created?
Actually, that is a (slightly) rhetorical question.

When you do not understand what is happening in code,
don't take these sorts of shortcuts, instead..

// something like this..
String filePath = ServletContext.getRealPath("access.properties");
System.out.println( filePath );
File accessProperties = new File(filePath);
System.out.println( accessProperties.exists() );
FileOutputStream output = new FileOutputStream( accessProperties );
System.out.println( "Outputstream opened.." );
properties.store( output, null );
properties2.store(new FileOutputStream("users.properties"),
null);
} catch (Exception e) {
System.out.println(e.getMessage());
}
//usuwanie obiektow z sesji

That comment is not very useful to me. :-(
session.removeAttribute("PrawaAll");

Why are these statements outside the 'try' above..?
session.removeAttribute("Users");
session.removeAttribute(Constants.USER_KEY);
session.invalidate();


return (mapping.findForward(Constants.SUCCESS));

And especially this one! Surely you should not return
Constants.SUCCESS here if you have jusst caught an
exception?
}

}
.....

i want to save a file or overwrite a file in tomcat webapp directory ..

Do you mean 'webapps'? The directory that contains the
web site's ROOT directory?
 
A

Andrew Thompson

.....
(Bryce, earlier)
Do you mean 'webapps'? The directory that contains the
web site's ROOT directory?

BTW - try this..

String filePath = ServletContext.getRealPath("/");
System.out.println( filePath );
 
M

Marcin Goldyn

thx Andrew for this letter. I know now where this file is stored but it
still dont resolved my problem. How can i get path to Tomcat directoery
where webapps are stored. There are different directories with tomcat server
where people may deploy my application so i need a idea how to get this path
and make my application universal.
 
B

Bryce

thx Andrew for this letter. I know now where this file is stored but it
still dont resolved my problem. How can i get path to Tomcat directoery
where webapps are stored. There are different directories with tomcat server
where people may deploy my application so i need a idea how to get this path
and make my application universal.

Look at the properties of ServletContext. Andrew gave you the
appropriate code above.
 
R

Roedy Green

I have an application which save own state to a properties files after user
logoff. It works when I did it on my computer on tomcat included in eclipse
IDE but it didnt work(dont save file) when i try deploy application on other
computer on ordinary tomcat. Tomcat doesnt let overwrite files or what??

If there is an Applet involved, an unsigned Applet won't let you save
properties on local hard disk.

You might be interested in exploring the Java 1.5 Preferences class
with is designed to replace the properties class. The idea is it is
not your problem to figure out where to store the persistent data.

When you tried to do the save, what did the stack track say? That is
what you need to figure out why the file is not getting written.
 
A

Andrew Thompson

thx very much

You're welcome, but I feel I am getting 'undue credit'.
99% of this solution came from Bryce and yourself[1].

I just wandered by at a lucky moment, when the vital
evidence was laid bare for all to see.

Glad you sorted it.

[1] Bryce took the time and effort to spell out what we needed
to know, and you had the good sense to provide it.
 
R

Raymond DeCampo

Roedy said:
If there is an Applet involved, an unsigned Applet won't let you save
properties on local hard disk.

You might be interested in exploring the Java 1.5 Preferences class
with is designed to replace the properties class. The idea is it is
not your problem to figure out where to store the persistent data.

Unfortunately, the Preferences class (which has been around longer than
1.5) does not allow an applet to access preferences without being
signed. An unfortunate decision on Sun's part.

(If you need evidence, google for a similar conversation where I played
the role you are in now.)
When you tried to do the save, what did the stack track say? That is
what you need to figure out why the file is not getting written.

Ray
 
R

Roedy Green

I have an application which save own state to a properties files after user
logoff. It works when I did it on my computer on tomcat included in eclipse
IDE but it didnt work(dont save file) when i try deploy application on other
computer on ordinary tomcat. Tomcat doesnt let overwrite files or what??
Pls help

Another possible explanation is that in Tomcat the file got written
somewhere else entirely. You might do a global search for a file of
that name or use absolute filenames.
 
R

Roedy Green

Unfortunately, the Preferences class (which has been around longer than
1.5) does not allow an applet to access preferences without being
signed. An unfortunate decision on Sun's part.

I should know that since I used the Preferences class in a signed app
I wrote back in 2003, the Replicator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top