JSF application not supporting multiple users logged in - java

I am a JSF beginner and have developed a mini application that is working fine. the problem is, when more than one user logs in, the application won't work. Only one user can log in and work. what part of application probably have to be checked??? the only static variable in my application are the beans managed name as follows:-
public static final String MANAGED_NAME = "catBean";
public static final String MANAGED_NAME = "appBean";
etc etc.
Do I need to change the static keyword? or where possibly can error be in my application. this is way too primitive question but owing to my very little knowledge accepted........:)

You need to take sessionScoped bean to maintain session data
It seems you are using static fields which are shared and they are not at object level they are associated with class

Related

How to store a static variable in desktop application designed in Java Swing?

I start my desktop app with a login, validate the user and "redirect" to the index Jframe.
What I want now is to store a user Object obtained from login so that it's available in all jframes that I'll use later.
Is it possible? I know that session variables are not that useful in desktop apps but I can't seem to find the way to create a static variable.
I tried creating my Object user in main, then setting it up after login. But it couldn't find the variable.
you can use a static variable:
public class MyAppContext {
public static volatile Object loginObject;
}
MyAppContext.loginObject = "whatever";
Or use a framework like Spring to manage your contexts.

Vaadin UI - cannot use static fields for every client

I'm creating a web app.
I have a MyUI that extends UI.
public class MyUI extends UI {
public static Authentication AUTH;
#Override
protected void init(VaadinRequest vaadinRequest) {
AUTH = new Authentication();
updateContent();
}
//other methods
}
In Authentication() I have the user logged. When I do the logout, that user is set to null.
I noticed that when I access to the server from two device, so when I should have two connection to the server, when someone do the logout, the other one is logged out too.
In Authentication class there is this method:
public void doLogout() {
System.out.println("User: " + this.user.getMail() + " has logged out.");
this.user = null;
}
I get error in user.getMail() because the user is already set to null, when I try to do the logout from the other account.
Is com.vaadin.ui.UI unique? How can I handle different connections in Vaadin?
Java, in general
Basic Java here, nothing to do with Vaadin…
The keyword static means one value per class, otherwise known as a “class variable”, more generally known as a “global variable”. Not object-oriented. Generally speaking, you should minimize your use of static.
Omitting that keyword means “one value per instance of this class”, an instance variable also known as member variables. This is object-oriented.
I suggest you learn more of the basics of Java before embarking on a Vaadin project. And learn the basics of how Java Servlet technology works, perhaps reading the Head First book on Servlet published by O’Reilly (now outdated, but basics are the same). You also need to learn about advanced topics such as threading and concurrency issues as a Servlet environment such as Vaadin is inherently multi-threaded. Eventually, read the book Java Concurrency In Practice by Goetz.
Vaadin specifically
To store values per user of a Vaadin app, use session attributes. This is a key-value store where you insert and retrieve a value by specifying a key, the name of the "attribute". The session is automatically instantiated for you when the user first connects, as part of the Java Servlet technology.
Alternatively, you can store values per user on that UI class as instance variables. Each Vaadin user starts with a UI instance when first connecting. But beware: you can open multiple web browser tabs/windows in a Vaadin 8 app, each having their own UI instance. So to share data between those multiple UI objects, use the session attributes.
Note that while every Vaadin app has at least one UI subclass defined, you can define additional UI subclasses as well, for those additional browser tabs/windows mentioned above to show different content.
Maybe instead of static variable you should use here session attribute (updated to be more generic):
UI.getCurrent().getSession().setAttribute("AUTH", AUTH);
Authentication auth2 = UI.getCurrent().getSession().getAttribute("AUTH");
So use VaadinSession to store session specific data.

Standalone JBOSS7 uses more JVMSs?

I would like to store some data in a static variables and I want all the webservices deployed on the same JBOSS7 to reach those data. I thought a standalone JBOSS runs in a single JVM and all the services run in the same JVM so they can access a static variable.
However I noticed that I got a NullPointerException when my webservice try to get the data.
This is my storage class:
public enum OneJvmCacheImpl {
INSTANCE;
private ConcurrentHashMap<String, Object> values = new ConcurrentHashMap<String, Object>();
public <T> T get(String key, Class<T> type) {
return type.cast(values.get(key));
}
...
}
OneJvmCacheImpl.INSTANCE.get(...);
Can you please advise me why I cannot access the values from my webservice?
Thanks,
V.
If you by deployments mean separate war files, the static variables will not be visible to the other webservices in other war files as they are loaded by different classloaders. Each war has it's own classloader, and hence it's own "class instance" of the class. You could perhaps solve it by moving the class in question to a place where it's shared amongst the deployments, but I would suggest that you solve it otherwise anyway, either by using the database or a distributed cache.
jBoss definitely won't allow you to share static variables across different deployments. That would be a huge security issue, what if I deploy a war next to yours and start changing your static variables...
You need to persist such values in something else like a database, memcache or shared file.

REQ: Retrieving properties in my java app. collected by "PropertyPlaceholderConfigurer" -- properties are stored in a database as key/value

PUsing Spring 3.2.0.release and JDK 1.6. I've a standalone Java program (NOT running inside tomcat etal) and I'm loading properties from a database.
I've used this excellent article as a base and it works perfectly. Using the PropertiesPrinter bean (defined there) as a base and adding getters I can do stuff like getFileLocation(), getPetDogsName() but then I need to have/create setter/getters for every property.
What I would like to have is a Spring Bean or normal Java class called DatabaseProperties with a method like getProperty("filelocation"); which I can use in my application (main)and so I can retrieve/get the value of the property filelocation which is somewhere inside the information collected by PropertyPlaceholderConfigurer.
I've done a lot of digging but can't seem to find the information I need or at least I'm not able to combine the gathered info into a working program as I'm not fluent with Spring....
Any hint/pointers/urls/code is higly appreciated. It's probably relative easy but it is still out of reach for me atm.
One solution for reading values set by the PropertyPlaceholderConfigurer, is to use the #Value annotation rather than a method for setting class member variables:
class MyClass {
#Value("${file.location}")
private String fileLocation;
...
}

Are there any plugins or modules like Django flatpages for Java in Spring?

Current I use Django and Python for web development.
I know Java, Spring, and Hibernate but not to an advanced level.
My main aim is to build a website with user registration and some static pages.
Now in Django, the flat pages modules are best for static pages and this is most commonly used.
Is there anything like that which I can use in Java with Spring? So that I don't have to code. Because I find it very hard to code the whole administration section for simple websites.
A simple way to serve up static pages would be to create a simple Controller that has a basic ReqestMapping which takes a PathVariable. Create static pages for each identifier and let the Controller serve them up. As follows:
#Controller
public class StaticPagesController {
#RequestMapping("/static/{id}/view.do")
public String subscribersListingHandler(#PathVariable String id) {
return id;
}
}
In the above, id is the name of the JSP file. Example call: http://myapp.com/static/about/view.do.

Categories

Resources