Manage State in Desktop Application? - java

i wonder session between Web Application and Desktop Application.
In web applications we can use Session or cookies(or HTTP Session - Stateful if use EJB), but in Desktop Application how can i manage state of object? (if not use Stateful ), i usually use managed state in text file or xml file, it mean when user login , user information will save in text or xml file for managed state and when user logout, system will delete text or xml file. I need some suggest.

I think that you mix the concepts. You should not need to hold user information for desktop application, because your application has only one user instead of many users of a web application. Your application already knows who is its user..
But if you need to save user preferences about your application, as expressed at previous post , you can use The Preferences api, or just you can save to a text file.

The Preferences API is probably what you're after (I think): java.util.prefs

The java.util.pref.Preferences class, which was added in Java 1.4 is used to store and get persistent (remains on disk between program executions) hierarchical name/values pairs.These preferences are stored in an operating system dependent manner, e.g in the Windows registry or a Mac preferences file.
So by using Preferences API, same thing is going to happen file I/O etc which you are doing already but will be in a better-manageable way.
Instead, if possible you can try using Map, accessed by respective methods for insertion/retrieval of user state through a singleton class.

Related

One User can access a method at a time in jsf+java+jsp in dynamic web project

I have one method which will create a file and transfer it to other machine in java.
It is a dynamic web project , java , jsp and jsf has been used for the same.
The problem is The application can be accessed by different users at a time, But for every user I am using one method which changes something in a file a transfer it to different machine .The problem is if at a time some other user logged in and changes the file then the previous user will get the wrong file.
I want to put that method as synchronized so that at a time only one user can access the same method .
Please suggest me to handle concurrent user in dynamic web project in java .
I am using JSF
First of all take in account that each user request is handled by the server on a separate thread so you are in a multi-thread environment even if you do not create threads in your code.
If possible I would create a different instance of the object, able to manage the file, for each user/request. In this way you will be sure that different users will not overwrite/change settings.

Java How do you use the preference API? Where do these variables store?

Say I have the following
Preferences prefs = Preferences.userRoot().node(this.getClass().getName());
String ID1 = "Test1";
System.out.println(prefs.getBoolean(ID1, true));
prefs.putBoolean(ID1, false);
//prefs.remove(ID1);
Is this variable persistent the next time I execute my program?
Where do these variables store?
What is the proper way of utilizing this?
Is the approach better than using properties files?
Yes, the value is persistent but only for the user. It won't be there for other users.
This is OS specific. For Windows it uses the registry, for Linux I believe it uses hidden files in the user root, although I'm not 100% sure.
You've got a pretty good example in your question.
It is different, not better. Preferences are a way of transparently storing settings for an application. These settings may be updated in run time by a user (for example you could use prefs to store user specific settings). Preferences are not meant to be editable outside of the application. Properties files tend to store hard setting specific to an application. These settings are the same for each user and tend not to change often. Properties files are text files and tend to accompany an application on deployment. You can edit them easily using a text editor. It is fairly rare for an application to update properties files.

How to deploy a Java Swing application with an embedded JavaDB database?

I have implemented an Java Swing application that uses an embedded JavaDB database. The database need to be stored somewhere and the database tables need to be created at the first run. What is the preferred way to do these procedures?
Should I always create the database in the local directory, and first check if the database file exist, and if it doesn't exist let the user create the tables (or at least show a message that the tables will be created).
Or should I let the user choose a path? but then I have to save the path somewhere. Should I save the path with Preferences.systemRoot();, and check if that variable is set on startup?
If the user choses a path and save it in the Preferences, can I get any problems with user permissions? or should it be safe wherever the user store the database? Or how do I handle this?
Any other suggestions for this procedure?
I would let the user choose. That way they can run multiple instances (simultaneously or otherwise). Supplying a sensible default would be a good move.
Store the path using the Preferences API (I would store per-user but that may differ depending on your application), and use the File object to determine if directories exist and/or are permissioned properly.

How to implement autoload in a Java application

So I'm writing this JFrame application that has its own document model that can be loaded and saved to a filepath. I'm wondering what good ways are there to make the application open the last saved file when it starts up.
Do I store last saved document filepath in a proprietary way or is there some facility in java that can handle this for me?
Why not use the Java Preferences API ?
That allows you to store settings/properties per user and/or per system. They'll be persisted automatically (via files in Unix/Linux, and in the registry in Windows, IIRC).

Open WIth dialog in Java

I was wondering if there's a cross platform way to mimic the Windows Open With dialog from inside a Java Swing application. My application is an editor for elearning packages and one of the users wanted to be able to open the content files in the editor of their choice from within the application, resources are generally HTML files, images, css, javascript, but can be any type of content that can run in a browser. Thanks
I think that you may be able to do something with JDIC (Java Desktop Integration Components).
https://jdic.dev.java.net/documentation/Examples.html
Take a look at the package org.jdesktop.jdic.filetypes
https://jdic.dev.java.net/nonav/documentation/javadoc/jdic/org/jdesktop/jdic/filetypes/package-summary.html
Provides classes for associating
applications with file types and
accessing a registered file type
association.
Every desktop allows the user to view
files with the associated
applications. Usually a desktop comes
with a registry that allows the user
to associate applications with file
types. An association includes
information like a MIME type, file
extensions, and actions that could be
applied to the file type. By accessing
the association information, the
desktop could invoke the appropriate
applications to handle a file, display
an URL or send an email.

Categories

Resources