I utworzeniu środowiska logowania za pomocą JSP. index.jsp, login.jsp
, Biorę nazwę użytkownika i hasło z bazy danych. Jeśli nazwa użytkownika i hasło mecze z bazy danych czyni proces logowania doskonale. Kiedy użytkownik dać złą nazwę i hasło to pokazuje się komunikat o błędzie invalid name or password
i przekierowanie do strony logowania. Nic w tym złego, ale ja w obliczu problemu, kiedy się najpierw zalogować. W miejscu, gdzie komunikat o błędzie jest wyświetlany po przesłaniu niewłaściwej nazwy lub hasła, które miejsce jest wyświetlana wartość null .
Dlaczego zerowy pokazuje?
Poniżej jest kod
index.jsp
<%@page contentType=text/html pageEncoding=UTF-8%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=Content-Type content=text/html; charset=UTF-8>
<title>JSP Example</title>
</head>
<body>
<form action=login.jsp method=post>
<center>
<table border=1 width=30% cellpadding=3>
<thead>
<tr>
<th colspan=2 align =left>Login Here</th> <h5><%=request.getAttribute(errorMessage)%> </h5>
</tr>
</thead>
<tbody>
<tr>
<td>User Name</td>
<td><input type=text name=uname value= /></td>
</tr>
<tr>
<td>Password</td>
<td><input type=password name=pass value= /></td>
</tr>
<tr>
<td><input type=submit value=Login /></td>
<td><input type=reset value=Reset /></td>
</tr>
</tbody>
</table>
</center>
</form>
</body>
</html>
login.jsp
<%@ page import =java.sql.* %>
<%
String userid = request.getParameter(uname);
String pwd = request.getParameter(pass);
Class.forName(org.postgresql.Driver);
Connection con = DriverManager.getConnection(jdbc:postgresql://localhost:5432/test, postgres, root);
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery(select * from member where uname=' + userid + ' and pass=' + pwd + ');
if (rs.next()) {
session.setAttribute(userid, userid);
response.sendRedirect(success.jsp);
} else {
request.setAttribute(errorMessage, Invalid user or password);
request.getRequestDispatcher(/index.jsp).forward(request, response);
}
%>