File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes help me, login different categories of users from databse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "help me, login different categories of users from databse" Watch "help me, login different categories of users from databse" New topic
Author

help me, login different categories of users from databse

majid nakit
Ranch Hand

Joined: Jun 26, 2001
Posts: 165
Hi, I am looking to have a struts login web application, for example we have different categories of users (doctors, nurses,...), I want when a doctor login he will go to doctor.jsp and nurse goes to nurse.jsp ??

Thank you, your help is appreciated.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
This seems fairly simple: Assign a role to each user (Doctor, Nurse, etc.). When the user logs on, retrieve the role information. In your action, implement logic that will forward to one page or the other based on the role retrieved from the database with the user ID provided.

Create two local forwards for the login action: one for doctor and one for nurse.


Merrill
Consultant, Sima Solutions
majid nakit
Ranch Hand

Joined: Jun 26, 2001
Posts: 165
I have two tables :
users (id, firsname, lastnime, username, password, rolekey)

roles (id, rolename)

In my LoginAction I need a call to a method getInfo(username, password) of another class Services to get the username and the rolename, my question is how I am going to save username and rolename in getInfo(...) and how I will pass get them back in LoginAction ??

Thank you.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Create a Transfer Object (Sometimes called Data Transfer Object, or DTO). This is a simple javaBean that is used to tranfer data. I'd suggest creating a DTO named User that contains a user ID, name, a role, and whatever other information you need about the user. Have your getInfo() method return this object. Once you have it, save the object in the session so that any time this user accesses a page, you know what role the user has.
majid nakit
Ranch Hand

Joined: Jun 26, 2001
Posts: 165
Could you please help me do the last lines that has ??? :

now I have the employees table and the roles table , I created the Employee javabean, in the LoginAction when the emploee login I put :
session.setAttribute("USER", username);
..
return (mapping.findForward(target)); this target is another Action called : EmployeeCustomizedAction :

public class EmployeeCustomizedAction extends Action {

protected ArrayList getEmployees(HttpServletRequest request) {

Employee employee = null;
ArrayList employees = new ArrayList();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

ServletContext context = servlet.getServletContext();
DataSource dataSource = (DataSource)
context.getAttribute(Action.DATA_SOURCE_KEY);
// get the username from session
HttpSession session = request.getSeesion();
String user = session.getAttribute("USER");

try {

conn = dataSource.getConnection();
stmt = conn.createStatement();
rs =
stmt.executeQuery("select * from employees, roles, "
+ "where employees.roleid=roles.roleid "
+ "and employees.username=user");

while ( rs.next() ) {

employee = new Employee();

employee.setUsername(rs.getString("username"));
employee.setName(rs.getString("name"));
employee.setRolename(rs.getString("rolename"));
employee.setPhone(rs.getString("phone"));
employee.setEmail(rs.getString("email"));
employee.setRoleid(new Integer(rs.getInt("roleid")));
employee.setDepid(new Integer(rs.getInt("depid")));
employee.setDepartment(rs.getString("depname"));

employees.add(employee);

System.err.println("Username : "
+ employee.getUsername()
+ " Department : " + rs.getString("depname"));
}
}
catch (SQLException e) {

System.err.println(e.getMessage());
}
finally {

if (rs != null) {

try {

rs.close();
}
catch (SQLException sqle) {

System.err.println(sqle.getMessage());
}
rs = null;
}
if (stmt != null) {

try {

stmt.close();
}
catch (SQLException sqle) {

System.err.println(sqle.getMessage());
}
stmt = null;
}
if (conn != null) {

try {

conn.close();
}
catch (SQLException sqle) {

System.err.println(sqle.getMessage());
}
conn = null;
}
}
return employees;
}

public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

Employee employee = null;
// Default target to success
String target = new String("success");


ArrayList employees = null;

employees = getEmployees(request);

// Set the target to failure
if ( employees == null ) {

target = new String("login");
}
else {
request.setAttribute("employees", employees);
if (rolename is = doctor HOW TO DO THIS ???)
target = new String("doctor");
if (rolename is = nurse HOW TO DO THIS ???)
target = new String("nurse");
}
// Forward to the appropriate View
return (mapping.findForward(target));
}
}
[ April 03, 2006: Message edited by: majid nakit ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Are you really expecting more than one employee to have the same user ID? If there's only one per User ID, call the method getEmployee and have it return a single employee.

Once you have that single employee, your code will look like this:



Another bit of advice for your design: It isn't necessary to put all your logic in classes that extend Action. I'd suggest a regular java class that follows the Data Access Object (DAO) pattern to handle the data retrieval. This object would have a getEmployee method. Then just call it directly from the Login action and put the logic for deciding which page to display in the login action as well. There's no need for a second action.
[ April 03, 2006: Message edited by: Merrill Higginson ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help me, login different categories of users from databse