版权声明
- 本文原创作者:谷哥的小弟
- 作者博客地址:http://blog.csdn.net/lfdfhl
在本节教程中,我们来实现学生列表刷新功能和退出系统登录功能。
学生列表刷新
在学生列表页面点击刷新按钮即可刷新学生列表数据。该功能实现较为简单,主要步骤如下:
- 1、点击刷新后调用ShowStudentServlet
- 2、ShowStudentServlet查询所有学生后再次回调到学生列表界面
退出系统登录
在学生列表页面点击退出按钮即可退出系统登录。
前端页面
studentList.jsp页面,代码如下:
function logout() {
window.location.href = "${pageContext.request.contextPath}/LogoutServlet";
}
图示如下:
LogoutServlet
请在servlet包中创建LogoutServlet,在LogoutServlet中清除Session中保留的用户数据并跳转至登录页面;代码如下:
package com.cn.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
@WebServlet(name = "LogoutServlet", urlPatterns = "/LogoutServlet")
public class LogoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//从Session中移除数据
HttpSession session = request.getSession();
session.removeAttribute("user");
//退出登录后重定向至首页
String contextPath = request.getContextPath();
response.sendRedirect(contextPath+"/index.jsp");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
图示如下: