当前位置: 首页 > news >正文

第十一章 从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>

三、运行效果

点击退出按钮和退出系统,实现了退出登录效果

四、未完待续

本系统搭建教程会每日持续更新,请继续关注后续章节……


http://www.mrgr.cn/news/34206.html

相关文章:

  • Python 列表操作:深入理解与高效实践
  • Science:这才是参加学术会议的正确打开方式!
  • 20 基于STM32的温度、电流、电压检测proteus仿真系统(OLED、DHT11、继电器、电机)
  • Web自动化测试
  • RS-232,422,485应用详解
  • 全方位洗衣洗鞋小程序系统,重塑干洗店服务新体验;
  • Java的cnum类型
  • 在Windows系统上安装的 Boost C++ 库
  • ???Ansible——Playbook基本功能
  • 基于flask常见trick——unicode进制编码绕过
  • 【Elasticsearch系列廿一】ES7 SQL 新特性
  • 阿里Arthas-Java诊断工具,基本操作和命令使用
  • 类与对象(中)
  • MySQL学习笔记(持续更新中)
  • 如何降低H5商城系统的开发成本
  • Find My化妆镜|苹果Find My技术与化妆镜结合,智能防丢,全球定位
  • 企微社群管理:构建高效沟通与活跃氛围的策略与实践
  • 昇思MindSpore进阶教程-参数
  • MATLAB在无线传感器网络设计中的应用与实践
  • 从零开始之AI面试小程序