• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help me, login different categories of users from databse

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
majid nakit
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic