One of the main reason behind writing this post is to help others
because i found that lots of people have same query but they don't get
the solution. So, i hope this post will be useful for those people.
If you are working with the web applications, then its easy to maintain sessions and accordingly authenticate the users for the contents they can access in the application.
But achieving the same result is different from the web applications. I needed the same thing in the current project i am working on. Like after login to the system, based on username i had to give different rights for the user to access the contents.
For example if the username is "Admin" then he can have all the rights like adding, deleting, modifying and so on.., but if the username is "Bharvi" then he can only view the particular contents.
For achieving the desired result, one has to know about System class in java. The System class can't be instantiated but it provides several useful static methods.
Now coming straight to the code snippet i have implemented.
public class Login_Controller implements ActionListener {
Login_Frame frame;
Login_Bean bean;
public Login_Controller(Login_Frame frame, Login_Bean bean)
{
this.frame= frame;
this.bean = bean;
}
@Override
public void actionPerformed(ActionEvent event) {
if(event.getSource()==frame.btnSubmit)
{
boolean result = false;
bean = new Login_Bean();
String username= frame.textfield_username.getText().toString();
String password= frame.passwordField_Password.getText().toString();
bean.setUsername(username);
bean.setPassword(password);
System.setProperty("id", username);//This is only thing i needed for my purpose. :)
Login_DAO dao = new Login_DAO();//Class where all the database related things are kept
result = dao.authenticate(bean); //the authenticate method calls the query for login authentication
if(result)
{
//Set the action you want to perform such as opening home interface
}
else{
JOptionPane.showMessageDialog(frame.frame, "Invalid Username or Password");
}
}
}
}
The setProperty method of the system calss takes a key-value pair as its parameter.
in the above example the key is "id" which is the name we are setting as the system property.
Once this property is set we can access it from anywhere in the application using System.getProperty(key) method.
For example if username is "Admin" the only i want to give the user access to delete a record in application. Then i can achieve it as:
public void actionPerformed(ActionEvent e)
{
if(e.getSource==anyframe.button_delete_record)
{
if(System.getProperty("id").equals("Admin"))
{
//perform the deletion task
}
else{
JOptionPane.showMessageDialog(anyframe.frame,"You don't have permission to delete records");
}
}
}
Another option is to store it (i.e. Username) in a hash map or value object and setting that object in the same way as above.
If you are working with the web applications, then its easy to maintain sessions and accordingly authenticate the users for the contents they can access in the application.
But achieving the same result is different from the web applications. I needed the same thing in the current project i am working on. Like after login to the system, based on username i had to give different rights for the user to access the contents.
For example if the username is "Admin" then he can have all the rights like adding, deleting, modifying and so on.., but if the username is "Bharvi" then he can only view the particular contents.
For achieving the desired result, one has to know about System class in java. The System class can't be instantiated but it provides several useful static methods.
Now coming straight to the code snippet i have implemented.
public class Login_Controller implements ActionListener {
Login_Frame frame;
Login_Bean bean;
public Login_Controller(Login_Frame frame, Login_Bean bean)
{
this.frame= frame;
this.bean = bean;
}
@Override
public void actionPerformed(ActionEvent event) {
if(event.getSource()==frame.btnSubmit)
{
boolean result = false;
bean = new Login_Bean();
String username= frame.textfield_username.getText().toString();
String password= frame.passwordField_Password.getText().toString();
bean.setUsername(username);
bean.setPassword(password);
System.setProperty("id", username);//This is only thing i needed for my purpose. :)
Login_DAO dao = new Login_DAO();//Class where all the database related things are kept
result = dao.authenticate(bean); //the authenticate method calls the query for login authentication
if(result)
{
//Set the action you want to perform such as opening home interface
}
else{
JOptionPane.showMessageDialog(frame.frame, "Invalid Username or Password");
}
}
}
}
The setProperty method of the system calss takes a key-value pair as its parameter.
in the above example the key is "id" which is the name we are setting as the system property.
Once this property is set we can access it from anywhere in the application using System.getProperty(key) method.
For example if username is "Admin" the only i want to give the user access to delete a record in application. Then i can achieve it as:
public void actionPerformed(ActionEvent e)
{
if(e.getSource==anyframe.button_delete_record)
{
if(System.getProperty("id").equals("Admin"))
{
//perform the deletion task
}
else{
JOptionPane.showMessageDialog(anyframe.frame,"You don't have permission to delete records");
}
}
}
Another option is to store it (i.e. Username) in a hash map or value object and setting that object in the same way as above.
good stuff...
ReplyDeletecongratulation dixit, tere blog ko maine padha kafi achha hai.... mai ese class me apne new students ke sath share karunga.....
ReplyDelete@Amit Anall Srivastava
ReplyDeleteSure sir :) and thanks a lot for the compliment..!!