This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

login page with different user

 
nishi kishore
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using servlet then you can put the redirect logic in your servlet.



Regards,
Jigar
 
nishi kishore
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nishi kishore
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but user_type is only available in database not in the login.jsp page or any other jsp page.
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Select userType from user where userName = 'john' and password = 'xyz'

Do a quick google search on JDBC.
 
nishi kishore
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1604
5
Android Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It need not be specific to one user. Jigar in his post has already pointed what to do.
 
nishi kishore
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1604
5
Android Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nishi,

What are the user types in the underlying table?
 
Jigar Naik
Ranch Hand
Posts: 762
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What value you are getting in your Steing variable "type" ?
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
nishi kishore
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good, now put one System.out.println(type); and check what value you are getting from database at runtime.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kunal, it will take a lot of time to learn a new concept and implement it.
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is you problem solved?
 
nishi kishore
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swastik, no i am still on the same error
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you print the value of user_type using System.out.println? If yes what does it show?
 
nishi kishore
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swastik, it is only showing the value admin in the console and rest of the values are empty.
 
sailaja usha
Greenhorn
Posts: 13
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr.Nishi ,

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

Sailaja
 
sailaja usha
Greenhorn
Posts: 13
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for the above message, i didn't see your code.

try to match the strings without using "equals" function.
 
nishi kishore
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sailaja usha , i have already posted the code in one of the previous posts.
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nishi,

What does it show for user_type when you login with user name john123?
 
nishi kishore
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey, for any other user apart from admin, the console is blank.
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== 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
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nishi

Could you please post the latest updated code?
 
sailaja usha
Greenhorn
Posts: 13
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show what value is it printing for each user type?
 
nishi kishore
Ranch Hand
Posts: 32
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic