How to use util class which serves based on user information - java

This is my first posting, glad to be here.
I am developing a web application, i have a one FormateUserLanguage util class in util package i want to initialize this util class with user specific information at the stating of the application and use that instance through out the user session(session is not available for this FormateUserLanguage class as it is util class).
example :
when userA login into the application userA information is initialized to util class and he can get the formated information for some areas.
as as userB, userC....etc,.
As this is multi user application need to show the userspecific information using FormateUserLanguage util class.
please help out how to implement this functionality in my application.
eagerly waiting for your reply. :)

I think you're thinking about an HttpSession which you can store arbitrary objects (attributes) which can be created/retrieved by the RequestObject.
You'll want to read up on lifecycles of web applications, and persisting such preferences. (Sessions are typically per Server JVM, but there are ways of transferring / sharing sessions between servers in a cluster)
You may want to figure out if you even need a per-session object, or if it could be created on a per-request basis with maybe with a Filter...

Related

How to implement tracking feature

I'm thinking about the best way of implementing a way to force my service to run certain methods when an endpoint is used.
For example:
I have a Car endpoint. A user can create a car with this endpoint. When a user creates a Car the databases saves the data.
The created car can also be edited by the user. The database saves this data as well.
But the thing I want implement is a way in the background to also save the data about the old Car object and the user when a create/edit on a car is done.
I can put this logic in the service layer of my application, but since this is a vital to my application to keep track of who does the changes, this must always be in the code.
Putting the logic in my service layer might result that the code is lost/not implemented correctly.
What is good way to implement this feature?
You should look into Change Data Capture pattern.
Details, on how this works for SQL Server, can be found here.

How does the Three Tier architectural style works? Some handy example

I have been provided with a Java Application on which to apply the Three-Tier architectural style; One of the use cases to do is the login.
I've studied all the theory and rules that apply to this architectural style, but I need to understand the logic of the objects co-operation between the various levels and how patterns work together on each level to realize this (and the others) use case.
First, I've created the three basical packages: Presentation, Application and Data. In addition, I included one more package about the Boundary classes, The various GUIs from which requests will be sent.
In the Presentation layer, I just put a Front Controller, that encapsulates the presentation logic required by clients using the application.
In the Data layer, I put a DatabaseConnection class (Class that communicates with the database and that is responsible for loading the driver, connecting to the database, querying etc.) and DAOs classes (Data Access Object, that interfaces with the database).
The real problem is that I don't know what to put in the Application level, which represents the main part of the application, defining the domain model of the application, that is: their entities, their relationships, and application logic. It should not contain any reference to how the data will be presented to the user or how they will be saved.
So, I currently have this hierarchy:
Main ---> Boundary > Presentation > Application > Data > Database
In accordance with this architecture, how can I make a simple login?
Bearing in mind that each level can communicate ONLY with the underlying level;
For example, a class in Boundary layer cannot comunicate directly with a class in Data layer, Boundary's classes can comunicate with Presentation's classes only.
If necessary, you can post a pseudocode, which makes the idea of the steps to do.
Your Boundary calls only basic methods on the Presentationlayer.
Let's say the User clicks a Button to create a User the flow would be the following: Boundary calls method createUser(String name, int age) on the FrontController (Presentationlayer). The Controller can check some basic (UI-Related) things and would then call a similar method on the Applicationlayer.
The Applicationlayer can now process some further checks (for example: is the Current Active User allowed to create a User?). The Applicationlayer takes the given informationen (name and age), creates a DAO based on that and invokes the method to create the User on the Datalayer (DAO).
The Datalayer simply inserts the given information.

Multiple users using same Servlets will they override eachothers variables?

I'm making calls to the Java backend through servlets and for each call to the API im using i need to supply password and username. Can I save the users password/username in a variable so I can use it every time the user makes a call to the API?
Or will the variable be overwritten if there are multiple users?
The overall question perhaps is: Does every user get new "fresh" servlets or is the data saved from the users before?
Servlets are shared for performance reasons, so they should be stateless (or thread-safe, but then you'd just be reinventing the wheel). If you need to keep state with a user, put it into HttpSession.
If you use static variables then the variables got shared across the users. Also, it's a concurrency issue and you need to handle it. If the variables are not static then each user would have an excluded instance.
Anyway, If you want to store some information and you want to retrieve them in the future when a specific user call the servlet then it's common to use HttpSession. Also, consider that you need to grab the http cookie in your client and send it to the server for the further calls.
Also, it's a common scenario that server provides an API key for each client. Then whenever the client wants to call an API; it's necessary to send the key through a specific http header.
The above scenario would have some security issues if you don't use a secure connection like HTTPS between server and clients.
Does every user get new "fresh" servlets or is the data saved from the
users before?
Servlet instance will be created only once (unless you are doing something differently like using SingleThreadModel) & maintained by the container (like Tomcat, etc..), i.e., one single servlet instance is going to be used/shared by all requests.
So, don't use the servlet's instance variables to store any data.
Can I save the users password/username in a variable so I can use it
every time the user makes a call to the API?
You shouldn't be saving userids and passwords into JVM (RAM) memory because you will loose all of the data when the JVM shuts down, so you need to use some persistent store like a database (prefer this) or file system.

When should I use setServletContext.setAttribute() at the application start up? (Spring mvc)

I was wondering when I should use setServletContext.setAttribute(), at the start up of the application, to store data.
I suppose I should use it to store data that I share among many classes of my web application.
For example: so far, I had a website in which I read the address, emails etc from a .properties file. I print address, emails etc in many pages, so I created a class (ConfigurationData.class) which reads that .properties file, and every class that need to know address, email etc "#Autowires" the ConfigurationData.class.
I was wondering if instead of #Autowired ConfigurationData.class in many other classes I should use setServletContext.setAttribute() at the start up of the application.
Which solution "use" less server resources?
Since every "#Autowired" class is a singleton, using many time "#Autowired" for the same class shoudn't be "heavy" for the server, should be?
Thank you in advance.
Which solution I should use ?
It is always better to use #Autowired ConfigurationData option because you can inject this object anywhere (like in service layers etc..) inside your application, where as servletcontext object you can use only in front end layers (like controllers). So obviously, using #Autowired ConfigurationData is the best option.
Since every "#Autowired" class is a singleton, using many time "#Autowired" for the same class shoudn't be "heavy" for the server, should be?
Using #Autowired many times for the same class does NOT create many objects (unless you are changing the default singleton scope of the bean), rather they use the same object with a different reference. Also, one more point is that even if you want to use servletcontext object, you need to use #Autowired in your controller (rather than accessing from HttpServletRequest), you can look at here
Which solution "use" less server resources?
It will not make a big difference because both of them are singleton objects.
However, when it comes to performance and resource usage, you need to benchmark your application and then find out the bottlenecks. Surely, in general, these small things do not become bottlenecks for the application.

Isolation of sessions and interface parameters Java EE

we have a JavaEE Web application that runs on Tomcat5 server (jsp, java and extjs are used on development of this tool).
We observe sometimes, particularly when the application is highly used, that some session variables or interface fields are mixed up in servlets.
This means that : when one user connects, his parameters are kept in the sessions.
After an update or insertion of data in the database ORACLE,the system returns the name of another user who was probably connected at the same time on a different navigator or a different computer.
Others times the request.getparameter gets a values from different clients or user interfaces.
Thank you to help me fix this problem.
The only way for this to happen is if you have a singleton somewhere that keeps track of the session and for some reason this singleton is mixing up the variables between sessions.
You can try this: Instead of differentiating a user only by its credentials (assuming you are not using Federation), return a token, unique amongst all sessions to the user logging in, and make the user send that unique id with every request. This way you will be able to differentiate the request/responses even if the user logs in using many different browsers.
But again, this will only happen when you do something like having a singleton connecting the session, by itself the session would not exchange variables amongst them.
I've finally understood how servlet.java works.
I works like main process that create a thread for any call
thus, all attributes of such class are common to all thread and we are not sure of the result that we get when accessing them.
If we want any client to use those variables in a private context, we must put them in the function "doGet" or "doPost" as local variables.
My problem have them been solved

Categories

Resources