JavaScript没有'JSP页面重定向后无法工作..任何解决方案

JavaScript doesn't work after JSP page redirect....Any solution?

本文关键字:工作 解决方案 任何 重定向 没有 JSP JavaScript      更新时间:2024-06-30

以下是代码:

<%
String username = request.getParameter("username");
String password = request.getParameter("password");
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
    con = DataSource.getInstance().getConnection();
    String sql = "select userid,password from users where userid=? and password=?";
    pstmt = con.prepareStatement(sql);
    pstmt.setString(1, username);
    pstmt.setString(2, password);
    rs = pstmt.executeQuery();
    if(!rs.isBeforeFirst())
    {
        response.sendRedirect("login.jsp");
        %>
        <script type="text/javascript">
            var x = document.getElementById("errorbox");
            alert(x);
            x.style.display = "block";
            x.innerHTML = "Ooops...User doesn't exist!!";
        </script>
        <%
    }
}
catch(Exception e)
{
    e.printStackTrace();
}
%>

这是该项目的目录结构。。

WebContent  
|  
|---Meta-Inf  
|  
|---resources  
|      |____CSS  
|      |____JS  
|      |____images  
|---Web-Inf  
|  
|-----login.jsp  
|-----verify.jsp

资源文件包含css、js和images文件夹,用于存储css、javascript和image资源。所有.jsp文件都在WebContent目录下。

当用户无法通过身份验证时,我使用sendRedirect("login.jsp"),在该页面上我有div元素,其display属性设置为none。所以我想使用JavaScript来设置display:block的属性。但是重定向后JavaScript就不起作用了。

Javascript代码绑定到一个网页。

当您在代码示例中发送Javascript代码时,您将它添加到当前在JSP中构建的页面中,在本例中,它就是verify.jsp(我想)。浏览器随后接收该代码和将用户重定向到login.jsp的请求。

当它这样做时,verify.jsp页面和其中包含的所有Javascript代码都会被卸载。如果你想在目标页面login.jsp上执行Javascript代码,你必须将其添加到此页面中。

如果你想将登录页面的验证和呈现保存到不同的文件中,你必须在你重定向到的URL中包括上次登录失败的信息,例如:

response.sendRedirect("login.jsp?loginfailed=1");

并在login.jsp:中对此进行检查

if (request.getParameter("loginfailed") == "1") {
    %>
    <div id="errorbox">
        Ooops...User doesn't exist!!
    </div>
    <%
}