昨天登录功能中叙述了密码 用户名的数据库验证以及转发 那么这篇文章在昨天的基础上 处理验证码的验证功能,今天需要用到session域,session用于一次会话。
package cn.lijun.demo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//0解决乱码
request.setCharacterEncoding("UTF-8");
//验证码校验
//获得页面输入的验证
String checkCode_client = request.getParameter("checkCode");
//根据验证码存储的session 数据 取出对应的数据
String checkCode_session = (String) request.getSession().getAttribute("checkcode_session");
//对比数据
if(!checkCode_session.equals(checkCode_client)){
request.setAttribute("loginInfo", "您的验证码不正确请重新输入");
//跳转
request.getRequestDispatcher("/login.jsp空间").forward(request, response);
return;
}
//昨天讲的密码 用户名验证
//......
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}