创建一个web项目LoginDemo
配置web服务器
配置成功
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190909181827345.png创建四个页面login.jsp、do_login.jsp、success.jsp和failure.jsp页面
将login.jsp设置为项目首页文件
在login.jsp里写入入代码
<head>
<title>登录页面</title>
</head>
<body>
<h3 style="text-align: center">用户登录</h3>
<form action="do_login.jsp" method="post">
<table border="1" cellpadding="10" style="margin: 0px auto">
<tr>
<td align="center">用户名</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td align="center">密 码</td>
<td><input type="password" name="password"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="登录"/><input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</body>
在do_login.jsp里写入入代码
<head>
<title>Title</title>
</head>
<body><%
//获取登录表单数据
String username = request.getParameter("username");
String password = request.getParameter("password");
//判断登录是否成功
if(username.equals("张三丰") && password.equals("12345")){
//采用重定向,跳转到登录成功页面
response.sendRedirect("success.jsp?username="+ username );
}else{
//采用重定向,跳转到登录失败页面
response.sendRedirect("failure.jsp?username=" + username);
}
%>
</body>
在success.jsp里写入入代码
<head>
<title>登录成功</title>
</head>
<body>
<h3><%= request.getParameter("username")%>,登录成功!</h3>
</body>
在failure.jsp里写入入代码
<head>
<title>登录失败</title>
</head>
<body>
<h3><%= request.getParameter("username")%>,登录失败!</h3>
</body>
运行结果为:
登录失败页面: