比如用户登录时输入的密码错了,验证后回到登录页面,用一段红字提示他错误
<span>密码错误</span>,这个是怎么实现的啊?

解决方案 »

  1.   

    也不一定要用ajax的。struts就有这个。
    其实自己实现起来也很容易的。
    我就用纯jsp给你个很简单的例子。
    登录页面:login.jsp
    <%
    Exception e = (Exception)request.getAttribute("LOGIN_EXP");
    if( e!=null ){
        out.print("<span style='font-weight:bold;color:red;'>");
        out.print(e.getMessage());
        out.println("</span>");
    }
    %>
    用户名:~~~~~
    密码:~~~~~~
    确定
    执行jsp:dologin.jsp
    <%
    Exception e = null;
    ~~~~~~
    if( 是否存在条件 ){
        e = new Exception();
        e.setMessage("无此用户");
    }else if( 密码判断条件 ){
        e = new Exception();
        e.setMessage("密码错误");
    }
    if( e != null ){
        request.setAttribute("LOGIN_EXP",e);
        request.getRequestDispatcher("login.jsp").forward(request,response);
    }else{
       response.sendRedirect("Success.jsp");
    }%>