javaweb-02


Cookie

Cookie是某些网站为了辨别用户身份,进行Session跟踪而储存在用户本地终端上的数据(通常经过加密),由用户客户端计算机暂时或永久保存的信息

新建Maven项目

image-20211025143651407

什么是session

在Jsp中,session是使用bean的一个生存期限,一般为page,session意思是在这个用户没有离开网站之前一直有效,如果无法判断用户何时离开,一般依据系统设定,tomcat中设定为30分钟

当用户打开浏览器,访问某个网站的时候,服务器就会在服务器的内存为该浏览器分配一个内存空间,该空间被这个浏览器独占,这个空间就是session空间。

该空间中的数据默认存在时间为30min,在tomcat的web.xml中的可以修改

session可以用来做什么?

1)可以用作网上商城的购物车

2)保存登录用户的信息

3)将某些数据放在session中,供同一用户的各个页面使用(共享数据)

4)防止用户非法登录到某个页面

客户端 服务端

  1. 服务端给客户端一个 信件,客户端下次访问服务端带上信件就可以了; cookie
  2. 服务器登记你来过了,下次你来的时候我来匹配你; 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 

测试运行:

image-20211025154918317

  • 一个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 

image-20211025163420692

编码解码:

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);
    }
}

运行测试:

image-20211026084443642

//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);
    }
}

image-20211026091132749

在web.xml中,可手动注销session


    

        1
    

Session与Cookie的区别:

  • Cookie是把用户的数据写给用户的浏览器,浏览器保存(可保存多个)
  • Session是吧用户的数据写到用户独占的Session中,服务器端保存(保存重要的信息,减少服务器资源的浪费)
  • Session对象由服务器创建

Session的使用场景:

  • 保存一个登录用户的信息
  • 购物车信息
  • 在整个网站中,经常会使用的数据,我们将它保存在Session中

image-20211026095430181

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程序

image-20211026105606308

浏览器向服务器发送请求,不管访问什么资源,其实都是在访问Servlet

image-20211026121121505

添加Jsper依赖,查看HttpJspBase

	
      tomcat
      jasper-runtime
      5.5.23
    

进入源码,可以看到JSP本质上就是一个Servlet

image-20211026110406187

//初始化
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("\r\n");这样的格式,输出到前端

基础语法

新建Maven项目,javaweb-03-jsp

导入依赖


    
 	   javax.servlet
 	   javax.servlet-api
       4.0.1
    
    
 	   javax.servlet.jsp
	    javax.servlet.jsp-api
 	   2.3.3
    
    
	    javax.servlet.jsp.jstl
	    jstl-api
 	   1.2
    
    
 	    taglibs
	    standard
	    1.1.2
    

任何语言都有自己的语法,JSP作为Java技术的一种应用,支持Java所有的语法,另外拥有一些自己扩充的语法(了解即可)。

<%--JSP表达式
作用:用来将程序的结果输出到客户端--%>
<%= new java.util.Date()%>

image-20211026135656263

<%--jsp脚本片段--%>
<%
    int sum = 0;
    for (int i = 0; i < 100; i++) {
        sum+=i;
    }
    out.println("

Sum="+sum+"

"); %>

image-20211026135606920

脚本片段的再实现:

<%--在代码中嵌入HTML元素--%>
<%
    for (int i = 0; i < 5; i++) {
%>

Hello World

<% } %>

image-20211026141114783

JSP声明会被编译到JSp生成的java类中,其它的,就会被生成到_jspService方法中。

自定义错误页面

jsp01.jsp

<%--定制错误页面--%>
<%@ page errorPage="error/500.jsp" %>



    Title



<%
int x=1/0;
%>



image-20211026161907968

自定义500.jsp



    Title



501



测试:

image-20211026161703780

自定义404错误页面

在web.xml中:

    
        404
        /error/404.jsp
    

404.jsp



    Title



404



image-20211026162812406

JSP指令

footer:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

我是footer

header:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

我是header

jsp02:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title



<%--@include会将两个页面合二为一--%>
<%@include file="common/header.jsp"%>

网页主体

<%@include file="common/footer.jsp"%>
<%--jsp标签 jsp:include:拼接页面,本质还是三个--%>

网页主体

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表达式


    
      javax.servlet.jsp.jstl
      jstl-api
      1.2
    

    
      taglibs
      standard
      1.1.2
    

EL表达式: ${ }

  • 获取数据
  • 执行运算
  • 获取web开发的常用对象

JSP标签:

<%--jsp:include--%>

<%--http://localhost:8080/javaweb_03_jsp_war_exploded/jsptag.jsp?name=wang&age=12--%>


    
    

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" %>


    Title



if测试


<%-- EL表达式获取表单中的数据 ${param.参数名} --%>
<%--判断如果提交的用户名是管理员,则登录成功--%>

image-20211122151915711

<%@ 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" %>


    Title



<%
    ArrayList people = new ArrayList<>();
    people.add(0,"张三");
    people.add(1,"李四");
    people.add(2,"王五");
    people.add(3,"赵六");
    people.add(4,"田七");
    request.setAttribute("list",people);
%>

<%--var 每一次遍历出来的变量  items 要遍历的对象--%>

    
    

<%--begin 开始 end 结束 step 步长--%>

image-20211122154633509

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" %>


    Title



<%
//    People people = new People();
//    people.setId();
//    people.setName();
//    people.setAge();
//    people.setAddress();

%>








<%--<%=people.getAddress()%>--%>

姓名:
年龄:
地址:
ID:



image-20211123135233621