0

我正在尝试在登录时使用 Javascript 和 JSP 实现警报提示,但我看不到成功。到目前为止,我所拥有的只是一条错误消息,我想将其作为提示。请帮忙。这就是我所拥有的:

小服务程序端:

String userValidate = logindao.authenticateUser(loginbean);
        if(userValidate.equals("SUCCESS")){
            request.setAttribute("userName", userName);
            request.getRequestDispatcher("/Home.jsp").forward(request, response);
        }else{
            request.setAttribute("errMessage", userValidate);
            request.getRequestDispatcher("/Login.jsp").forward(request, response);
        }

我的 JSP 方面:

<td><%=(request.getAttribute("errMessage") == null)
                                    ? ""
                                    : request.getAttribute("errMessage")%></td>

我的道:

public class LoginDao {
    public String authenticateUser(LoginBean loginbean){
        String userName = loginbean.getUserName();
        String password = loginbean.getPassword();

        Connection conn = null;
        String userNameDB = "";
        String passwordDB = "";
        DBConnection db = new DBConnection();
        try{
            conn = db.getDbConnection();
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT USER_NAME, P_WORD, WORK_CLASS FROM USER_LOGIN "
                    + "where USER_NAME = '" + userName + "'");

            while(rs.next()){
                userNameDB = rs.getString("USER_NAME");
                passwordDB = rs.getString("P_WORD");
            }
            if(userName.equalsIgnoreCase(userNameDB)&& password.equalsIgnoreCase(passwordDB)){
                return "SUCCESS";
            }
            if(userName.equals("") || password.equals("")){
                return "Username or Password is blank";
            }
        }catch(Exception asd){
            System.out.println(asd);
        }
        return "invalid user credentials";
    }
}

如果证书有误,我得到的只是页面上打印的错误消息。我怎样才能将其作为提示?

4

1 回答 1

2

在您的 JSP 中添加以下内容

<c:if test="${not empty errMessage}">
    <script>alert("${errMessage}");</script>
</c:if>

我已经使用 EL,因为在 JSP 中使用 scriptlet 已被弃用。该语法只是按顺序从pagerequestsessionapplication范围中${attr}提取名为attr的属性的值作为 test 。

编辑:(调试为)

<p>errMessage is: '<%= request.getAttribute("errMessage") %>'</p>
<c:if test="${not empty errMessage}">
    <script>alert("${errMessage}");</script>
</c:if>
<% request.setAttribute("errMessage", "forced error message"); %>
<c:if test="${not empty errMessage}">
    <script>alert("${errMessage}");</script>
</c:if>
于 2013-07-06T03:47:00.073 回答