使用jsp中的会话登录页面

时间:2015-07-29 09:31:16

标签: java jsp

我已经使用jsp创建了一个登录环境。 index.jsp, login.jsp。我从数据库中获取用户名和密码。如果用户名和密码与数据库匹配,则它会完美地登录。当用户提供错误的名称或密码时,它会显示错误消息invalid name or password并重定向到登录页面。没有什么不对,但我首先登录时遇到问题。提交错误名称或密码后显示错误消息的位置 null

为什么显示null?

以下是我的代码

的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);


 }
%>

5 个答案:

答案 0 :(得分:2)

您可以在index.jsp

中使用它
<% if ((String) request.getAttribute("errorMessage") != null) {%>
            <h3 style="color: red;"><%=(String) request.getAttribute("errorMessage")%></h3>
            <%
                }
            %>

而不是这个

<%=request.getAttribute("errorMessage")%>

答案 1 :(得分:1)

第一次请求中无法使用errorMessage属性。请检查空值,然后显示消息。

<%=request.getAttribute("errorMessage")%>

以下代码可以帮助您。

<tr>
    <th colspan="2" align ="left">Login Here</th>
    <h5>
        <%
        String errorMsg = request.getAttribute("errorMessage");
        if (errorMsg != null) {
        %>
            <%=errorMsg%> 
        <%
        }
        %>
    </h5>
</tr>

答案 2 :(得分:1)

请在显示错误之前检查条件,如果请求参数为null则不应该呈现,否则它将被渲染,根据以下代码段修改代码

python -c "a=5
for i in range(a): print i"

答案 3 :(得分:1)

由于第一次加载时请求中的errorMessage属性为null,因此您将获得null。您需要为其添加 null check 。你可以这样做

<th colspan="2" align ="left">Login Here</th>
<% if (request.getAttribute("errorMessage") != null) {
        out.println("<h5>" + request.getAttribute("errorMessage") + "</h5>");
}%>

答案 4 :(得分:1)

在login.jsp

中更正您的代码

写session.setAttribute(“errorMessage”,“无效的用户或密码”);

而不是request.setAttribute(“errorMessage”,“无效的用户或密码”);

...

因此而显示空信息。