| | |
user authentication and authorization
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2007
Posts: 15
Reputation:
Solved Threads: 0
Hi,
We are developing a software in j2ee/jboss appserver. I have designed a login page the source code of which is given below,
loginpage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>index of third eye</title>
<link rel="stylesheet" href="stylepage.css">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body><b>
<center><form name="form" method="post" action="processinfo.jsp" >
UserName: <input type="text" name = "username" value=""><br><br>
Password:
<input type="password" name="password" value=""><br><p></p>
<input type="submit" name="submit" value="Login">
<input type = "reset" name="Reset" value="Reset"><br> <p></p>
<select>
<option value="generaluser" name="opt1">General User</option>
<option value="administrator" name="opt1">Administrator</option>
</select>
</form></center>
</body>
</html>
</body>
</html>
Processinfo.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.util.*" %>
<jsp:useBean id="idhandler" class="authenticateusers.Login" scope="request">
<jsp:setProperty name="idhandler" property="*"/>
</jsp:useBean>
<%if (idhandler.authenticateUser()){
%>
<jsp:forward page="mainpage1.html"/>
<% } else { %>
<jsp:forward page="error.jsp"/>
<% } %>
and the bean class
package authenticateusers;
import java.sql.*;
public class Login {
private String username="";
private String password="";
private String opt1[];
public Login() {
//authenticateUser();
}
public void setUsername(String username){
this.username=username;
}
public void setPassword(String password){
this.password=password;
}
public String getUsername()
{
return username;
}
public String getPassword()
{
return password;
}
public boolean authenticateUser(){
String query="select * from users;";
String DbUsername;
String DbPassword;
try {
Class.forName("org.postgresql.Driver");
Connection con=DriverManager.getConnection("jdbc:postgresql://192.168.128.150:5432/thirdeye", "postgres", "postgres");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(query);
while (rs.next()) {
DbUsername=rs.getString("userName");
System.out.println("username:" + DbUsername);
System.out.println("Entered user name : " + username);
DbPassword=rs.getString("Password");
System.out.println("password = " + DbPassword);
System.out.println("Entered password : " + password);
if (username.equals(DbUsername)&& password.equals(DbPassword))
{
return true;
}
}
}catch(Exception e){
e.printStackTrace();
}
return false;
}
}
This code works fine for me. However we want that the users should be redirected to different pages depeding on their roles which the user selects from a drop down box while logging in. Could any one please tell me how to do that.
Thanks in advance.
Saswati
We are developing a software in j2ee/jboss appserver. I have designed a login page the source code of which is given below,
loginpage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>index of third eye</title>
<link rel="stylesheet" href="stylepage.css">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body><b>
<center><form name="form" method="post" action="processinfo.jsp" >
UserName: <input type="text" name = "username" value=""><br><br>
Password:
<input type="password" name="password" value=""><br><p></p>
<input type="submit" name="submit" value="Login">
<input type = "reset" name="Reset" value="Reset"><br> <p></p>
<select>
<option value="generaluser" name="opt1">General User</option>
<option value="administrator" name="opt1">Administrator</option>
</select>
</form></center>
</body>
</html>
</body>
</html>
Processinfo.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.util.*" %>
<jsp:useBean id="idhandler" class="authenticateusers.Login" scope="request">
<jsp:setProperty name="idhandler" property="*"/>
</jsp:useBean>
<%if (idhandler.authenticateUser()){
%>
<jsp:forward page="mainpage1.html"/>
<% } else { %>
<jsp:forward page="error.jsp"/>
<% } %>
and the bean class
package authenticateusers;
import java.sql.*;
public class Login {
private String username="";
private String password="";
private String opt1[];
public Login() {
//authenticateUser();
}
public void setUsername(String username){
this.username=username;
}
public void setPassword(String password){
this.password=password;
}
public String getUsername()
{
return username;
}
public String getPassword()
{
return password;
}
public boolean authenticateUser(){
String query="select * from users;";
String DbUsername;
String DbPassword;
try {
Class.forName("org.postgresql.Driver");
Connection con=DriverManager.getConnection("jdbc:postgresql://192.168.128.150:5432/thirdeye", "postgres", "postgres");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(query);
while (rs.next()) {
DbUsername=rs.getString("userName");
System.out.println("username:" + DbUsername);
System.out.println("Entered user name : " + username);
DbPassword=rs.getString("Password");
System.out.println("password = " + DbPassword);
System.out.println("Entered password : " + password);
if (username.equals(DbUsername)&& password.equals(DbPassword))
{
return true;
}
}
}catch(Exception e){
e.printStackTrace();
}
return false;
}
}
This code works fine for me. However we want that the users should be redirected to different pages depeding on their roles which the user selects from a drop down box while logging in. Could any one please tell me how to do that.
Thanks in advance.
Saswati
Simple if statement with checking user selection will solve the problem
PS: Please use hash sign "#" to insert any code into your post. It will keep code format as from IDE and post would not be so long. Thanx
JSP Syntax (Toggle Plain Text)
if(student) { RequestDispatcher dispatcher = getServletContex().getRequestDispatcher("/student.jsp"); dispatcher.forward(request, response); } else if(teacher) { RequestDispatcher dispatcher = getServletContex().getRequestDispatcher("/teacher.jsp"); dispatcher.forward(request, response); } else { RequestDispatcher dispatcher = getServletContex().getRequestDispatcher("/admin.jsp"); dispatcher.forward(request, response); }
PS: Please use hash sign "#" to insert any code into your post. It will keep code format as from IDE and post would not be so long. Thanx
Last edited by peter_budo; Jun 22nd, 2007 at 4:26 am.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Jul 2007
Posts: 2
Reputation:
Solved Threads: 0
Hi,
You can opt one more solution. Try putting user roles in your user table in database. Now when user will log into your system, check their role and populate it into bean. This way, user bean will always be having their associated role. Put this bean in session scope.
Now after login functionality simply redirect user to a common url using the same code as peter said.
Use a filter to authenticate user to access particular resource like your jsp & servlets. Because you have user bean in session you can always check its role and corresponding url that can be accessed by that particular user..
let me see if your problem is solved or not.
have a nice time
regards
sam.n
You can opt one more solution. Try putting user roles in your user table in database. Now when user will log into your system, check their role and populate it into bean. This way, user bean will always be having their associated role. Put this bean in session scope.
Now after login functionality simply redirect user to a common url using the same code as peter said.
Use a filter to authenticate user to access particular resource like your jsp & servlets. Because you have user bean in session you can always check its role and corresponding url that can be accessed by that particular user..
let me see if your problem is solved or not.
have a nice time
regards
sam.n
![]() |
Similar Threads
- Help me in username and password validation through accessing the database (ASP.NET)
- PHP/MySQL User authentication & redirection (Existing Scripts)
- login scripts (PHP)
- Login used to work (ASP.NET)
- loging in to samba from a xp box (*nix Software)
Other Threads in the JSP Forum
- Previous Thread: JSP and JDBC
- Next Thread: ...how tod evelop a groupware in jsp
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial update video web






