0

I have a login page i.e index.jsp which is having 3 fields:

1.> Usertype select tag (it include admin, emp, proj. co-ordinator, proj. manager) 2.> Username textbox 3.> Password textbox

Login button

Now the issue is I had different table for different users (admin, emp, proj. co-ordinator, proj. manager) with their username and password.

So is there any step wise procedure to validate multi user login using servlet and jsp.

I m successfully able to run for single user (like for only username and password) but unable for multi user.

Atiq
  • 166
  • 2
  • 6
  • 21
  • 1
    This is an issue with your db design. Make it perfect. – Kris Feb 11 '14 at 08:42
  • Your design has a flaw. But to give hint to you, you could have `if-else` based on what user type user has selected, you can lookup different table. – Pradeep Simha Feb 11 '14 at 08:45
  • @PradeepSimha saying that the db design has a flaw and then recommending that the OP introduces _another_ flaw is not necessarily constructive. At the very least it should be an `enum` that holds the correct `PreparedStatement`. – Boris the Spider Feb 11 '14 at 08:47
  • @Boris the Spider, as far as I know, he is very beginner. Telling him to go to enum makes more problem more complex than required. – Pradeep Simha Feb 11 '14 at 08:49
  • Krishnanunni in this case how to make db design perfect – Atiq Feb 11 '14 at 09:07
  • Pradeep Simha I tried using if-else but not successful as of yet now and using if-else statement is proper standard – Atiq Feb 11 '14 at 09:10

2 Answers2

2

Suppose in controller you get usertype in variable type, then you can manually create query and fetch the result from db like this :

String query = "Select * from "+type+" where <Your condition>";

But This is really bad idea to do this. Instead you should look at inheritance in db. and I guess this will help you to restructure your database.

Why this is bad idea?

Lets suppose you have 3 type of user currently in your system say : admin, employee and user.

Now after some time, if you need to add another type of user in system say HR, then what steps you have to perform:
1) Create another table for HR with same duplicate columns.
2) Change your logic in java files for HR user.
3) To scan the whole project for the same effect if you have hard-coded the query.

And benefits of inheritance in db:

Just insert the record in db with type = "HR" and you are done. Isn't is quite simple?

And FYI :

Advantages:

  • Simple approach.
  • Easy to add new classes, you just need to add new columns for the additional data.
  • Supports polymorphism by simply changing the type of the row.
  • Data access is fast because the data is in one table.
  • Ad-hoc reporting is very easy because all of the data is found in one table.

Disadvantages:

  • Coupling within the class hierarchy is increased because all classes are directly coupled to the same table. A change in one class can affect the table which can then affect the other classes in the hierarchy.
  • Space potentially wasted in the database.
  • Indicating the type becomes complex when significant overlap between types exists.
  • Table can grow quickly for large hierarchies.

When to use:

  • This is a good strategy for simple and/or shallow class hierarchies where there is little or no overlap between the types within the hierarchy.
Community
  • 1
  • 1
Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
0

In your case most probably there are two approahes

1)You can manually create query as said above

2) You can create a table which contains usertype(it include admin, emp, proj. co-ordinator, proj. manager) and its concerned table. So at controller when you get required info

But that will be a overhead and for new usertype you have to create anew user. So add it would be better if you have a single table which stores username and password of all and for user type you can add new column.

Dhruv Pal
  • 849
  • 2
  • 10
  • 25