验证码登录和退出
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"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascrpit">
function changeImg(){
document.getElementById("img1").src = "/web03/CheckImgServler?time" + new Date().getTime();
}
</script>
</head>
<%
String msg = "";
if(request.getAttribute("msg") !=null){
msg = (String)request.getAttribute("msg");
}
%>
<body>
<h1>登录页</h1>
<from action="/web03/LoginServlet" method="post">
<h3><font color="red"><%= msg %></h3>
<table border="1" width="400">
<tr>
<td>用户名</td>
<ts><input type="text" name="username" value="${ cookie.remember.value} }"></ts>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>验证码</td>
<td>
<input type="text" name="checkcode" size="6">
<img id="img1" src="/web03/CheckImgServelt">
< a href="javascript:;" onclick="changeImg">看不清,换一张</ a>
</tr>
</table>
</body>
</html>
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);
}
}