以下是代码:
<%
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和图像文件夹来存储css、javascript和图像资源。所有.jsp文件都在WebContent目录下。
当无法对用户进行身份验证时,我使用sendRedirect("login.jsp"),在该页面上有div元素,其显示属性设置为none。因此,我希望使用JavaScript将属性设置为display:block。但是JavaScript在重定向之后就不起作用了。
发布于 2015-02-28 19:14:46
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>
<%
}https://stackoverflow.com/questions/28785381
复制相似问题