• 73.【JavaWeb-01】


    (一)、WEB基本概念

    1.JavaWeb基本概念

    1.web开发:
    (1).web 网页的意思。
    (2).静态web:  html,css,提供给所有人看的数据始终不会发生变化
    (3).动态web:  淘宝,京东,提供给所有人看的数据始终会发生变化,根据时间和地点变化
    2.javaweb定义:
    (1).在java中,动态web资源开发的技术统称为JavaWeb技术
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.Web应用程序

    1.web应用程序: 可以提供浏览器访问的程序。
    (1).html..多个web资源,这些web资源可以被外界访问,对外界提供服务
    (2).我们能访问到的任何一个页面和资源,都存在与某个角落的一台计算机上。
    (3).URL:
    (4).这些统一的外部资源会被放在同一个文件夹里---》依赖于(Tomcate)
    (5).一个web应用有多部分组成(静态web,动态web)
    html css JavaScript
    jsp servlet
    java程序
    jar包
    配置文件(properties)
    (6).web应用程序编写完毕之后,若想提供给外界访问需要一个服务器统一管理;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    1.百度来自于一个IP地址叫做XXX的计算机上
    在这里插入图片描述

    3.静态Web

    1. *.html,*htm,这些都是网页的后缀,如果服务器上一直存在这些东西,我们
    就可以直接读取,(需要通网)
    
    • 1
    • 2

    这里是引用
    在这里插入图片描述

    2.静态web存在的缺点是:
    (1).Web页面无法动态更新,所有用户看到的都是同一个页面。
      .轮播图,点击特效:伪动态.
       JavaScript (实际开发中,他用的最多)
    (2).他无法和数据库交互(数据无法持久,用户无法交互)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4.动态Web

    1.页面动态服务:
    (1).JSP/Serciet
    2.缺点:
    (1).假如服务器的动态资源出现了错误,我们需要重新编写我们的后台
    3.优点:
    (1).百人百面
    (2).可以与数据库进行交互
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    这里是引用

    (二)、 WEB服务器讲解

    5.web服务器

    1.实现服务器的手段(语言)
    (1).ASP  : 微软(国内最早流行的) 在HTML中嵌入了VB的脚本代码,ADP+COM,
    基本上一个页面都有几千行的业务代码,页面及其混乱。维护成本极高
    (2).PHP: PHP开发速度快,功能很强,跨平台,代码很简单。无法承载大访问量的
    情况,
    (3).JSP/JAVA SERIVE PAGE: SUN公司主推的B/S架构,基于Java语言,可以承载
    (高并发,高性能,高可用)带来的影响。
    (4).两种模式
    B/S模式: brower/Service 浏览器/服务器
    C/S模式: Customer/Service 顾客/服务器
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    2.实现服务器的工具
    (1).Tomcat : 
    TomcatApache 软件基金会(Apache Software Foundation)的Jakarta 项目中
    的一个核心项目,由ApacheSun 和其他一些公司及个人共同开发而成。由于有了
    Sun 的参与和支持,最新的Servlet 和JSP 规范总是能在Tomcat 中得到体现,
    Tomcat 5支持最新的Servlet 2.4 和JSP 2.0 规范。因为Tomcat 技术先进、性能
    稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,
    成为比较流行的Web 应用服务器。
    (2).LIS: 微软的
    3.什么是服务器?
    (1).服务器是一种被动的操作,用来处理用户的一些请求和给用户一些响应的操作
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    (三)、Tomcat启动和配置

    6.Tomcat文件夹的作用

    1.文件夹的作用:
    在这里插入图片描述

    7.Tomcat开启/关闭

    http://127.0.0.1:80
    
    • 1

    在这里插入图片描述

    1.Tomacat端口号的查询:这里是引用
    2.验证是否开启Tomcat:
    在这里插入图片描述

    8.Tomcat配置

    1.配置端口号
    2.配置主机的名字
    3.网站放置的位置是: webapps
    
    • 1
    • 2
    • 3

    1.核心配置文件:
    在这里插入图片描述

    常见面试

    4.网站是如何进行访问的?
    (1).输入一个域名,回车
    (2).检查本机C:\Windows\System32\drivers\etc配置文件下是否有没有这个域名的映射
    假如说有: 直接返回对应的IP地址:
    假如说没有: 就去DNS找,找不到就返回无连接.
    
    • 1
    • 2
    • 3
    • 4
    • 5

    域名映射这里是引用
    2.假如说没有就去DNS找
    在这里插入图片描述

    9.发布一个Web网站

    各个资源应该u存放的位置。
    
    • 1

    这里是引用

    1.复制一个ROOT文件,复制至wkbapps文件夹中
    2.删除相应的图片和其他文件
    3.新建文本文档: 结尾以html结尾
    4.使用idea打开进行操作
    5.如需CSS文件/JAVA文件/音频放入到包中即可
    6.开启服务器运行即可
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    1~2这里是引用
    3-4
    在这里插入图片描述
    5
    在这里插入图片描述
    6.在这里插入图片描述

    (四)、Http讲解

    10.什么是HTTP?

    1.什么是HTTP?
    
    • 1

    超文本传输协议(Hyper Text Transfer Protocol,HTTP)是一个简单的请求-响应协议,它通常运行在TCP之上。它指定了客户端可能发送给服务器什么样的消息以及得到什么样的响应。请求和响应消息的头以ASCII形式给出;而 消息内容则具有一个类似MIME的格式。这个简单模型是早期Web成功的有功之臣,因为它使开发和部署非常地直截了当。

    2.文本与超文本
    文本: html,字符串....
    超文本: 图片,视频,音乐,定位,地图....
    3.http端口: 80 https端口: 443;
    
    • 1
    • 2
    • 3
    • 4

    11.HTTP的两个时代

    1.http1.0
    (1).HTTP/1.0: 客户端可以与web服务器连接后,只能获得一个web资源(想要访问其他的,需要再次进行申请)
    2.http2.0
    (1).HTTP/1.1: 客户端可以与web服务器连接后,可以获得多个web资源(想要访问其他的,不需要再次进行申请)
    
    • 1
    • 2
    • 3
    • 4

    12.Http请求

    1.HTTP请求:
    (1)."Accept: application/json, text/javascript, */*; q=0.01" 《》
    (2).Accept-Encoding: gzip, deflate, br  《接受编码》
    (3).Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6 《语言》
    (4).Connection: keep-alive 《存活方式》
    
    • 1
    • 2
    • 3
    • 4
    • 5
    2.请求行:
    (1).请求行中的请求方式: GET/POST
    请求 URL: https://www.baidu.com/sugrec?prod=pc_his&from=pc_web&json=1&sid=&hisdata=%5B%7B%22time%22%3A1666835536%2C%22kw%22%3A%22ideal%22%7D%2C%7B%22time%22%3A1667643329%2C%22kw%22%3A%22tomcat%22%7D%5D&_t=1667695534824&req=2&sc=eb&csor=0
    请求方法: GET
    状态代码: 200 OK
    远程地址: 39.156.66.18:443
    引用者策略: unsafe-url
    3.GET/POST的请求方式区别:
    (1).请求能够携带的参数比较少,大小有限制,会在浏览器的URL地址栏显示数据
    内容,不安全,但是高效。
    (2)..请求能够携带的参数多,大小无限制,不会在浏览器的URL地址栏显示数据
    内容,安全,但是不高效。 
    4.消息头:
    (1)."Accept: 告诉浏览器,他所支持的数据类型
    (2).Accept-Encoding:  支持那种编码格式
    (3).Accept-Language:  支持哪种语言
    (4).Connection: 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    13.Http响应

    1.HTTP响应:
    (1).HTTP/1.1 200 OK    《协议》
    (2).Content-Length: 187
    (3).Content-Type: text/plain; charset=UTF-8  《响应编码类型》
    (4).Date: Sun, 06 Nov 2022 00:45:34 GMT    《响应时间》
    2.响应体:
    (1)."Accept: 告诉浏览器,他所支持的数据类型
    (2).Accept-Encoding:  支持那种编码格式
    (3).Accept-Language:  支持哪种语言
    (4).Connection:  请求完成之后是断开还是链接?
    (5).Refresh: 告诉客户端多久刷新一下
    (6).Location: 让网页重新定位.
    3.响应状态码:
    (1).200 请求成功
    (2).3** 请求重定向,你重新到我指定的新位置。
    (3).404 找不到资源
    (4).5**: 服务器代码错误
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    14.常见面试题:

    1.当你的浏览器中的地址栏输入地址并回车的一瞬间到页面能够展示回来,经历了什么?
    
    • 1

    (五)、Maven

    1.为什么要使用Maven?
    (1).因为正常的javaWeb开发,需要很多的jar包。
    (2).Maven可以帮助我们自动导入jar包
    
    • 1
    • 2
    • 3

    15.Maven项目架构管理工具

    1.Maven的核心思想: 约定大于配置(有约束,不要去违反)
    (1).下载zip
    (2).配置环境 MAVEN_HOME : 文件所在的目录 M3_HOME: bin目录
    
    • 1
    • 2
    • 3

    16.阿里云镜像

    1.镜像: mirrors
    (1).作用:加速我们的下载
    (2).国内建议使用阿里云
    
    • 1
    • 2
    • 3

    2.在maven里面配置阿里云镜像

      <mirror>
        <id>nexus-aliyun</id>
          <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </mirror>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    1.配置镜像在这里插入图片描述
    2.复制镜像
    在这里插入图片描述
    3.粘贴到
    在这里插入图片描述

    17.本地仓库

    1.建立一个本地仓库: 
    
    • 1
    E:\Maven\apache-maven-3.8.6\maven-repo
    
    • 1

    1.建立一个本地仓库这里是引用

    (六)、在IDEA中使用MAVEN

    18.基本步骤

    1.启动IDEA
    2.创建一个MAVEN项目
    3.出现最后一个,即可
    
    • 1
    • 2
    • 3

    这里是引用
    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    19.IDEA中的MAVEN设置

    注意工作空间和配置空间
    
    • 1

    这里是引用

    20.创建一个普通的Maven项目

    1.创建项目这里是引用
    2.文件详细讲解在这里插入图片描述
    3.为文件添加属性
    在这里插入图片描述

    21.在IDEA中配置TOMCAT

    1.导入Tomcat这里是引用
    2.Tomcat界面的信息
    在这里插入图片描述
    3.创建工作空间
    在这里插入图片描述
    在这里插入图片描述
    7.创建成功
    在这里插入图片描述
    8.链接成功
    在这里插入图片描述
    8.详解Tomcat在这里插入图片描述
    9.MOVEN的信息在这里插入图片描述

    22.Moven的POM文件

    1.pom.xml是Maven的核心配置文件
    
    • 1
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!--Maven版本和头文件-->
    
    <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>
      
    <!--我们刚才配置的GAV-->
    
      <groupId>com.Jsxs</groupId>
      <artifactId>javaweb-maven01</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>war</packaging>
    <!--Package :项目的打包方式
        jar: java应用
        war: javaweb应用
    -->
      <name>javaweb-maven01 Maven Webapp</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
      
    <!--配置-->
    
      <properties>
      
    <!--项目默认的构建编码    -->
    
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        
    <!--    编码版本-->
    
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
      </properties>
      
    <!--项目依赖-->
    
      <dependencies>
      
    <!--    具体依赖的jar包-->
    
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
      
    <!--项目构建要用的东西-->
    
      <build>
        <finalName>javaweb-maven01</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
          <plugins>
            <plugin>
              <artifactId>maven-clean-plugin</artifactId>
              <version>3.1.0</version>
            </plugin>
            <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
            <plugin>
              <artifactId>maven-resources-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.0</version>
            </plugin>
            <plugin>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.22.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-war-plugin</artifactId>
              <version>3.2.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-install-plugin</artifactId>
              <version>2.5.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-deploy-plugin</artifactId>
              <version>2.8.2</version>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90

    在这里插入图片描述

    Maven由于他的约定大于配置,我们之后可能遇到我们写的配置文件,无法导出或则生效的问题,解决方案:

    !--在build中配置resources-->
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <exclude>**/*.properties
                        **/*.xml</include>
                    </includes>
                    <filtering>true</filtering>
                </resource>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.properties
                        **/*.xml</include>
                    </includes>
                    <filtering>true</filtering>
                </resource>
            </resources>
    </build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    23.Moven空项目测试添加依赖

    1.添加前:
    在这里插入图片描述

    <dependencies>
    <!--    具体依赖的jar包-->
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.添加后:
    在这里插入图片描述
    3.仓库用法:

    1.找资源在这里插入图片描述> 复制在这里插入图片描述
    导入成功在这里插入图片描述

    24.替换webapp4.0版本与tomcat一致

    <?xml version="1.0" encoding="UTF-8"?>
            <web-app metadata-complete="true" version="4.0" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee">
    </web-app>
    
    • 1
    • 2
    • 3

    这里是引用
    在这里插入图片描述

    25.书写helloservlet

    1.查看案列在这里插入图片描述

    2.查看TOMCAT的jar包在这里插入图片描述
    3.配置仓库
    在这里插入图片描述
    4.配置web
    在这里插入图片描述
    5.进行映射查找
    在这里插入图片描述
    6.映射查找(一定要在webapp目录中,否则不算)
    在这里插入图片描述
    7.不是表单会默认GET方式
    在这里插入图片描述
    8.编码乱码在这里插入图片描述

    9.设置UTF-8在这里插入图片描述

    在这里插入图片描述

    (七)、第一个项目HelloServlet

    26.构建一个MAVEN项目

    1.什么是Servlet?
    
    • 1

    Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,具有独立于平台和协议的特性,主要功能在于交互式地浏览和生成数据,生成动态Web内容。
    狭义的Servlet是指Java语言实现的一个接口,广义的Servlet是指任何实现了这个Servlet接口的类,一般情况下,人们将Servlet理解为后者。Servlet运行于支持Java的应用服务器中。从原理上讲,Servlet可以响应任何类型的请求,但绝大多数情况下Servlet只用来扩展基于HTTP协议的Web服务器
    最早支持Servlet标准的是JavaSoft的Java Web Server,此后,一些其它的基于Java的Web服务器开始支持标准的Servlet。

    1.构建一个普通的MAVEN项目
    2.删除里面的Src目录,以及不必要的代码
    3.导入两个依赖.
    4.构建Module模块: 
    (1).关于Maven父子工程的理解:
    父项目中会有: 
        <modules>
            <module>Servlet-01</module>
        </modules>
    子项目中会有:(没有的话手动添加)
      <parent>
        <artifactId>父项目名字</artifactId>
        <groupId>com.Jsxs</groupId>
        <version>1.0-SNAPSHOT</version>
      </parent>
    5.重写Web项目: 更新最新的4.0
    6.在main包中创建目录: java resource
    7.在java中创建一个com.Jsxs.包,并在这个包中设一个类
    8.实现Serlvet接口。这里我们直接继承HttpServlet  
    9.在类中重写两个方法: doGet doPost
    10.在类中书写信息:
    11.在web.xml中编写映射: 我们写的是java程序,但是要通过浏览器访问,而浏览器
    需要连接web服务器,所以我们需要在web服务中注册我们写的Servlet,还需要能够
    访问的路径。
    12.配置Tomcat.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
            <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>javax.servlet.jsp-api</artifactId>
                <version>2.2.1</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
           <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.1</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    1.构建Model模块这里是引用
    在这里插入图片描述
    2.配置类方法:
    在这里插入图片描述
    3.配置web.xml
    在这里插入图片描述
    在这里插入图片描述

    package com.Jsxs.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class HelloServlet extends HttpServlet {
    //    由于get和post只是请求实现的不同方式,可以相互调用,业务逻辑都一样;
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("进入GET模式");
            PrintWriter writer = resp.getWriter();  //响应流
            writer.println("hello servlet!");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app metadata-complete="true" version="4.0" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee">
    <!--注册servlet-->
    <servlet>
        <servlet-name>hello</servlet-name>
    <!--    -->
        <servlet-class>com.Jsxs.servlet.HelloServlet</servlet-class>
    </servlet>
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>
    </web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    (八)、Servlet原理

    27.Servlet原理

    1.Servlet是由WEB服务器调用,WEB服务器在收到浏览器请求之后,
    
    • 1

    在这里插入图片描述

    28.Mapping映射

    1.一个Servlet可以指定一个映射路径
    
    2.一个Servlet可以指定多个映射路径
    
    3.一个Servlet可以指定通用映射路径 
    (1).  默认通道: /*                 */
    (2).通用映射路径:
     hello/*                        */
    4.指定一些后缀或则前缀等待.... 
    (1).   *.后缀 ; *前面不能加/
    5.优先级问题:
    (1).指定了固有的映射路径优先级最高,如果找不到就会走默认的处理请求.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    1.指定一个Servlet映射路径这里是引用> 2.指定多个Servlet映射路径
    在这里插入图片描述
    3.指定通用的Servlet
    在这里插入图片描述
    4.指定后缀的名字
    在这里插入图片描述
    5.指定的固有的映射路径最高,最先执行
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    (九)、ServletContext

    29.详解ServletContext的

    (运用多个servlet进行交互)
    1.web容器在启动的时候,他会为每一个web程序都创建一个对应的ServietContext
    对象,它代表了当前的web应用;
    (1).共享数据:我在A中保存的数据,可以在另一个Servlet中拿到;
    2.基本代码展示:
    (1). 设置ServletContext
    ServletContext servletContext = this.getServletContext();
    (2).设置键值对的操作:
    servletContext.setAttribute("username",userName);  //将一个数据保存在了ServletContext中,名字为: username,值为:userName
    =================================================存储值
    (3).获取存储在共享ServletContext
    因为我们知道他返回的是什么,然后程序不知道,所以我们要进行强制转化。
    String username = (String) servletContext.getAttribute("username");
    (4).输出信息的操作
    3.执行顺序:
    (1).必须先进行ServletContext进行存储,然后才能进行读取的操作。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1.共享效果:

    1.共享效果:这里是引用

    1.存储信息

    package com.Jsxs.Servlet;
    
    import javax.servlet.ServletContext;
    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 HelloServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    //        this.getInitParameter()   获取初始化参数
    //        this.getServletConfig()   Servlet配置
    //        this.getServletContext()  servlet上下文
            ServletContext servletContext = this.getServletContext();
            String userName="吉士先生";
            servletContext.setAttribute("username",userName);  //将一个数据保存在了ServletContext中,名字为: username,值为:userName
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    2.读取共享的信息

    package com.Jsxs.Servlet;
    
    import javax.servlet.ServletContext;
    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 GetServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext servletContext = this.getServletContext();
    //        因为我们知道他返回的是什么,然后程序不知道,所以我们要进行强制转化。
            String username = (String) servletContext.getAttribute("username");
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("text/html");
            resp.getWriter().print("名字是:"+username);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    1.执行顺序在这里插入图片描述在这里插入图片描述

    2.获取配置信息:
    2.获取配置信息:
    (1).在web添加配置信息:
     <context-param>
       <param-name>名字A</param-name>
       <param-value>配置的内容</param-value>
     </context-param>
     
    (2). 设置ServletContext
    ServletContext servletContext = this.getServletContext();
    
    (3).设置方法getInitParameter()
    String url = servletContext.getInitParameter("配置的名字");
    
    (4).配置web 映射以及servlet:
    <servlet></servlet>
    <servlet -mapping></servlet -mapping>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    1.设置ServletContext

    package com.Jsxs.Servlet;
    
    import javax.servlet.ServletContext;
    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 ServletDemo03 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext servletContext = this.getServletContext();
            String url = servletContext.getInitParameter("url");
            resp.getWriter().print(url);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    2.配置web

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app metadata-complete="true" version="4.0" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee">
     <context-param>
       <param-name>url</param-name>
       <param-value>jdbc:mysql://127.0.0.1:3306/demo1</param-value>
     </context-param>
    
      <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.Jsxs.Servlet.HelloServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
      </servlet-mapping>
    
      <servlet>
        <servlet-name>getHello</servlet-name>
        <servlet-class>com.Jsxs.Servlet.GetServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>getHello</servlet-name>
        <url-pattern>/getHello</url-pattern>
      </servlet-mapping>
      <servlet>
        <servlet-name>url</servlet-name>
        <servlet-class>com.Jsxs.Servlet.ServletDemo03</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>url</servlet-name>
        <url-pattern>/demo3</url-pattern>
      </servlet-mapping>
    
    </web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    这里是引用

    3.请求转发:
    3.请求转发:
    (1).设置请求转发:
    RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("转发到哪个地址?");
    (2).调用forward实现请求转发,
    requestDispatcher.forward(req,resp);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    请求转发:这里是引用
    重定义:在这里插入图片描述

    请求转发

    package com.Jsxs.Servlet;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletContext;
    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 ServletDemo4 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("进入到demo444444");
            ServletContext servletContext = this.getServletContext();
    //        转发到demo03这个项目里面
            RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/demo3");
    //        调用forward实现请求转发,
            requestDispatcher.forward(req,resp);
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    WEB

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app metadata-complete="true" version="4.0" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee">
     <context-param>
       <param-name>url</param-name>
       <param-value>jdbc:mysql://127.0.0.1:3306/demo1</param-value>
     </context-param>
    <!--第一个hello-->
      <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.Jsxs.Servlet.HelloServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
      </servlet-mapping>
    <!--得到名字-->
      <servlet>
        <servlet-name>getHello</servlet-name>
        <servlet-class>com.Jsxs.Servlet.GetServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>getHello</servlet-name>
        <url-pattern>/getHello</url-pattern>
      </servlet-mapping>
    <!--  获取参数-->
      <servlet>
        <servlet-name>url</servlet-name>
        <servlet-class>com.Jsxs.Servlet.ServletDemo03</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>url</servlet-name>
        <url-pattern>/demo3</url-pattern>
      </servlet-mapping>
    <!--  请求转发-->
      <servlet>
        <servlet-name>demo4</servlet-name>
        <servlet-class>com.Jsxs.Servlet.ServletDemo4</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>demo4</servlet-name>
        <url-pattern>/demo4</url-pattern>
      </servlet-mapping>
    </web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    输入这个转发到demo3这个目录这里是引用

    4.读取资源文件:
    4.读取资源文件:
    (1).在java目录下新建properties
    (2).在resource目录下新建properties
    都被打包到了同一个路径下:classes,我们俗称这个路径为classpath
    (3).切记我们赋值路径,复制的是out目录下的路径.
    
    • 1
    • 2
    • 3
    • 4
    • 5

    这里是引用

    2.在class文件夹下进行添加properties文件.资源导出可能会出现问题:
    在这里插入图片描述

    Maven由于他的约定大于配置,我们之后可能遇到我们写的配置文件,无法导出或则生效的问题,解决方案:

    !--在build中配置resources-->
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.properties
                        **/*.xml</include>
                    </includes>
                    <filtering>true</filtering>
                </resource>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.properties
                        **/*.xml</include>
                    </includes>
                    <filtering>true</filtering>
                </resource>
            </resources>
    </build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    合成成功在这里插入图片描述

    基本思路:
    (1).设置getServletContext()
    ServletContext servletContext = this.getServletContext();
    (2).导入文件的相对目录
    InputStream is = servletContext.getResourceAsStream("/WEB-INF/classes/Database.properties");
    (3).进行流的加载
    Properties properties = new Properties();
    properties.load(is);
    (4).通过键值对进行获取值
    String username = properties.getProperty("username");
    String password = properties.getProperty("password");
    (6).打印输出
    resp.getWriter().println(username+" "+password);
    (7).配置web
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    package com.Jsxs.Servlet;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class ServletDemo05 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext servletContext = this.getServletContext();
            InputStream is = servletContext.getResourceAsStream("/WEB-INF/classes/Database.properties");
            Properties properties = new Properties();
            properties.load(is);
            String username = properties.getProperty("username");
            String password = properties.getProperty("password");
            resp.getWriter().println(username+" "+password);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    这里是引用
    在这里插入图片描述

    (十)、HttpServletResponse下载文件

    30.简单分类:

    web服务器接受到客户端的http请求,针对这个请求,分别创建一个代表响应的
    HttpServletResponse对象,和一个代表请求HttpServletRequest的对象。
    (1).如果要获取客户端请求过来的参数: 找httpServletRequest
    (2).如果要给客户端相应一些信息:HttpServletResponse
    1.简单分类:
    负责向浏览器发送数据的方法:
    (1).
    Servletoutputstream getOutputStream() throw IOException;
    (2).
    Printwriter getwriter() throw IOException;
    
    负责向浏览器发送响应头的方法:
    void setContentLength(int var1);
    void setContentLengthLong(long var1);
    void setContentType(String var1);
    void setBufferSize(int var1);
    
    void setDateHeader(String var1, long var2);
    void addDateHeader(String var1, long var2);
    void setHeader(String var1, String var2);
    void addHeader(String var1, String var2);
    void setIntHeader(String var1, int var2);
    void addIntHeader(String var1, int var2);
    响应的状态码:
    int SC_CONTINUE = 100;
    int SC_SWITCHING_PROTOCOLS = 101;
    int SC_OK = 200;
    int SC_CREATED = 201;
    int SC_ACCEPTED = 202;
    int SC_NON_AUTHORITATIVE_INFORMATION = 203;
    int SC_NO_CONTENT = 204;
    int SC_RESET_CONTENT = 205;
    int SC_PARTIAL_CONTENT = 206;
    int SC_MULTIPLE_CHOICES = 300;
    int SC_MOVED_PERMANENTLY = 301;
    int SC_MOVED_TEMPORARILY = 302;
    int SC_FOUND = 302;
    int SC_SEE_OTHER = 303;
    int SC_NOT_MODIFIED = 304;
    int SC_USE_PROXY = 305;
    int SC_TEMPORARY_REDIRECT = 307;
    int SC_BAD_REQUEST = 400;
    int SC_UNAUTHORIZED = 401;
    int SC_PAYMENT_REQUIRED = 402;
    int SC_FORBIDDEN = 403;
    int SC_NOT_FOUND = 404;
    int SC_METHOD_NOT_ALLOWED = 405;
    int SC_NOT_ACCEPTABLE = 406;
    int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
    int SC_REQUEST_TIMEOUT = 408;
    int SC_CONFLICT = 409;
    int SC_GONE = 410;
    int SC_LENGTH_REQUIRED = 411;
    int SC_PRECONDITION_FAILED = 412;
    int SC_REQUEST_ENTITY_TOO_LARGE = 413;
    int SC_REQUEST_URI_TOO_LONG = 414;
    int SC_UNSUPPORTED_MEDIA_TYPE = 415;
    int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
    int SC_EXPECTATION_FAILED = 417;
    int SC_INTERNAL_SERVER_ERROR = 500;
    int SC_NOT_IMPLEMENTED = 501;
    int SC_BAD_GATEWAY = 502;
    int SC_SERVICE_UNAVAILABLE = 503;
    int SC_GATEWAY_TIMEOUT = 504;
    int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    1.下载文件
    resp.setHeader("content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"utf-8"));
    1.向浏览器输出消息
    2.下载文件:
    (1).要知道下载文件的路径
    (2).下载的文件名是啥?
    (3).设置想办法让浏览器支持下载我们需要的东西
    (4).获取下载文件的输入流
    (5).获取输出流
    (6).使用输出流将缓存区的数据输出到客户端.
    (7).配置web的操作
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    package com.Jsxs.Servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.URLEncoder;
    
    public class FileServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //        (1).要知道下载文件的路径
            String realPath = "E:\\Ideal源码\\FirstMaven\\Servelete03\\src\\main\\resources\\1.jpg";
    //        (2).下载的文件名是啥?
            String fileName = realPath.substring(realPath.lastIndexOf("\\") + 1);
    //        (3).设置想办法让浏览器支持下载我们需要的东西
            resp.setHeader("content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"utf-8"));
    //        (4).获取下载文件的输入流
            FileInputStream fileInputStream = new FileInputStream(realPath);
    //        (5).设置缓冲区
            int count=0;
            byte[] bytes = new byte[1024];
    //        (6).获取fillOutputStream输出流
            ServletOutputStream outputStream = resp.getOutputStream();
    //        (7).将fillOutputStream流写入到缓存区,使用输出流将缓存区的数据输出到客户端.
            while ((count=(fileInputStream.read(bytes)))!=-1){
                outputStream.write(bytes,0,count);
            }
            outputStream.close();
            fileInputStream.close();
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    在这里插入图片描述

    2.验证码
    2.验证码功能:
    (1).浏览器每秒刷新的操作:
    resp.setHeader("refresh","3");
    
    • 1
    • 2
    • 3
    package com.Jsxs.Servlet;
    
    import javax.imageio.ImageIO;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.Random;
    
    public class ImageServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //       让浏览器秒刷新一次
            resp.setHeader("refresh","3");
    //        在内存中国创建一个图片
            BufferedImage bufferedImage = new BufferedImage(80, 20, BufferedImage.TYPE_INT_BGR);
    //        得到图片
            Graphics2D graphics2D=(Graphics2D) bufferedImage.getGraphics();  //笔
    //        设置图片的背景颜色
            graphics2D.setColor(new Color(224, 222, 222));
            graphics2D.fillRect(0,0,80,20);
    //        给图片写入数据
            graphics2D.setColor(new Color(25, 132, 206));
            graphics2D.setFont(new Font("黑体",Font.BOLD,25));
            graphics2D.drawString(makeNum(),0,20);
    //        告诉浏览器,这个方式用图片的方式打开
            resp.setContentType("image/jpg");
    //        取消缓存
            resp.setDateHeader("expires",-1);
            resp.setHeader("Cache-Control","no-cache");
            resp.setHeader("pragma","no-cache");
    //        把图片写给浏览器
            ImageIO.write(bufferedImage,"jpg",resp.getOutputStream());
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
        private String makeNum(){
            Random random=new Random();
            String s = random.nextInt(9999999) + "";
            StringBuffer stringBuffer=new StringBuffer();
    //        假如不足七位数就进行0填充
            for (int i = 0; i <7-s.length() ; i++) {
                stringBuffer.append("0");
            }
            s=stringBuffer.toString()+s;
            return s;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    这里是引用

    3.实现重定向(状态码:302)=《路径/ /》
    1.重定向的定义:
    一个web资源收到客户端的请求后,他会通知客户端去访问另一个web资源,这个过程就叫做重定向。
    2.常见场景:  用户登入
    void sendRedirect(String var1) throws IOException; 
    
    • 1
    • 2
    • 3
    • 4

    这里是引用

    package com.Jsxs.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 RedirectServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
           resp.sendRedirect("/Servelete03_war/demo2");
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    向映射demo2进行跳转的操作在这里插入图片描述

    基本原理的操作:

    resp.setHeader("Location","/Servelete03_war/demo2");
    resp.setStatus(HttpServletResponse.SC_FOUND);  302
    代替上面的操作: resp.sendRedirect("/Servelete03_war/demo2");
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    在这里插入图片描述

    4.读取前端信息
    请你说说重定向和转发的区别:
    (1).相同点:
    页面都会跳转
    (2).不同点:
    请求转发的时候: url不会改变
    重定向的时候,url会改变
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    在jsp文件中,输入汉字的时候el表达式,需要加上:
    <%@page contentType="text/html; charset=UTF-8" %>
    为了防止产生乱码的现象。
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    转发:
    
    • 1

    Java

    package com.Jsxs.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 Demo5 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          resp.setCharacterEncoding("UTF-8");
          resp.setContentType("text/html");
           resp.getWriter().println("哈哈哈,我是demo5页面");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    JSP

    <html>
    <body>
    <h2>Hello World!</h2>
    <%@page contentType="text/html; charset=UTF-8" %>
    <form action="${pageContext.request.contextPath}/demo5" method="get">
        用户名: <input type="text" name="username" placeholder="请输入您的用户名">
        密码: <input type="password" name="password" placeholder="请输入您的密码">
        <input type="submit">
    </form>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    利用了表单转发在这里插入图片描述

    重定向:
    
    • 1
    package com.Jsxs.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 Demo5 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          resp.setCharacterEncoding("UTF-8");
          resp.setContentType("text/html");
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            System.out.println(username+" "+password);
            resp.sendRedirect("/Servelete03_war/succes.jsp");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    <html>
    <body>
    <h2>Hello World!</h2>
    <%@page contentType="text/html; charset=UTF-8" %>
    <form action="${pageContext.request.contextPath}/demo5" method="get">
        用户名: <input type="text" name="username" placeholder="请输入您的用户名">
        密码: <input type="password" name="password" placeholder="请输入您的密码">
        <input type="submit">
    </form>
    </body>
    </html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    (十一)、HttpServletRequest(获取客户端信息)

    HttpServletRequest代表客户端的请求,用户通过http协议访问服务器,Http请求
    中的所有信息会被封装到HttpServletRequest,通过这个HttpServletRequest的方法
    获得客户端的所有信息。
    
    • 1
    • 2
    • 3

    35.获取前端 传递的参数

    在这里插入图片描述

    index.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h1>登入</h1>
    <div>
        <form action="${pageContext.request.contextPath}/demo1" method="post">
            用户名: <input type="text" name="username" placeholder="请输入您的账号"> <br/>
            密码: <input type="password" name="password" placeholder="请输入您的密码"> <br/>
    
            爱好:
            <input type="checkbox" name="s" value="girl"> 女孩
            <input type="checkbox" name="s" value="sing"> 唱歌
            <input type="checkbox" name="s" value="song"> 跳舞
            <input type="checkbox" name="s" value="playGrame"> 打牌
            <input type="submit"> <br/>
        </form>
    </div>
    </body>
    </html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    succes.jsp

    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h1>欢迎来到吉士先生的世界?</h1>
    </body>
    </html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    java

    package com.Jsxs.Servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.Arrays;
    
    public class Demo1 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //        响应编码
            resp.setCharacterEncoding("utf-8");
    //        请求编码
            req.setCharacterEncoding("utf-8");
            String username = req.getParameter("username");
            String password = req.getParameter("password");
    //        获取多个
            String[] s = req.getParameterValues("s");
            System.out.println("=====================================");
            System.out.println(password);
            System.out.println(username);
            System.out.println(Arrays.toString(s));
            System.out.println("=====================================");
            resp.sendRedirect("/Servelete04_war/succes.jsp");
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    这里是引用

  • 相关阅读:
    JAVA案例驱动模式
    Http代理与socks5代理有何区别?如何选择?(二)
    【集训DAY N】函数【数学】
    快速解决“找不到msvcr120.dll无法执行代码”问题,总结5解决方法
    利用社群媒体打造雇主正面品牌形象的9种方法
    PB系统函数介绍:共享对象函数
    Unity Shader学习(六)实现雷达扫描效果
    华为AppLinking中统一链接的创建和使用
    【Linux】Linux 历史与环境搭建
    始祖双碳新闻 | 2022年8月3日碳中和行业早知道
  • 原文地址:https://blog.csdn.net/qq_69683957/article/details/127705683