第十一章 从0-1搭建一个简单的JavaWeb系统(三)
目录
一、工程代码结构
二、代码实现
三、运行效果
四、未完待续
本章节的每一段代码,建议全部自己敲一遍,加深印象,切勿直接复制黏贴。
一、工程代码结构
本章节实现注销(退出)功能,以下图片中标红的是新增的代码
二、代码实现
下图的SysFilter过滤器主要实现拦截访问的功能,对于用户访问页面时,Session中获取不到用户信息(即未登录)的情况进行拦截,重定向到Error页面(该页面可以再次连接到登录页面)。
package com.example.filter;import com.example.pojo.User;import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;public class SysFilter implements Filter {public void init(FilterConfig config) throws ServletException {}public void destroy() {}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {HttpServletRequest httpServletRequest = (HttpServletRequest)request;HttpServletResponse httpServletResponse = (HttpServletResponse)response;HttpSession session = httpServletRequest.getSession();User user = (User)session.getAttribute("userSession");if (user == null) {httpServletResponse.sendRedirect((((HttpServletRequest) request).getContextPath() + "/error.jsp"));} else {chain.doFilter(request, response);}}
}
LogoutServlet接收用户的注销登录请求,具体实现是用户点击该页面的退出按钮时传递请求给LogoutServlet。
package com.example.servlet;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;public class LogoutServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {HttpSession session = request.getSession();session.removeAttribute("userSession");response.sendRedirect(request.getContextPath() + "/login.jsp");}
}
页面上的.../jsp/logout.do请求路径对应的是web.xml中配置的LogoutServlet请求路径:
web.xml中相较上一章节,增加了LogoutServlet和SysFilter:
<?xml version="1.0" encoding="UTF-8"?>
<web-appversion="4.0"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xml="http://www.w3.org/XML/1998/namespace"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"><display-name>Archetype Created Web Application</display-name><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>com.example.filter.CharacterEncodingFilter</filter-class></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter><filter-name>SysFilter</filter-name><filter-class>com.example.filter.SysFilter</filter-class></filter><filter-mapping><filter-name>SysFilter</filter-name><url-pattern>/jsp/*</url-pattern></filter-mapping><servlet><servlet-name>LoginServlet</servlet-name><servlet-class>com.example.servlet.LoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>LoginServlet</servlet-name><url-pattern>/login.do</url-pattern></servlet-mapping><servlet><servlet-name>LogoutServlet</servlet-name><servlet-class>com.example.servlet.LogoutServlet</servlet-class></servlet><servlet-mapping><servlet-name>LogoutServlet</servlet-name><url-pattern>/jsp/logout.do</url-pattern></servlet-mapping><!-- 设置欢迎页面 --><welcome-file-list><welcome-file>/login.jsp</welcome-file></welcome-file-list>
</web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8" %>
<!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><p>请登录后再访问该页面!<a href="login.jsp" style="color: #0cde28" >返回</a>
</p>
<p><img src="${pageContext.request.contextPath}/images/权限不够.png" alt="" align="middle" height="600px" width="1000px">
</p></body>
</html>
三、运行效果
点击退出按钮和退出系统,实现了退出登录效果
四、未完待续
本系统搭建教程会每日持续更新,请继续关注后续章节……