Мне нужна помощь с перенаправлением на другую страницу после входа в систему.
Мне удается сделать обычное перенаправление входа в систему на appDashboard.jsp
, однако, я пытаюсь сделать это, если пользователь впервые входит в систему, затем онибудет перенаправлен на appSetup.jsp
, а если нет, то будет перенаправлен на appDashboard.jsp
.Как я могу это сделать?
Данные для FIRST_TIME
в базе данных вставлены как yes
при регистрации.Ниже приведены мои коды.
appLogin.jsp
<!-- 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>
ApplicantLoginServlet.java
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);
}
}
ApplicantDA.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;
}
ApplicantBean.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;
}
}
Это мой первый учебный сервлет.Заранее спасибо!