@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
String checkcode=request.getParameter("checkcode");
HttpSession session = request.getSession();
String checkCode_session = (String)session.getAttribute("checkCode_session");
session.removeAttribute("checkCode_session");
if(checkCode_session!=null&&checkCode_session.equalsIgnoreCase(checkcode)){
if("zhangsan".equals(username)){
session.setAttribute("user",username);
response.sendRedirect(request.getContextPath()+"/success.jsp");
}else{
request.setAttribute("login_error","用户名或者密码错误");
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}else{
request.setAttribute("cc_error","验证码错误");
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
} |
|