Hello I have this code in a servlet in netbeans and I have a problem that when I login as a valid user or invalid it keeps this url localhost:8080/LogIN123/login
.
The login is the servlet and it doesn't response for the next page.
Can anyone help me?
The program must enter the username and password, then if the Id
for user are the same for supervisor it will resend to another page
that has employee under him and if he is just an employee it will redirect him to a page that have information about him without editing.
public class login extends HttpServlet {
String url = "jdbc:mysql://localhost:3306/";
String dbName = "employee";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name;
String pass;
Connection conn;
Statement Stmt;
ResultSet rs;
try {
name = request.getParameter("username");
pass = request.getParameter("password");
Class.forName(driver);
conn = (Connection) DriverManager.getConnection(url + dbName, userName, password);
Stmt = conn.createStatement();
PreparedStatement ps = conn.prepareStatement("select * from emp_info where username=? && password=?");
rs = ps.executeQuery();
here I have class name userbean that contanis the data from the data base
UserBean userBean = new UserBean();
and here a listof beans that will be sends to the other pages it contains the user info
List<UserBean> listOfUserBean = new ArrayList<UserBean>();
if (rs.next()) {
userBean.setUserID(rs.getString("id"));
userBean.setUserName(rs.getString("username"));
userBean.setUserGender(rs.getString("gender"));
userBean.setUserSupervisour(rs.getString("supervisour"));
userBean.setUserBirthDay(rs.getString("BirthOfDate"));
userBean.setUserSalary(rs.getString("salary"));
}
listOfUserBean.add(userBean);
response.sendRedirect("mangerpage.jsp");
request.setAttribute("userlist", listOfUserBean);
the code below checks if the user id are the same supervisor it means that he is the manager of some employee and he will get new page that show all employees under his management
if (userBean.getUserID().equals(userBean.getUserSupervisour())) {
Stmt = conn.createStatement();
PreparedStatement ps2 = conn.prepareStatement("select * from emp_info where supervisour=?");
ResultSet rs2 = ps2.executeQuery();
if (rs2.next()) {
userBean.setUserID(rs2.getString("id"));
userBean.setUserName(rs2.getString("username"));
userBean.setUserGender(rs2.getString("gender"));
userBean.setUserSupervisour(rs2.getString("supervisour"));
userBean.setUserBirthDay(rs2.getString("BirthOfDate"));
userBean.setUserSalary(rs2.getString("salary"));
}
listOfUserBean.add(userBean);
request.setAttribute("userlist",listOfUserBean);
response.sendRedirect("mangerpage.jsp");
} else {
request.setAttribute("userlist",listOfUserBean);
request.setAttribute("name", name);
response.sendRedirect("mypage.jsp");
}
if (userBean.getUserID().equals("")) {
response.sendRedirect("flogin.jsp");
}
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
out.close();
}
}