javaweb-02
Cookie
Cookie是某些网站为了辨别用户身份,进行Session跟踪而储存在用户本地终端上的数据(通常经过加密),由用户客户端计算机暂时或永久保存的信息
新建Maven项目
什么是session?
在Jsp中,session是使用bean的一个生存期限,一般为page,session意思是在这个用户没有离开网站之前一直有效,如果无法判断用户何时离开,一般依据系统设定,tomcat中设定为30分钟
当用户打开浏览器,访问某个网站的时候,服务器就会在服务器的内存为该浏览器分配一个内存空间,该空间被这个浏览器独占,这个空间就是session空间。
该空间中的数据默认存在时间为30min,在tomcat的web.xml中的
session可以用来做什么?
1)可以用作网上商城的购物车
2)保存登录用户的信息
3)将某些数据放在session中,供同一用户的各个页面使用(共享数据)
4)防止用户非法登录到某个页面
客户端 服务端
- 服务端给客户端一个 信件,客户端下次访问服务端带上信件就可以了; cookie
- 服务器登记你来过了,下次你来的时候我来匹配你; seesion
保存会话的两种技术
cookie
- 客户端技术 (响应,请求)
session
- 服务器技术,利用这个技术,可以保存用户的会话信息,把数据放在Session中
Cookie[] cookies = req.getCookies(); //获得Cookie
cookie.getName(); //获得cookie中的key
cookie.getValue(); //获得cookie中的vlaue
new Cookie("lastLoginTime", System.currentTimeMillis()+""); //新建一个cookie
cookie.setMaxAge(24*60*60); //设置cookie的有效期
resp.addCookie(cookie); //响应给客户端一个cookie
cookie一般会保存在本地用户目录下 appdata;
服务器响应给客户端cookie
//保存用户上一次访问的时间
public class CookieDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-16");
resp.setCharacterEncoding("utf-16");
PrintWriter out = resp.getWriter();
//Cookie 服务器端从客户端获取
Cookie[] cookies = req.getCookies();//这里返回数组,说明Cookie可能存在多个
//判断Cookie是否存在
if(cookies!=null) {
//如果存在怎么办
out.write("你上一次访问的时间是:");
for (int i = 0; i <cookies.length; i++) {
Cookie cookie = cookies[i];
//获取cookie的名字
if (cookie.getName().equals("lastLoginTime")) {
//获取cookie中的值
;
long lastLoginTime = Long.parseLong(cookie.getValue());
Date date = new Date(lastLoginTime);
out.write(date.toLocaleString());
}
}
}else {
out.write("这是您第一次访问本站");
}
//服务器给客户端响应一个cookie
Cookie cookie = new Cookie("lastLoginTime",System.currentTimeMillis()+"");
//cookie有效期1天
cookie.setMaxAge(24*60*60);
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
测试运行:
- 一个Cookie只能保存一个信息;
- 一个web站点可以给浏览器发送多个cookie,最多存放20个cookie;
- 浏览器的cookie上限为300个
- Cookie大小有限制
删除Cookie:
- 不设置有效期,关闭浏览器,自动失效;
- 设置有效期时间为0
public class CookieDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建一个cookie,名字必须和要删除的名字一致
Cookie cookie = new Cookie("lastLoginTime",System.currentTimeMillis()+"");
//cookie立马过期
cookie.setMaxAge(0);
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
拿取cookie中文字符:
public class CookieDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-16");
resp.setCharacterEncoding("utf-16");
Cookie[] cookies = req.getCookies();//这里返回数组,说明Cookie可能存在多个
PrintWriter out = resp.getWriter();
//判断Cookie是否存在
if(cookies!=null) {
//如果存在怎么办
out.write("你上一次访问的时间是:");
for (int i = 0; i <cookies.length; i++) {
Cookie cookie = cookies[i];
//获取cookie的名字
if (cookie.getName().equals("name")) {
//获取cookie中的值
//System.out.println(cookie.getValue());
out.write(cookie.getValue());
}
}
}else {
out.write("这是您第一次访问本站");
}
//服务器给客户端响应一个cookie
Cookie cookie = new Cookie("name", URLEncoder.encode("张三","utf-8"));
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
编码解码:
URLEncoder.encode("z","utf-8")
URLDecoder.decode(cookie.getValue(),"UTF-8")
Session(重点)
服务器会给每个用户(浏览器)创建Session对象,一个Session独占一个浏览器,用户登录之后,整个网站都可以访问
新建SessionDemo01:
public class SessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码问题
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
//得到session
HttpSession session = req.getSession();
//给Session中存东西
session.setAttribute("name","张三");
//获取Session的ID
String id = session.getId();
//判断Session是不是新创建
if(session.isNew()){
resp.getWriter().write("session创建成功,ID:"+id);
}else {
resp.getWriter().write("session已经在服务器中存在了,id:"+id);
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
运行测试:
//Session创建的时候做了什么事
Cookie cookie = new Cookie("JSESSIONID",id);
resp.addCookie(cookie);
新建Person对象:
public class Person{
private String name;
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString(){
return "Person(){"+
"name='"+name+'\''+
",age="+age+
'}';
}
}
新建session时,往session中存入person对象
//给Session中存东西
session.setAttribute("name",new Person("张三",14));
新建SessionDemo02,取出存于session的对象
public class SessionDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码问题
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
//得到session
HttpSession session = req.getSession();
Person person = (Person) session.getAttribute("name");
System.out.println(person.toString());
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
在web.xml中,可手动注销session
<!--设置Session默认的失效时间-->
<session-config>
<!-- 1分钟后Session自动失效-->
<session-timeout>1</session-timeout>
</session-config>
Session与Cookie的区别:
- Cookie是把用户的数据写给用户的浏览器,浏览器保存(可保存多个)
- Session是吧用户的数据写到用户独占的Session中,服务器端保存(保存重要的信息,减少服务器资源的浪费)
- Session对象由服务器创建
Session的使用场景:
- 保存一个登录用户的信息
- 购物车信息
- 在整个网站中,经常会使用的数据,我们将它保存在Session中
JSP
原理
什么是JSP
Java Server Pages:Java服务器端页面,也和Servlet一样,用于动态web技术
最大特点,写JSP就像写HTML
区别:HTML只给用户提供静态的数据;JSP页面中可以嵌入Java代码,为用户提供动态数据
JSP运行原理:
1)客户端通过浏览器向服务器发出请求,在该请求中包含了请求的资源的路径,这样当服务器接收到该请求后就可以知道被请求的内容。
2)服务器根据接收到的客户端的请求来加载相应的JSP文件。
3)Web服务器中的JSP引擎会将被加载的JSP文件转化为Servlet。
4)JSP引擎将生成的Servlet代码编译成Class文件。
5)服务器执行这个Class文件。
6)最后服务器将执行结果发送给浏览器进行显示。
在电脑地址:C:\Users\Pluto\AppData\Local\JetBrains\IntelliJIdea2021.2\tomcat\2e39fcec-632e-4be9-89e8-2d15549e2047\work\Catalina\localhost\javaweb_session_cookie_war\org\apache\jsp
页面转变成了Java程序
浏览器向服务器发送请求,不管访问什么资源,其实都是在访问Servlet
添加Jsper依赖,查看HttpJspBase
<dependency>
<groupId>tomcat</groupId>
<artifactId>jasper-runtime</artifactId>
<version>5.5.23</version>
</dependency>
进入源码,可以看到JSP本质上就是一个Servlet
//初始化
public void _jspInit() {
}
//销毁
public void _jspDestroy() {
}
//JSPService
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
1、判断请求
2、内置一些对象
final javax.servlet.jsp.PageContext pageContext; //页面上下文
javax.servlet.http.HttpSession session = null; //session
final javax.servlet.ServletContext application; //applicationContext
final javax.servlet.ServletConfig config; //config
javax.servlet.jsp.JspWriter out = null; //out
final java.lang.Object page = this; //page:当前
HttpServletRequest request //请求
HttpServletResponse response //响应
3、输出页面前增加的代码
response.setContentType("text/html"); //设置文本响应的页面类型
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
4、以上的这些个对象我们可以在JSP页面中直接使用!
在JSP页面中;
只要是 JAVA代码就会原封不动的输出;如果是HTML代码,就会被转换为:out.write("<html>\r\n");
这样的格式,输出到前端
基础语法
新建Maven项目,javaweb-03-jsp
导入依赖
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
任何语言都有自己的语法,JSP作为Java技术的一种应用,支持Java所有的语法,另外拥有一些自己扩充的语法(了解即可)。
<%--JSP表达式
作用:用来将程序的结果输出到客户端--%>
<%= new java.util.Date()%>
<%--jsp脚本片段--%>
<%
int sum = 0;
for (int i = 0; i < 100; i++) {
sum+=i;
}
out.println("<h1>Sum="+sum+"</h1>");
%>
脚本片段的再实现:
<%--在代码中嵌入HTML元素--%>
<%
for (int i = 0; i < 5; i++) {
%>
<h1>Hello World</h1>
<%
}
%>
JSP声明会被编译到JSp生成的java类中,其它的,就会被生成到_jspService方法中。
自定义错误页面
jsp01.jsp
<%--定制错误页面--%>
<%@ page errorPage="error/500.jsp" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
int x=1/0;
%>
</body>
</html>
自定义500.jsp
<html>
<head>
<title>Title</title>
</head>
<body>
<img src="./img/500.jpg" alt="501">
</body>
</html>
测试:
自定义404错误页面
在web.xml中:
<error-page>
<error-code>404</error-code>
<location>/error/404.jsp</location>
</error-page>
404.jsp
<html>
<head>
<title>Title</title>
</head>
<body>
<img src="./img/404.jpg" alt="404">
</body>
</html>
JSP指令
footer:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是footer</h1>
header:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是header</h1
jsp02:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--@include会将两个页面合二为一--%>
<%@include file="common/header.jsp"%>
<h1>网页主体</h1>
<%@include file="common/footer.jsp"%>
<hr>
<%--jsp标签
jsp:include:拼接页面,本质还是三个--%>
<jsp:include page="/common/header.jsp"/>
<h1>网页主体</h1>
<jsp:include page="/common/footer.jsp"/>
</body>
</html>
9大内置对象
- PageContest
- Request
- Response
- Session
- Application【SerlvetContext】
- config【SerlvetConfig】
- page
- exception
作用域:
pageContext.setAttribute("name1","001"); //保存的数据只在一个页面中有效
request.setAttribute("name2","002"); //保存的数据只在一次请求中有效,请求转发会携带这个数据
session.setAttribute("name3","003"); //保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
application.setAttribute("name4","004"); //保存的数据只在服务器有效,从打开服务器到关闭服务器
//从pageContext取出,我们通过寻找的方式来
//从底层到高层(作用域):page->request->session->application 双亲委派机制
String name1 = (String) pageContext.findAttribute("name1");
String name2 = (String) pageContext.findAttribute("name2");
String name3 = (String) pageContext.findAttribute("name3");
String name4 = (String) pageContext.findAttribute("name4");
JSP标签、JSTL标签、EL表达式
<!-- JSTL表达式的依赖-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!-- standrd标签-->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
EL表达式: ${ }
- 获取数据
- 执行运算
- 获取web开发的常用对象
JSP标签:
<%--jsp:include--%>
<%--http://localhost:8080/javaweb_03_jsp_war_exploded/jsptag.jsp?name=wang&age=12--%>
<jsp:forward page="/jsptag2.jsp">
<jsp:param name="name" value="wang"/>
<jsp:param name="age" value="23"/>
</jsp:forward>
JSTL标签:
JSTL标签库的使用就是为了弥补HTML'标签的不足,它自定义了许多标签供我们使用,标签的功能和Java代码一样
使用步骤:1.引入对应的taglib;2.使用其中的方法
- 核心标签(掌握部分)
- 格式化标签
- SQL标签
- XML标签
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--引入JSTL核心标签库,我们才能使用JSTL标签--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h4>if测试</h4>
<hr>
<form action="coreif.jsp" method="get">
<%-- EL表达式获取表单中的数据 ${param.参数名} --%>
<input type="text" name="username" value="${param.username}">
<input type="submit" value="登录">
</form>
<%--判断如果提交的用户名是管理员,则登录成功--%>
<c:if test="${param.username=='admin'}" var="isAdmin">
<c:out value="登录成功"/>
</c:if>
<c:out value="${isAdmin}"/>
</body>
</html>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
ArrayList<String> people = new ArrayList<>();
people.add(0,"张三");
people.add(1,"李四");
people.add(2,"王五");
people.add(3,"赵六");
people.add(4,"田七");
request.setAttribute("list",people);
%>
<%--var 每一次遍历出来的变量 items 要遍历的对象--%>
<c:forEach var="people" items="${list}">
<c:out value="${people}"/>
<br>
</c:forEach>
<hr>
<%--begin 开始 end 结束 step 步长--%>
<c:forEach var="people" items="${list}" begin="1" end="3" step="2">
<c:out value="${people}"/><br>
</c:forEach>
</body>
</html>
JavaBean
实体类
JavaBean有特定的写法:
- 必须要有一个无参构造
- 属性必须私有化
- 必须有对应的get/set方法
一般用来和数据库的字段做映射 ORM
ORM:对象关系映射
- 表-->类
- 字段-->属性
- 行记录-->对象
id | name | age | address |
---|---|---|---|
1 | 张三 | 23 | 重庆 |
2 | 李四 | 25 | 西安 |
3 | 王五 | 26 | 工作 |
package com.wang.pojo;
//实体类 我们一般都是和数据库中的表结构一一对应
public class People {
private int id;
private String name;
private int age;
private String address;
public People(){
}
public People(int id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
<%@page import="com.wang.pojo.People" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
// People people = new People();
// people.setId();
// people.setName();
// people.setAge();
// people.setAddress();
%>
<jsp:useBean id="people" class="com.wang.pojo.People" scope="page"/>
<jsp:setProperty name="people" property="address" value="西安"/>
<jsp:setProperty name="people" property="age" value="26"/>
<jsp:setProperty name="people" property="name" value="王五"/>
<jsp:setProperty name="people" property="id" value="3"/>
<%--<%=people.getAddress()%>--%>
姓名:<jsp:getProperty name="people" property="name"/>
年龄:<jsp:getProperty name="people" property="age"/>
地址:<jsp:getProperty name="people" property="address"/>
ID:<jsp:getProperty name="people" property="id"/>
</body>
</html>
MVC三层架构
什么是MVC:Model(模型)、View(视图)、Controller(控制器)
控制器(Servlet):1.接收用户的请求(req:请求参数、Session信息) 2.交给业务层处理对应的代码(返回数据) 3.控制视图跳转
视图层(Jsp):1.展示数据模型 2.提供用户操作、提供链接发起Servlet请求(a,form,img)
模型(Model):控制业务操作、保存数据、修改数据、删除数据、查询数据
- 业务处理:业务逻辑(Service)
- 数据持久层:CRUD(Dao)
约定:为了易于维护和使用,Servlet专注于处理请求以及控制视图跳转,Jsp专注于显示数据
Filter
Filter(过滤器)
- 处理中文乱码
- 登录验证...
Filter开发步骤:
1.导包import javax.servlet.*;
2.编写过滤器
新建maven项目,添加依赖
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wang</groupId>
<artifactId>javaweb-filter</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- Servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!-- JSP依赖-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<!-- JSTL表达式的依赖-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!-- standard标签库-->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- 连接数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
</dependencies>
</project>
showServlet.java:
package com.wang.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class showServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// resp.setContentType("text/html");
// resp.setCharacterEncoding("utf-8");
resp.getWriter().write("你好,世界!");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
实现Filter接口,重写对应的方法,CharacterEncodingFilter.java:
import java.io.IOException;
public class CharacterEncodingFilter implements Filter {
// 初始化:web服务器启动,就初始化了,随时等待过滤对象出现
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("CharacterEncodingFilter初始化");
}
@Override
//Chain:链
/**
* 1.过滤中的所有代码,在过滤特定请求的时候都会执行
* 2.必须要让过滤器继续通行 filterChain.doFilter(servletRequest,servletResponse);
*/
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=UTF-8");
System.out.println("CharacterEncodingFilter执行前...");
filterChain.doFilter(servletRequest,servletResponse); //让请求继续走,如果不写,程序到这里被拦截停止了
System.out.println("CharacterEncodingFilter执行后...");
}
// 销毁:web服务器关闭的时候,过滤器销毁
@Override
public void destroy() {
System.out.println("CharacterEncodingFilter销毁");
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
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"
version="4.0">
<servlet>
<servlet-name>showServlet</servlet-name>
<servlet-class>com.wang.servlet.showServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>showServlet</servlet-name>
<url-pattern>/servlet/show</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>showServlet</servlet-name>
<url-pattern>/show</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.wang.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<!-- 只要是/servlet的任何请求,会经过这个这个过滤器-->
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
</web-app>
执行程序:
监听器
实现一个监听器的接口
//在线网站在线人数:统计session
public class OnlineCountListener implements HttpSessionListener {
//创建session监听
@Override
public void sessionCreated(HttpSessionEvent se) {
ServletContext ctx = se.getSession().getServletContext();
Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
if(onlineCount==null){
onlineCount = new Integer(1);
}else {
int count = onlineCount.intValue();
onlineCount=new Integer(count+1);
}
ctx.setAttribute("OnlineCount",onlineCount);
}
//销毁session监听
@Override
public void sessionDestroyed(HttpSessionEvent se) {
ServletContext ctx = se.getSession().getServletContext();
Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
if(onlineCount==null){
onlineCount = new Integer(0);
}else {
int count = onlineCount.intValue();
onlineCount=new Integer(count-1);
}
ctx.setAttribute("OnlineCount",onlineCount);
}
}
index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h1>当前有<span><%=this.getServletConfig().getServletContext().getAttribute("OnlineCount")%></span>人在线</h1>
</body>
</html>
web.xml:
<!-- 注册监听器-->
<listener>
<listener-class>com.wang.listener.OnlineCountListener</listener-class>
</listener>
Filter实现权限拦截
用户登录之后才能进入主页!用户注销后就不能进入主页了
Login.jsp登录页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>登录</h1>
<form action="/servlet/login" method="post">
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>
登录判断LoginServlet:
package com.wang.servlet;
import com.wang.util.Constant;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取前端请求的参数
String username = req.getParameter("username");
if(username.equals("admin")){//登录成功
req.getSession().setAttribute(Constant.USER_SESSION,req.getSession().getId());
resp.sendRedirect("/sys/success.jsp");
}else {//登录失败
resp.sendRedirect("/error.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
登录成功跳转success.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>主页</h1>
<p><a href="/servlet/logout">注销</a> </p>
</body>
</html>
登录失败跳转error.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>错误</h1>
<h3>没有权限,用户名错误</h3>
<a href="/Login.jsp">返回登录页面</a>
</body>
</html>
注销功能LogoutServlet:
package com.wang.servlet;
import com.wang.util.Constant;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class LogoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Object user_session = req.getSession().getAttribute(Constant.USER_SESSION);
if (user_session!=null){
req.getSession().removeAttribute(Constant.USER_SESSION);
resp.sendRedirect("/Login.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
注销后进行权限拦截,无法直接登录SysFilter:
package com.wang.listener;
import com.wang.util.Constant;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class SysFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
if ( request.getSession().getAttribute(Constant.USER_SESSION)==null){
response.sendRedirect("/error.jsp");
}
chain.doFilter(request,response);
}
@Override
public void destroy() {
}
}
Constant常量池:
package com.wang.util;
public class Constant {
public final static String USER_SESSION = "USER_SESSION";
}
web.xml页面注册:
<!-- Filter实现权限拦截-->
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.wang.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.wang.servlet.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/servlet/logout</url-pattern>
</servlet-mapping>
<filter>
<filter-name>SysFilter</filter-name>
<filter-class>com.wang.listener.SysFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SysFilter</filter-name>
<url-pattern>/sys/*</url-pattern>
</filter-mapping>
运行后,登录界面:
success界面:
注销后无法无法直接登录:
完成!