登录jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%-- 验证码登录页面,并自动刷新图片 --%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
function _change(){
var imgEle = document.getElementById("img");
//如果不加参数,IE浏览器中无法进行刷新验证码图片,因为浏览器缓存了请求的地址;所以要增肌参数,去实时进行请求参数,服务器不会获取参数
imgEle.src = "/JavaWEB11/LoginCodeServlet?a=" + new Date().getTime();
}
</script>
<%
String message = "";
String msg = (String)request.getAttribute("msg");
if(msg != null)
message = msg;
%>
<font color="red"><b><%=message %></b></font>
<form action="/JavaWEB11/LoginCodeVerifyServlet" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" value="lisi" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td>验证码</td>
</tr>
<tr>
<td>请输入验证码</td>
<td><input type="text" name="code"></td>
</tr>
<tr>
<!-- 请求到 http://127.0.0.1:8081/JavaWEB11/LoginCodeServlet,返回验证码图片 -->
<td><img id="img" src="/JavaWEB11/LoginCodeServlet"/></td>
<td><a href="javascript:_change()">换一张</a></td>
</tr>
<tr>
<td><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
后台Servlet验证输入的验证码和生成验证码是否一致
package cn.test.javaweb12;
/*
* 验证验登录验证后台代码页面
*/
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LoginCodeServlet
*/
@WebServlet(name = "LoginCodeVerfiyServlet", urlPatterns = { "/LoginCodeVerfiyServlet" })
public class LoginCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginCodeServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
/*
* 校验验证码
* 1.从session中获取正确的验证码
* 2.从表单中获取用户填写的验证码
* 3.进行比较
* 4.如果相同,向下运行;否则保存错误信息到request域中,转发到登录页面
*/
request.setCharacterEncoding("utf-8");
String code = request.getParameter("code");
String sessionCode = (String) request.getSession().getAttribute("session_code");
if(!code.equals(sessionCode)){
request.setAttribute("msg", "验证码错误,请重新登录");
//进行转发,转发的地址不需要包含项目名
request.getRequestDispatcher("/session/loginFail2.jsp").forward(request, response);
return;
}
String name = request.getParameter("username");
String passwd = request.getParameter("password");
request.getSession().setAttribute("name", name);
//请求重定向的地址需要包含项目名
response.sendRedirect("/JavaWEB11/session/loginSuccess.jsp");
}
}
输入验证码和后台验证码不一致,servlet转发登录失败的jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
alert("你还没有登录呢");
</script>
</head>
<body>
<div><%=request.getAttribute("msg")%></div>
<%--
response.sendRedirect("/JavaWEB11/Login.jsp");
--%>
</body>
</html>
输入验证码和后台验证码一致,servlet转发登录成功的jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>登录成功</h2>
<%
String name = (String)session.getAttribute("name");
if(name == null)
{
request.setAttribute("msg", "您还没有登录");
request.getRequestDispatcher("/session/loginFail2.jsp").forward(request, response);
return;
}
%>
<div>欢迎<%=session.getAttribute("name") %>登录成功</div>
</body>
</html>