Servlet登录重定向到其他JSP

时间:2018-11-24 10:16:47

标签: java jsp servlets

登录后我需要帮助重定向到其他页面。
我设法将普通登录重定向到appDashboard.jsp,但是,我尝试做的是,如果用户首次登录系统,则将他们重定向到appSetup.jsp,否则将被重定向到appDashboard.jsp。重定向到FIRST_TIME。我该怎么办?

在注册过程中,数据库中yes的数据作为<!-- Form Module--> <div class="module form-module"> <div class="toggle"><i class="fa fa-times fa-pencil"></i> <div class="tooltip" style=" margin-right: -110px;">Not a member? <a href="appRegister.jsp" style="color: gray">Register Here</a></div> </div> <!-- LOGIN FORM--> <div class="form"> <h2>Login to your account</h2> <form action="AppLogin"> <input type="text" placeholder="Username" name="appusername"/> <input type="password" placeholder="Password" name="apppassword"/> <button type="submit" name="login">Login</button> </form> </div> <div class="cta"><a href="forgotPassword.jsp">Forgot your password?</a></div> </div> 插入。下面是我的代码。

appLogin.jsp

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        ApplicantBean applicant = new ApplicantBean();
        applicant.setUsername(request.getParameter("appusername"));
        applicant.setPassword(request.getParameter("apppassword"));

        applicant = ApplicantDA.Login(applicant);

        if(applicant.isValid()) {
            HttpSession session = request.getSession(true); //creating session
            session.setAttribute("currentApplicant", applicant);
            PrintWriter out = response.getWriter();

            String firstTime = applicant.getFirstTime();
            if(firstTime == "yes" ) {
                System.out.println("ft " + firstTime);
                response.setContentType("text/html");  
                response.setContentType("text/html");  
                out.println("<script type=\"text/javascript\">");  
                out.println("alert('Successfully Logged In.');");
                out.println("window.location= \"appSetup.jsp\"");
                out.println("</script>");
            }
            else {
                response.setContentType("text/html");  
                response.setContentType("text/html");  
                out.println("<script type=\"text/javascript\">");  
                out.println("alert('Successfully Logged In.');");
                out.println("window.location= \"appDashboard.jsp\"");
                out.println("</script>");
            }
        }
        else {
            PrintWriter out = response.getWriter();  
            response.setContentType("text/html");  
            out.println("<script type=\"text/javascript\">");  
            out.println("alert('Sorry, invalid username or password.');");
            out.println("window.location= \"appLogin.jsp\"");
            out.println("</script>");

            //response.sendRedirect("InvalidLogin.jsp");
        }
    }
    catch (Throwable theException) {
        System.out.println(theException);
    }
}

ApplicantLoginServlet.java

public static ApplicantBean Login(ApplicantBean bean) { //ApplicantLoginServlet line 24.
    //preparing some objects for connection
    Statement stmt = null;

    String username = bean.getUsername();
    String password = bean.getPassword();

    String searchQuery = "SELECT * FROM APPLICANT WHERE APPLICANT_USERNAME='" + username + "'AND APPLICANT_PASSWORD='" + password + "'";

    //"System.out.println" prints in the console; Normally used to trace the process
    System.out.println("Your username is " + username);
    System.out.println("Your password is " + password);
    System.out.println("Query : " + searchQuery);

    try {
        //Connect to DB
        currentCon = ConnectionManager.getConnection();
        stmt = currentCon.createStatement();
        rs = stmt.executeQuery(searchQuery);
        boolean more = rs.next();

        if(!more) {
            System.out.println("Sorry, you are not a registered user! Please sign up first.");
            bean.setValid(false);
        }
        else if(more) {
            String fullname = rs.getString("APPLICANT_FULLNAME");
            String email = rs.getString("APPLICANT_EMAIL");
            String image = rs.getString("APPLICANT_IMAGE");
            String firstTime = rs.getString("FIRST_TIME");
            System.out.println("Welcome " + fullname);
            System.out.println("Email : " + email);
            System.out.println("Image : " + image);
            System.out.println("First Time : " + firstTime);
            bean.setFullname(fullname);
            bean.setEmail(email);
            bean.setImage(image);
            bean.setValid(true);
        }
    }
    catch(Exception ex) {
        System.out.println("Login failed: An Exception has occured! " + ex);
    }

    //some exception handling
    finally {
        if(rs != null) {
            try {
                rs.close();
            }
            catch(Exception e) {
            }
            rs = null;
        }
        if(stmt != null) {
            try {
                stmt.close();
            }
            catch(Exception e) {
            }
            stmt = null;
        }
        if(currentCon != null) {
            try {
                currentCon.close();
            }
            catch(Exception e) {
            }
            currentCon = null;
        }
    }
    return bean;
}

ApplicantDA.java

public class ApplicantBean {
    private String fullname;
    private String username;
    private String email;
    private String password;
    private String image;
    private String firstTime;
    public boolean valid;

    public String getFullname() { return fullname; }
    public String getUsername() { return username; }
    public String getEmail() { return email; }
    public String getPassword() { return password; }
    public String getImage() { return image; }
    public String getFirstTime() { return firstTime; }

    public void setFullname(String newFullname) {
        fullname = newFullname;
    }

    public void setUsername(String newUsername) {
        username = newUsername;
    }

    public void setEmail(String newEmail) {
        email = newEmail;
    }

    public void setPassword(String newPassword) {
        password = newPassword;
    }

    public void setImage(String newImage) {
        image = newImage;
    }

    public void setFirstTime(String newFirstTime) {
        firstTime = newFirstTime;
    }

    public boolean isValid() {
        return valid;
    }

    public void setValid(boolean newValid) {
        valid = newValid;
    }
}

ApplicantBean.java

{{1}}

这是我第一次学习servlet。提前谢谢!

0 个答案:

没有答案
相关问题