【问题标题】:Alert inside a jsp page before redirect重定向前在jsp页面内发出警报
【发布时间】:2016-02-29 16:14:12
【问题描述】:

我试图在重定向到另一个页面之前在 JSP 页面上显示警报。如果我不重定向,则会出现警报。但是当我重定向时,警报根本不会出现并立即重定向。有没有办法解决?

代码:

if (rs.next()) {
                ResultSet getHashedPw = st.executeQuery("select password from user where username='" + userid + "' LIMIT 1");
                if(getHashedPw.next()){
                    String hashed = getHashedPw.getString(1);
                    if (BCrypt.checkpw(pwd, hashed)){
                        session.setAttribute("userid", userid);
                        response.sendRedirect("home.jsp");
                    }
                    else {

                        %>

                            <script>
                                alert("Error");
                            </script>

                        <%

                        response.sendRedirect("index.html");//works if I don't have this redirect. 
                    }
                }
            }
            else {
                response.sendRedirect("index.html");
            }

【问题讨论】:

    标签: java jsp


    【解决方案1】:

    您可以尝试在 javascript 中进行重定向。

    <script>
       alert("Error");
       window.location= "index.html";
    </script>
    

    【讨论】:

      【解决方案2】:

      此外,我还鼓励使用 JSTL。它有助于克服将 Java 源代码塞进 html 的问题,随着应用程序的增长,这可能会变得棘手且容易出错。

      这对于数据库驱动的 web 应用程序特别有用。 JSTL 语法起初确实有一点学习曲线,但从长远来看,这是值得的,因为您将拥有一个可供您重复使用的标签存储库。看到这个link

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-25
        相关资源
        最近更新 更多