1

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();
        }
    }
4

2 回答 2

0

由于您在设置属性之前重定向到页面,因此它显示为空白可能是因为列表为空。

只需设置userlist属性,然后再重定向到managerpage.jsp

request.setAttribute("userlist", listOfUserBean);
response.sendRedirect("mangerpage.jsp");

您可以在页面上检查用户列表是否为空。

于 2012-11-05T13:29:41.367 回答
0

您必须已将您的 servlet 映射到 action login

我认为您没有正确重定向到您的 JSP。

response.sendRedirect("mangerpage.jsp");您应该给出绝对路径或正确的相对路径。

对于相对路径将需要查看您的目录结构进行评论。

看看这个

于 2012-11-05T11:21:11.883 回答