Im fairly new to programming in JSP and I am making this web application where users need to log-in. I've done the registration of users but I am having problems when users are logging-in.
The main problem I am having is that, even though a user is able to successfully log-in, the information from the login form is lost. I need to retain the login information so that I can access the user's information during his/her session using the web application.
Here is the code that I currently have:
index.jsp (this is where the login form is shown)
<form name="Login Form" action="login.jsp"><table>
<tbody>
<tr>
<td><input type="text" name="emaillogin" value="email"/></td>
<td><input type="text" name="passlogin" value="password"/></td>
</tr>
<tr>
<td colspan="2" align="RIGHT><input type="submit" value="login" name="Login"/></td>
</tr>
</tbody></table></form>
login.jsp (this performs checking whether the user is valid or not, and redirects it to the user homepage if valid)
<%!
String email = "";
String password = "";
%>
<%
List users = new ArrayList();
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "root", "pass");
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery("SELECT email,password FROM users");
while (result.next()) {
users.add(result.getString(1));
users.add(result.getString(2));
}
con.close();
email = request.getParameter("emaillogin");
password = request.getParameter("passlogin");
int hasUser = 0;
Iterator<String> it = users.iterator();
while (hasUser == 0 && it.hasNext()) {
if (email.equals(it.next()) && password.equals(it.next())) {
hasUser = 1;
}
}
if (hasUser == 1) {
response.sendRedirect("homepage.jsp");
} else {
response.sendRedirect("index.jsp");
}
%>
homepage.jsp (this is where the user is redirected to after logging in, showing his/her email address)
.
.
<% out.println("Logged in as: " + request.getParameter("email")); %>
.
.
The current problem is that, null value is what I am getting from homepage.jsp. How can I solve this problem?
Thank you!