JavaScript 在 JSP 页面重定向后不起作用....有什么解决办法吗?
JavaScript doesn't work after JSP page redirect....Any solution?
代码如下:
<%
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和图片资源。所有 .jsp 文件都在 WebContent 目录下。
当用户无法通过身份验证时,我使用 sendRedirect("login.jsp") 并且在该页面上我有 div 元素,其显示 属性 设置为 none.所以我想使用 JavaScript 将 属性 设置为 display:block。但是 JavaScript 在重定向后不起作用。
Javascript代码绑定到一个网页。
当您在代码示例中发送 Javascript 代码时,您会将其添加到当前在 JSP 中构建的页面,在本例中为 verify.jsp
(我想)。然后浏览器收到此代码和将用户重定向到 login.jsp
.
的请求
执行此操作时,verify.jsp
页面将被卸载,其中包含的所有 Javascript 代码也将被卸载。如果您希望 Javascript 代码在您的目标页面 login.jsp
上执行,您必须将其添加到此页面中。
如果您想将登录页面的验证和呈现保留在不同的文件中,则必须在要重定向到的 URL 中包含上次登录失败的信息, 例如:
response.sendRedirect("login.jsp?loginfailed=1");
并在 login.jsp
中检查:
if (request.getParameter("loginfailed") == "1") {
%>
<div id="errorbox">
Ooops...User doesn't exist!!
</div>
<%
}
代码如下:
<%
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和图片资源。所有 .jsp 文件都在 WebContent 目录下。
当用户无法通过身份验证时,我使用 sendRedirect("login.jsp") 并且在该页面上我有 div 元素,其显示 属性 设置为 none.所以我想使用 JavaScript 将 属性 设置为 display:block。但是 JavaScript 在重定向后不起作用。
Javascript代码绑定到一个网页。
当您在代码示例中发送 Javascript 代码时,您会将其添加到当前在 JSP 中构建的页面,在本例中为 verify.jsp
(我想)。然后浏览器收到此代码和将用户重定向到 login.jsp
.
执行此操作时,verify.jsp
页面将被卸载,其中包含的所有 Javascript 代码也将被卸载。如果您希望 Javascript 代码在您的目标页面 login.jsp
上执行,您必须将其添加到此页面中。
如果您想将登录页面的验证和呈现保留在不同的文件中,则必须在要重定向到的 URL 中包含上次登录失败的信息, 例如:
response.sendRedirect("login.jsp?loginfailed=1");
并在 login.jsp
中检查:
if (request.getParameter("loginfailed") == "1") {
%>
<div id="errorbox">
Ooops...User doesn't exist!!
</div>
<%
}