前面一篇实现了用户登录过程,这篇来学习下如何把用户注销。用户注销,实际上就是把session对象给销毁。但是我们前面一篇没有使用session对象,所以这里我们先把前面文章使用request域对象保存user对象改成存储在session中,然后来看用户注销功能的实现过程。
1.优化前一篇代码
前面一篇,我们把用户登录成功的用户对象存在request域中,这个有两点不好。第一,存session更好,多个页面能拿到这个用户对象。第二个没有判断登录失败的情况。现在把LoginServlet.java代码修改如下
package com.anthony.web.servlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import com.anthony.domain.User;
import com.anthony.service.UserService;
import com.anthony.service.impl.UserServiceImpl;
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
//获取表单数据
User user = new User();
try {
BeanUtils.populate(user, req.getParameterMap());
// 调用业务方法
UserService us = new UserServiceImpl();
User u = us.login(user);
//判断u是否为空
if(u !=null) {
//登录成功
//分发转向
req.getSession().setAttribute("u", u);
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}else {
//登录失败就让用户跳转登录页面
resp.sendRedirect(req.getContextPath() +"/login.jsp");
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
添加判断,登录成功就把u对象存到session对象中,如果u等于空,也就是登录失败,就跳转到登录页面。
2.注销功能实现
2.1index.jsp链接添加跳转到LogoutServlet.java
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>网站首页</title>
</head>
<body>
<!-- 如果u(来自request域对象)为空,显示登录和注册按钮,如果不为空,显示欢迎用户消息 -->
<c:if test="${empty u}">
<a href="login.jsp">登录</a>
<a href="register.jsp">注册</a>
</c:if>
<c:if test="${not empty u}">
欢迎您:${u.username}<a href="${pageContext.request.contextPath}/servlet/logoutServlet">注销</a>
</c:if>
</body>
</html>
下面就来写LogoutServlet.java代码
2.2 新建LogoutServlet.java
在web.servlet包新建一个LogoutServlet.java, servlet代码内容如下。
package com.anthony.web.servlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import com.anthony.domain.User;
import com.anthony.service.UserService;
import com.anthony.service.impl.UserServiceImpl;
public class LogoutServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
//点击注销,意味session对象要销毁
req.getSession().invalidate();
//往首页跳转
resp.sendRedirect(req.getContextPath()+"/index.jsp");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
重新部署之后,登录成功,点击注销,会跳转到主页。