JSP简单网页登陆

练习题:编写一个JSP程序,实现用户登陆,当用户输入的用户名或者密码错误的时候,将页面重定向到错误提示页,并在该页面显示30秒后,自动返回到用户登陆页面。

首先写一个登陆页面 login.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登陆页面</title>
</head>
<body>
<form name="form1" method="post" action="check.jsp">
<table>
<tr>
<td width="50%" >用户名:</td>
<td width="50%"><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td>密&nbsp;&nbsp;码:</td>
<td><input type="text" name="pwd" id="pwd"></td>
</tr>
<tr>
<td align="center"><input type="submit" name="Submit" value="登陆" ></td>
<td align="center"><input type="reset" name="Reset" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>

然后写一个验证页面,点击上面的登陆就到这里面验证,如果对的就输出登陆成功,如果错误的就跳到错误提示页

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<%
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
%>
<%
if("mr".equals(name)&&"pwd".equals(pwd)){
    out.print("<script language='javascript'>alert('登录成功!');window.location.href='login.jsp';</script>");
}else{
    response.sendRedirect("mistake.jsp");  //重定向网页
}
%>
</body>
</html>

错误提示页,然后30秒后返回登陆页面

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
response.setHeader("refresh","30;URL=login.jsp");  //设置30秒后刷新到指定页面
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>错误页面</title>
</head>
<body>
您输入的用户名或者密码错误,请检查后重新输入!
</body>
</html>

猜你喜欢

转载自blog.csdn.net/lx127372/article/details/81106928
今日推荐