wood burning stoves 2.0*
The moose likes JSP and the fly likes login page with different user Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "login page with different user" Watch "login page with different user" New topic
Author

login page with different user

nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

hello,
i have 3 user types in my database as follows : admin,buyer and seller.
i want to create a login.jsp page. in this page if admin logged in then it should redirect to admin.jsp.
if buyer logged in then it should redirect to buyer.jsp and for seller it should redirect to seller.jsp.
it simply means that i want to use one login page for each user type and redirect to appropriate jsp page according to the user type.

thanks and regards
nishi kishore
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 751
If you are using servlet then you can put the redirect logic in your servlet.



Regards,
Jigar


Jigar Naik


nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

i m refining my problem as follows :

i have a table named as "user" which is defined as follows :

user_name|password|user_type
admin|admin|admin
john123|john1| seller
albert|albert|buyer

i want to create a login.jsp page which will ask for username and password.
if i logged with admin login credentials, it should redirect to admin.jsp page.

if john123 logged in on the same login.jsp page, it should redirect to seller.jsp
and if albert logged in on the same login.jsp, it should redirect to buyer.jsp

i dont want to redirect on the basis of user_name, but on the basis of user_type which is only available in the database.

please help me to sort out this problem.

thanks and regards
nishi kishore

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

You already have a field in you table as user_type. When the use loggs in check, what user type he/she is. For e.g. if the logged in user has user_type field value buyer, redirect him to buyer page.


Swastik
nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

but user_type is only available in database not in the login.jsp page or any other jsp page.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

I believe the user needs to first login, right? When the user submits his/her details in the login page, the request goes to a servlet, the servlet receives the userid and password, validates these details against the values stored in underlying database, and if valid checks the user_type variable value, and based on this value the user gets redirected to the specific page.
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 751
Select userType from user where userName = 'john' and password = 'xyz'

Do a quick google search on JDBC.
nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

yes Swastik Dey, you got my problem.

but it is not specific to one user.
there are multiple seller user_type.

like john123 is a seller.
same as ricky is a seller, patrick is a seller and so on.

same is the case for the buyer user_type.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

It need not be specific to one user. Jigar in his post has already pointed what to do.
nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

hi,
first of all i am sorry for the late reply. i didnt reply due to some health issue.

so here is my code for the problem as i mentioned earlier :


<form method="post" class="searchform" action="#">
<table class="pretty-table">
<tr><td>Username</td><td><input type="text" name="txtusername" class="textbox" /></td></tr>

<tr><td>Password</td><td><input type="password" name="txtpassword" class="textbox" /></td></tr>
<tr><td><input name="btnLogin" type="submit" value="Login" class="button"/></td><td><input name="btnCancel" type="reset" value="Cancel" class="button"/></td></tr>
<tr>
<td><a href="register.jsp">Back to registration page</a></td>
<td><a href="index.jsp">Back to home page</a></td>
</tr>
</table>
</form>

<%
String uname=request.getParameter("txtusername");
String password=request.getParameter("txtpassword");
PreparedStatement pst=conn.prepareStatement("Select user_type from user where user_name = ? and password = ? ");
pst.setString(1,uname);
pst.setString(2,password);
ResultSet rst=pst.executeQuery();
if(rst.next())
{
String type=rst.getString("user_type");
if("admin".equals(type))
{
response.sendRedirect("admin.jsp");
}
else if("seller".equals(type))
{
response.sendRedirect("seller.jsp");
}
else if("buyer".equals(type))
{
response.sendRedirect("buyer.jsp");
}
}


%>

this code is working fine with admin login only.

when i tried with other login(buyer or seller), it is not working. it stays on the same page.

please suggest me appropriate solution for this problem.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

Nishi,

What are the user types in the underlying table?
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 751
Please use code tags.

Please check what role you are getting from database. Please debug below code. There might be case sensitivity issue.

nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

swstik day the user type is classified as admin, seller and buyer.

jigar i have checked the code and there is no such kind of error.
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 751
What value you are getting in your Steing variable "type" ?
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
First of all , use the code tag to post your code.

Also follow MVC architecture. That's very helpful. Avoid using of scriplets and java codes inside your jsp page.

Print the value of String type, and check out what value you are getting


kunal
nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

jigar ........ type holds the value of data_type which resides in the table user in the database.

kunal........... i am not familiar with MVC architecture.
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 751
Good, now put one System.out.println(type); and check what value you are getting from database at runtime.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
Nishi, do a bit of googling. You will find many topics on that. Even the simple search on this (javaranch) forum will fetch you the explanations.
nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

kunal, it will take a lot of time to learn a new concept and implement it.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

Is you problem solved?
nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

swastik, no i am still on the same error
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

Did you print the value of user_type using System.out.println? If yes what does it show?
nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

swastik, it is only showing the value admin in the console and rest of the values are empty.
sailaja usha
Greenhorn

Joined: Jun 23, 2012
Posts: 13

Mr.Nishi ,

Post your code(part where you got strucked) such that the others can identify the error or reason.

Sailaja


regards
Sailaja
sailaja usha
Greenhorn

Joined: Jun 23, 2012
Posts: 13

sorry for the above message, i didn't see your code.

try to match the strings without using "equals" function.
nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

sailaja usha , i have already posted the code in one of the previous posts.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

Nishi,

What does it show for user_type when you login with user name john123?
nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

sailaja usha, i used the equals to sign(==). but now the admin value is removed and rest of values are still at same condition s earlier.
nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

Swastik Dey, for any other user apart from admin, the console is blank.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

== sign is for reference comparison not value comparison. You should use .equals method and if there is any case sensitivity issue it's better to use equalsIgnoreCase.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

Nishi

Could you please post the latest updated code?
sailaja usha
Greenhorn

Joined: Jun 23, 2012
Posts: 13

try testing on your if conditions.....

try placing the if condition for "admin" in else if() like

if("seller".equals(type))
......
else if("admin".equals(type))
.....
else if()
....

such that we can find out where the issue is.
nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

sailaja usha , i have changed the code as follows in if condition

if("seller".equals(type))
{
System.out.println(type);
//response.sendRedirect("admin.jsp");
}
else if("admin".equals(type))
{
System.out.println(type);
//response.sendRedirect("seller.jsp");
}
else if("buyer".equals(type))
{
System.out.println(type);
//response.sendRedirect("buyer.jsp");
}


but still the console is showing the value for admin and rest of the values are empty
sailaja usha
Greenhorn

Joined: Jun 23, 2012
Posts: 13

This shows you are not receiving the value of type other than admin.

could you please post the database values of the table user....
nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

Swastik Dey, here is the updated code


<form method="post" class="searchform" action="#">
<table class="pretty-table">
<tr><td>Username</td><td><input type="text" name="txtusername" class="textbox" /></td></tr>

<tr><td>Password</td><td><input type="password" name="txtpassword" class="textbox" /></td></tr>
<tr><td><input name="btnLogin" type="submit" value="Login" class="button"/></td><td><input name="btnCancel" type="reset" value="Cancel" class="button"/></td></tr>
<tr>
<td><a href="register.jsp">Back to registration page</a></td>
<td><a href="index.jsp">Back to home page</a></td>
</tr>
</table>
</form>

<%
String uname=request.getParameter("txtusername");
String password=request.getParameter("txtpassword");
PreparedStatement pst=conn.prepareStatement("Select user_type from user where user_name = ? and password = ? ");
pst.setString(1,uname);
pst.setString(2,password);
ResultSet rst=pst.executeQuery();
while(rst.next())
{
String type=rst.getString("user_type");
if("admin".equals(type))
{
System.out.println(type);
//response.sendRedirect("admin.jsp");
}
else if("seller".equals(type))
{
System.out.println(type);
//response.sendRedirect("seller.jsp");
}
else if("buyer".equals(type))
{
System.out.println(type);
//response.sendRedirect("buyer.jsp");
}
}


%>
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

Nishi,

Print the value before starting the condition check, then only you will be able to figure out what value you are getting. Because if the condition check is not satisfied it will never print the value.

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

while(rs.next()) is not required for your case, because you should have a single record for one user, use if(rs.next())
nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

Swastik Dey, now i am getting the exact value for each user type

if(rst.next())
{
String type=rst.getString("user_type");
System.out.println("user type="+type);
if("admin".equals(type))
{
System.out.println(type);
//response.sendRedirect("admin.jsp");
}
else if("seller".equals(type))
{
System.out.println(type);
//response.sendRedirect("seller.jsp");
}
else if("buyer".equals(type))
{
System.out.println(type);
//response.sendRedirect("buyer.jsp");
}
}
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

Can you show what value is it printing for each user type?
nishi kishore
Greenhorn

Joined: Jun 21, 2012
Posts: 26

sailaja usha ,

userid, user_name, password, first_name, last_name, gender, user_type, address, phone, email_id
1, 'rameshk', 'ramesh', 'ramesh', 'kumar', ' male ', ' seller ', 'delhi', '98746578765', 'ramesh@gmail.com'
2, 'vikramv', 'vikram', 'vikram', 'verma', ' male ', ' buyer ', 'ghaziabad', '9869098998', 'vik@yahoo.com'
3, 'nishik', 'nishi', 'nishi', 'kishore', ' male ', ' seller ', 'mumbai', '9002345678', 'nishi@gmail.com'
4, 'rohits', 'rohit', 'rohit', 'sharma', ' male ', ' buyer ', 'lucknow', '86834734', 'rohit@gmail.com'
10, 'nishi4567', 'nishi', '', '', ' Select your gender ', ' Select user type ', '', '', ''
11, 'piyush123', '123123123', 'piyush', 'gupta', ' male ', ' buyer ', 'jaipur', '86834734', 'p@gmail.com'
12, 'admin', 'admin', 'admin', 'admin', 'male', 'admin', 'unknown', '9808787876', 'admin@attanoida.com'
 
wood burning stoves
 
subject: login page with different user
 
Similar Threads
filters in java
Session Management Still weird
struts2 redirect from action login
How to invalidate user's session forcefully
want to develop dynamic shopping cart