Michael Caines wrote:

> Is there a way for a servlet to alter and save a Properties object to a
> file on the server?  I want to allow the user to make changes to a
> profile (a .properties file on the server), and have them persist on the
> server without using a database.
>
> Here are the things I'm looking at:
>
> The java.util.Properties class has methods for loading and saving a
> Properties object from a stream, but no setter method for changing
> individual properties.
>

Umm, what's wrong with put(Object key, Object value)?
It is inherited from Hashtable, which Properties is a subclass of.

>
> sun.servlet.http.HttpServer::loadProperties()  No saveProperties()
> method, plus same problems as above once you get the Properties object.
> Also, it's not part of the official Java spec.
>

My feelings about using internal sun.* classes are probably obvious from my
previous postings :-).

>
> There is the java.io.File class.  This usually results in coding some
> system-dependent filenames which I would like to avoid if possible.  I
> could also scrap the Properties approach and create my own flat-file for
> storing profiles.  However, I'd like to use a .properties file, since it
> seems naturally suited for the task.
>

There's no problem using properties files, because you've got
Properties.save() to write them out for you.  I do this to a FileOutputStream
all the time.

The problem, of course, is that you have to know where to store them --
probably configured via some initialization parameter to your servlet.  But
that applies to knowing where to read them from as well, if you are using the
loadProperties() method.

>
> 1) java.lang.Class::getResource() is read-only
> 2) ServletConfig::getServletContext().getResource() is read-only
> Jason Hunter's article in JavaWorld mentions using a resource for output
> as well.  This is done by taking the resource's URL object, creating a
> URLConnection object via URL::openConnection(), invoking
> setDoOutput(true) on the URLConnection, and using
> URLConnection::getOutputStream() to open an output stream to the
> resource.  I haven't tried writing to a .properties file with number 2
> above, but trying it with number 1 in some test code resulted in a
> "protocol doesn't support output" Exception.  Maybe number 2 would be
> able to work by using the ServletClassLoader, and not the
> SystemClassLoader--I'm not sure.
>

Whether output is supported will depend on the URL that your servlet engine
uses as the base for the ServletContext.getResource().  Java uses the scheme
part of the URL ("http:", "ftp:", or whatever) to select an underlying
URLStreamHandler implementation class, which might or might not implement
output.  The details will really depend on which servlet engine you are using,
and what flexibility you have in providing your own URLStreamHandler if the
existing ones don't help.

>
> java.util.PropertyResourceBundle::getBundle()  read-only
>

This is read-only as you noted, but it can read in a properties file created
by one of the techniques discussed earlier.

>
> There is a com.sun.server.util.ExProperties class, which is a direct
> subclass of java.util.Properties.  It adds functionality for setting
> properties, but it's not part of the official Java spec.  If there is a
> method of doing this that uses only java.* classes, I would prefer to
> use it.
>

See above -- java.util.Properties.put() works just fine.

>
> Michael
>

Craig McClanahan

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to