2020-03-28

验证码登录和退出

package controller;

import java.io.IOException;

import java.sql.SQLException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import domain.User;

import model.UserModel;

/**

* Servlet implementation class LoginServlet

*/

@WebServlet("/LoginServlet")

public class LoginServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

     request.setCharacterEncoding("UTF-8");

  try {

   String username = request.getParameter("username");

   String password = request.getParameter("password");

   User user = new User();

   user.setUsername(username);

   user.setPassword(password);

   String checkcode1 = request.getParameter("checkckcode");

   String checkcode2 = (String) request.getSession().getAttribute("checkcode");

   request.getSession().removeAttribute("checkcode");

   if(!checkcode1.equalsIgnoreCase(checkcode2)) {

    request.setAttribute("msg", "验证码输入错误!");

    request.getRequestDispatcher("/login.jsp").forward(request, response);

    return;

   }

   UserModel userModel = new UserModel();

   User existUser = userModel.login(user);

   if(existUser == null) {

    //登录失败

    request.setAttribute("msg", "用户名或密码错误!");

    request.getRequestDispatcher("/login.jsp").forward(request, response);

   }else {

    //登录成功

    HttpSession session = request.getSession();

    session.setAttribute("existUser", existUser);

    String remember = request.getParameter("remember");

    if("true".equals(remember)) {

     Cookie cookie = new Cookie("remember",existUser.getUsername());

     cookie.setPath("/web03");

     cookie.setMaxAge(60*60*24);

     response.addCookie(cookie);

    }

    response.sendRedirect("/web03/success.jsp");

   }

  } catch (SQLException e) {

   e.printStackTrace();

   throw new Runtime

throw new RuntimeException();

  }

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  doGet(request, response);

}

}

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

Insert title here

<%

   String msg = "";

   if(request.getAttribute("msg") !=null){

    msg = (String)request.getAttribute("msg");

   }

%>

登录页

<%= msg %>

   

   

   

   

   

   

   

   

   

   

   

   

用户名
密码
验证码

   

   

    < a href="javascript:;" onclick="changeImg">看不清,换一张

   

package controller;

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 LogoutServlet

*/

@WebServlet("/LogoutServlet")

public class LogoutServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

request.getSession().invalidate();

request.sendRedirect("/web03/success.jsp");

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);

}

}

你可能感兴趣的:(2020-03-28)