1.web基本概念 Java Web
1.1、基本概念 web 开发:
web ,网页的意思,www.baidu.com
静态web
html,css,
提供给所有人看的数据始终不会发生变化!
动态web
提供给所有人看的数据会发生变化,每个人在不同的时间、不同的地点、看到的信息各不相同。
淘宝,几乎所有的网站
技术栈:Servlet/Jsp、Asp、Php
在java 中,动态web资源开发的技术通常为JavaWeb;
1.2、Web应用程序 web应用程序:可以提供浏览器访问的程序;
a.html、b.html …多个web资源,这些web资源可以被外界访问,对外界提供服务;
你们能访问到的任何一个页面或者资源,都存在于这个世界的某一个角落的计算机上
URL
这个同一的web资源会被放在同一文件夹下,web应用程序—-》Tomcat 服务器
一个web应用由多部分组成(静态web,动态web)
html,css,js
Jsp,Servlet
Java程序
jar包
配置文件(Properties)
web应用程序编写完毕后,若想提供给外界访问:需要一个服务器来统一管理;
1.3、静态Web *.htm, *.html 这些都是网页的后缀,如果服务器上一直存在这些东西,
在这里插入图片描述
静态web存在的缺点
Web 页面 无法动态更新,所有用户看到的是同一页面
轮播图,点击特性:伪动态
JavaScript
VBScript
它无法和数据库交互(数据无法持久化,用户无法交互)
1.4、动态web 页面会动态展示:“Web 的页面展示效果因人而异”;
在这里插入图片描述
缺点:
假如服务器的动态web资源出现了错误,我们需要重新编写我们的后台程序
优点:
Web 页面 可以动态更新,所有用户看到的都不是同一页面
它可以和数据库交互(数据持久化:注册、商品信息)
新手村:—-》魔鬼训练(分析原理,看源码) —-》PK场
2、web服务器 2.1、技术讲解 ASP:
微软:国内最流行的就是ASP;
在html中嵌入VB脚本,ASP+COM
在ASP开发中,基本一个页面都有几千行的业务代码,页面极其换乱。
维护成本考
C#
IIS
PHP:
PHP开发速度快,功能很强大,跨平台,代码很简单(70%)
无法承载大访问量的情况(有局限性)
JSP/Servlet:
B/S:浏览器和服务器
C/S:客户端和服务器
sun公司主推的B/S架构
基于java语言的(所有的大公司,或者一些开源的组件,都是用java写的)
可以承载三高问题带来的影响;
2.2、web服务器 服务器是一种被动的操作,用户处理用户的一些请求和给用户一些响应信息;
IIS 微软的 用来跑ASP程序的,windows中自带的
Tomcat Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。
下载:
安装 or 解压
了解配置文件和目录
这个东西的作用
1、安装Tomcat tomcat官网:
https://tomcat.apache.org/
在这里插入图片描述
2、tomcat启动和配置 文件夹作用
在这里插入图片描述
启动 、关闭t omcat
在这里插入图片描述
访问测试
1 2 3 4 http:// localhost:8080 /1 XML
在这里插入图片描述
在这里插入图片描述
可以配置启动的端口号
http: 80
https:443
mysql:3306
redis:6379
tomcat默认:8080
1 2 3 4 5 6 <Connector port= "8080" protocol= "HTTP/1.1" connectionTimeout= "20000" redirectPort= "8443" />123 XML
可以配置主机的名称
1 2 3 4 5 <Host name ="localhost" appBase ="webapps" unpackWARs ="true" autoDeploy ="true" > 12 XML
完整配置文件
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 <?xml version="1.0" encoding="UTF-8" ?> <Server port ="8005" shutdown ="SHUTDOWN" > <Listener className ="org.apache.catalina.core.AprLifecycleListener" SSLEngine ="on" /> <Listener className ="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className ="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className ="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources > <Resource name ="UserDatabase" auth ="Container" type ="org.apache.catalina.UserDatabase" description ="User database that can be updated and saved" factory ="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname ="conf/tomcat-users.xml" /> </GlobalNamingResources > <Service name ="Catalina" > <Connector port ="8080" protocol ="HTTP/1.1" connectionTimeout ="20000" redirectPort ="8443" /> <Engine name ="Catalina" defaultHost ="localhost" > <Realm className ="org.apache.catalina.realm.LockOutRealm" > <Realm className ="org.apache.catalina.realm.UserDatabaseRealm" resourceName ="UserDatabase" /> </Realm > <Host name ="localhost" appBase ="webapps" unpackWARs ="true" autoDeploy ="true" > <Valve className ="org.apache.catalina.valves.AccessLogValve" directory ="logs" prefix ="localhost_access_log" suffix =".txt" pattern ="%h %l %u %t " %r" %s %b" /> </Host > </Engine > </Service > </Server > 123456789101112131415161718192021222324252627282930313233343536373839404142 XML
3、高难度面试题 请你谈谈网站是如何访问的!
输入一个域名;点击回车
客户端在发送请求之前,会
先去检查 本机的 hosts 文件
有没有这个域名的映射
有: 直接返回对应的ip地址,在这个地址中,有我们需要访问的web程序,可以直接访问 127.0.0.1 www.bloghut.cn
没有:去DNS 服务器 找到的话就返回, 找不到就返回找不到
在这里插入图片描述
4、发布一个web网站 将自己写的网站放到服务器(Tomcat) 中指定的web应用的文件夹(webapps)下,就可以访问了
网站应该有的结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 --webapps :Tomcat服务器的web目录 - ROOT - bloghut : 网站的目录名 - WEB_INF - classes : java程序 - lib : web 应用依赖的jar包 - web.xml : 网站的配置文件 - index.html 默认的首页 - static -css - style.css -js -img XML
1 什么是Http 超文本传输协议(Hyper Text Transfer Protocol,HTTP)是一个简单的;请求-响应协议,它通常运行在TCP之上。
文本:html,字符串,~
超文本:图片,音乐,视频,定位,地图…
80
https:安全的
2、两个时代 http 1.0
http/1.0:客户端可以与web服务器连接后,只能获得一个web资源,断开连接
http 2.0
http/1.1:客户端可以与web服务器连接后,可以获得多个web资源。
3、Http请求 客户端—–发请求(Request)——-服务器
百度:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 Request URL: https://www.baidu.com/ Request Method: GET Status Code: 200 OK Remote Address: 14.215 .177.38 :443 1234 Accept : text/html Accept -Encoding: gzip, deflate, Accept -Language: zh-CN,zh;q=0.9 Connection: keep-alive Cookie: Host: www.baidu.com123456 XML
3.1.请求行
请求行中的请求方式:GET
请求方式:GET、POST、HEAD、DELETE、PUT
get :一次请求能够携带的参数比较少,大小限制,会在浏览器的url 地址栏显示内容,不安全,但高效。
post:一次请求能够携带的参数没有限制,大小没有限制,不会在浏览器的url 地址栏显示内容,安全,但不高效。
3.2.请求头 1 2 3 4 5 6 7 8 9 Accept Accept -Encoding Accept -Language Cache-Control Connection HOST: 123456 XML
4、Http响应 服务器—–响应(response)—–客户端
1 2 3 4 5 6 7 8 9 10 11 12 Cache -Control: private --缓存控制Connection : keep-alive --连接Content -Encoding: gzip --编码 Content -Type: text/html;charset=utf-8 --类型Date : Fri, 03 Sep 2021 15 :18 :56 GMT --当前时间Expires : Fri, 03 Sep 2021 15 :18 :23 GMT --当前时间Set -Cookie: BDSVRTM=0 ; path=/ -- cookie 会话技术Set -Cookie: BD_HOME=1 ; path=/Set -Cookie: H_PS_PSSID=123456789 XML
4.1.响应体 1 2 3 4 5 6 7 8 9 10 11 Accept Accept -Encoding Accept -Language Cache-Control Connection HOST: Refresh: Location 12345678 XML
4.2.响应状态码
200 请求响应成功
4xx 找不到资源 404
3** 请求重定向
5xx 服务器代码错误 500
5、常见面试题 当你的浏览器中地址栏输入地址并回车的一瞬间到页面能够展示回来,经历了什么?
我为什么要学习这个?
在JavaWeb开发中需要使用大量的jar包,我们手动导入;
如何能够让一个东西帮我导入和配置这些jar包
我们目前用来就是方便导入jar包的! maven的核心思想:约定大于配置 有约束,不要去违反
maven 会规定好你该如何去编写我们的Java代码,必须按照这个规范来;
2.下载安装Mavne 1 2 3 4 https: //maven.apache.org /1 JAVA
在这里插入图片描述
下载完成后解压即可
目录结构
bin 放一些可执行文件
boot 该目录只包含一个jar文件,plexus-classworlds-2.5.2.jar。maven就是用它来加载自己的类库的。
conf 该目录下最重要的是settings.xml 文件,它主要用于全局的定制maven的行为
lib 该目录包含了maven运行时的java类库
在这里插入图片描述
在我们的系统环境变量中 配置如下信息:
M2_HOME maven目录下的bin目录
MAVEN_HOME maven的目录
在系统的path 中配置 MAVEN_HOME
在这里插入图片描述
测试是否安装成功
在这里插入图片描述
4.阿里云镜像
镜像:mirros 作用:加速我们的下载
国内建议使用阿里云
1 2 3 4 5 6 7 8 9 <mirror > <id > alimaven</id > <name > aliyun maven</name > <url > http://maven.aliyun.com/nexus/content/groups/public/</url > <mirrorOf > central</mirrorOf > </mirror > 123456 JAVA
5.本地仓库 在本地的仓库,远程仓库
建立一个本地仓库:
1 2 3 4 <localRepository > D:\Program Files (x86)\maven_repository</localRepository > 1 JAVA
6.在idea中使用maven
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
idea中的maven设置
在这里插入图片描述
经常在idea中出现一个问题,就是项目自动创建完成后, 这个maven home 会使用idea默认的,我们如果发现了这个问题,手动改为本地。
在这里插入图片描述
7.创建一个普通的maven项目
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这个只有web的项目才有
在这里插入图片描述
8.标记文件夹功能
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
9.在idea中配置tomcat
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
解决警告问题 为什么会有这个问题,我们访问一个网站,需要指定一个文件夹的名字
在这里插入图片描述
这个过程叫虚拟路径映射
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
10.pom文件 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 pom.xml 是maven的 核心配置文件<?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 > cn.bloghut</groupId > <artifactId > demo1</artifactId > <version > 1.0-SNAPSHOT</version > <packaging > war</packaging > <name > demo1 Maven Webapp</name > <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 > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > 4.11</version > <scope > test</scope > </dependency > </dependencies > <build > <finalName > demo1</finalName > <pluginManagement > <plugins > <plugin > <artifactId > maven-clean-plugin</artifactId > <version > 3.1.0</version > </plugin > <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 > XML
Servlet 就是sun公司开发动态web的一门技术 sun 在这些API 中提供了一个接口:Servlet,如果你想开发一个Servlet 程序,只需要完成两个步骤
编写一个类,实现Servlet 接口
把开发号好的Java类部署到服务器中
把实现了Servlet接口的Java 程序 叫做:Servlet
2.HelloServlet Servlet 接口有两个默认的实现类:HttpServlet 、GenericServlet
关于mavne 父子工程的理解 父项目中会有
1 2 3 4 5 6 <modules> <module> servlet -01 </module></modules> 123 JAVA
子项目中会有
1 2 3 4 5 6 7 8 <parent > <artifactId > servlet</artifactId > <groupId > cn.bloghut</groupId > <version > 1.0-SNAPSHOT</version > </parent > 12345 JAVA
父项目中的jar包 子项目可以直接使用
子项目中的jar包 父项目不能使用
编写一个普通类,实现Servlet接口,这里我们直接继承httpServlet
1 2 3 4 5 6 public class HelloServlet extends HttpServlet { }123 JAVA
在这里插入图片描述
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException { PrintWriter writer = resp.getWriter(); writer.println("hello,Servlet" ); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException { doGet(req,resp); } }123456789101112131415161718 JAVA
3.编写Servlet的映射 为什么需要映射:我们写的Java程序,但是要通过浏览访问,而浏览器需要连接web服务器,所以我们需要在web服务中注册我们写的Servlet,还需要给他一个浏览器能够访问的路径。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app xmlns ="http://java.sun.com/xml/ns/javaee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version ="3.0" > <servlet > <servlet-name > hello</servlet-name > <servlet-class > cn.bloghut.servlet.HelloServlet</servlet-class > </servlet > <servlet-mapping > <servlet-name > hello</servlet-name > <url-pattern > /hello</url-pattern > </servlet-mapping > </web-app > 123456789101112131415161718192021 JAVA
配置tomcat
测试
在这里插入图片描述
4.Servlet 原理 Servlet 是由web服务器调用,web服务器在收到浏览器请求之后,会调用相关API对请求处理。
在这里插入图片描述
5.Mapping 问题: 一个请求(Servlet)可以指定一个映射路径
1 2 3 4 5 6 7 8 9 10 11 12 <servlet > <servlet-name > hello</servlet-name > <servlet-class > cn.bloghut.servlet.HelloServlet</servlet-class > </servlet > <servlet-mapping > <servlet-name > hello</servlet-name > <url-pattern > /hello</url-pattern > </servlet-mapping > 123456789 JAVA
一个请求(Servlet)可以指定多个映射路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <servlet > <servlet-name > hello</servlet-name > <servlet-class > cn.bloghut.servlet.HelloServlet</servlet-class > </servlet > <servlet-mapping > <servlet-name > hello</servlet-name > <url-pattern > /hello1</url-pattern > </servlet-mapping > <servlet-mapping > <servlet-name > hello</servlet-name > <url-pattern > /hello2</url-pattern > </servlet-mapping > <servlet-mapping > <servlet-name > hello</servlet-name > <url-pattern > /hello3</url-pattern > </servlet-mapping > 12345678910111213141516171819 JAVA
一个请求(Servlet)可以指定通用映射路径
1 2 3 4 5 6 7 8 <servlet-mapping > <servlet-name > hello</servlet-name > <url-pattern > /*</url-pattern > </servlet-mapping > 12345 JAVA
指定一些后缀或者前缀等
1 2 3 4 5 6 7 <servlet-mapping > <servlet-name > hello</servlet-name > <url-pattern > *.do</url-pattern > </servlet-mapping > 1234 JAVA
优先级问题 指定了固有的映射路径优先级最高,如果找不到就会走默认的处理请求;
6.ServletContext web容器在启动的时候,它会为每个web程序都创建一个ServletContext对象,它代表了当前的web应用。
6.1.共享数据 我在这个Servlet中保存的数据
在这里插入图片描述
放置数据的Servlet
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 public class setData extends HttpServlet { @Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("hello" ); ServletContext servletContext = this .getServletContext(); String username = "闲言" ; servletContext.setAttribute("username" ,username); } @Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } } 读取数据的Servletpublic class GetData extends HttpServlet { @Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this .getServletContext(); String username = (String) context.getAttribute("username" ); resp.setCharacterEncoding("utf-8" ); resp.setContentType("text/html" ); PrintWriter writer = resp.getWriter(); writer.write(username); } @Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 JAVA
6.2获取初始化参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <context-param> <param-name>url</param-name> <param-value>jdbc:mysql: </context-param>1234 public class ServletDemo03 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException { ServletContext context = this .getServletContext(); String url = context.getInitParameter("url" ); System .out.println("url: " +url); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException { doGet(req,resp); } }12345678910111213141516 JAVA
6.3 请求转发 路径不会发生变化
1 2 3 4 5 6 7 8 9 10 11 12 @Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this .getServletContext(); RequestDispatcher dispatcher = context.getRequestDispatcher("/get" ); dispatcher.forward(req,resp); }123456789 JAVA
在这里插入图片描述
6.4 读取配置文件 Properties 在resources 目录下新建properties 在java 目录下新建properties发现 :都被打包到了同一个路径下:classes,我们俗称这个路径为classpath;思路 :需要一个文件流
1 2 3 4 5 6 properties文件username = xy123password = 123 123 JAVA
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class ServletDemo05 extends HttpServlet { @Override protected void do Get(HttpServletRequest req , HttpServletResponse resp ) throws ServletException, IOException { InputStream stream = this.getServletContext() .getResourceAsStream("/WEB-INF/classes/cn/bloghut/servlet/aa.properties" ) ; Properties pt = new Properties() ; pt.load(stream); String username = pt.getProperty("username" ) ; String password = pt.getProperty("password" ) ; resp.getWriter() .println(username+":" +password); } @Override protected void do Post(HttpServletRequest req , HttpServletResponse resp ) throws ServletException, IOException { do Get(req ,resp ) ; } }1234567891011121314151617 JAVA
效果
在这里插入图片描述
maven资源导出失败问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <build > <resources > <resource > <directory > src/main/resources</directory > <includes > <include > **/*.xml</include > <include > **/*.properties</include > </includes > </resource > <resource > <directory > src/main/java</directory > <includes > <include > **/*.properties</include > <include > **/*.xml</include > </includes > </resource > </resources > </build > 123456789101112131415161718 JAVA
6.5HttpServletResponse web服务器接收到客户端的http请求,会针对这个请求,分别创建一个代表请求的HttpServletRequest对象,代表响应的一个HttpServletRespoonse;
如果要获取客户端请求过来的参数:找HttpServletRequest
如果要给客户端响应一些信息:找HttpServletResponse
简单分类
负责向浏览器发送数据的方法
1 2 3 4 5 public ServletOutputStream getOutputStream () throws IOException ;public PrintWriter getWriter () throws IOException ;12 JAVA
负责向浏览器写一些响应头的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public void setCharacterEncoding (String charset) ;public void setContentLength (int len) ;public void setContentLengthLong (long len) ;public void setContentType (String type) ;public void setDateHeader (String name, long date) ;public void addDateHeader (String name, long date) ;public void setHeader (String name, String value) ;public void addHeader (String name, String value) ;public void setIntHeader (String name, int value) ;public void addIntHeader (String name, int value) ;1234567891011 JAVA
响应的状态码常量
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 public static final int SC_CONTINUE = 100;public static final int SC_SWITCHING_PROTOCOLS = 101; public static final int SC_OK = 200;public static final int SC_CREATED = 201;public static final int SC_ACCEPTED = 202;public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;public static final int SC_NO_CONTENT = 204;public static final int SC_RESET_CONTENT = 205;public static final int SC_PARTIAL_CONTENT = 206; public static final int SC_MULTIPLE_CHOICES = 300; public static final int SC_MOVED_PERMANENTLY = 301; public static final int SC_MOVED_TEMPORARILY = 302; public static final int SC_FOUND = 302;public static final int SC_SEE_OTHER = 303;public static final int SC_NOT_MODIFIED = 304;public static final int SC_USE_PROXY = 305;public static final int SC_TEMPORARY_REDIRECT = 307;public static final int SC_BAD_REQUEST = 400;public static final int SC_UNAUTHORIZED = 401;public static final int SC_PAYMENT_REQUIRED = 402;public static final int SC_FORBIDDEN = 403;public static final int SC_NOT_FOUND = 404;public static final int SC_METHOD_NOT_ALLOWED = 405; public static final int SC_NOT_ACCEPTABLE = 406; public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;public static final int SC_REQUEST_TIMEOUT = 408;public static final int SC_CONFLICT = 409; public static final int SC_GONE = 410;public static final int SC_LENGTH_REQUIRED = 411; public static final int SC_PRECONDITION_FAILED = 412; public static final int SC_REQUEST_ENTITY_TOO_LARGE = 413; public static final int SC_REQUEST_URI_TOO_LONG = 414; public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416; public static final int SC_EXPECTATION_FAILED = 417; public static final int SC_INTERNAL_SERVER_ERROR = 500; public static final int SC_NOT_IMPLEMENTED = 501; public static final int SC_BAD_GATEWAY = 502;public static final int SC_SERVICE_UNAVAILABLE = 503; public static final int SC_GATEWAY_TIMEOUT = 504;public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505; 1234567891011121314151617181920212223242526272829303132333435363738394041 JAVA
下载文件 1.向浏览器输出消息 2.下载文件 2.1.要获取文件的路径 2.2下载的文件名是啥 2.3设置想办法让浏览器能够支持下载我们需要的东西 2.4获取下载文件的输入流 2.创建缓冲区 2.5获取OutputStream对象 2.6将FileOutputStream流写入到buff缓冲区 2.7使用OutputStream将缓冲区中的数据输出到客户端!
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 @Override protected void do Get(HttpServletRequest req , HttpServletResponse resp ) throws ServletException, IOException { String realPath = "G:\\JavaWeb\\狂\\servlet\\response-2\\target\\classes\\1.png" ; System . out.println("下载路径:" + realPath); String fileName = realPath.substring(realPath.lastIndexOf("//" ) + 1 ); resp.setHeader("Content-Disposition" , "attachment;fileName=" + URLEncoder.encode (fileName ,"UTF-8" ) ); FileInputStream in = new FileInputStream(realPath ) ; int len = 0 ; byte[] buffer = new byte[1024 ] ; ServletOutputStream out = resp.getOutputStream() ; while ((len = in .read(buffer)) > 0 ) { out.write(buffer,0 ,len); } out.close() ; in .close() ; } @Override protected void do Post(HttpServletRequest req , HttpServletResponse resp ) throws ServletException, IOException { do Get(req , resp ) ; }12345678910111213141516171819202122232425262728293031 JAVA
验证码功能 验证码怎么来的? 1.前端实现 2.后端实现,需要用到Java图片类,生成一个图片
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 public class ImageServlet extends HttpServlet { @Override protected void do Get(HttpServletRequest req , HttpServletResponse resp ) throws ServletException, IOException { resp.setHeader("refresh" , "3" ) ; BufferedImage bufferedImage = new BufferedImage(80, 20, BufferedImage.TYPE_INT_RGB) ; Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics() ; graphics.setColor(Color.white ) ; graphics.fillRect(0, 0, 80, 20) ; graphics.setColor(Color.BLUE) ; graphics.setFont(new Font(null ,Font.BOLD,20) ); graphics.drawString(makeNumber () ,0 ,20 ); resp.setContentType("image/png" ) ; resp.setDateHeader("expires" ,-1) ; resp.setHeader("Cache-Control" ,"no-cache" ) ; resp.setHeader("Pragma" ,"no-cache" ) ; ImageIO . write(bufferedImage,"jpg" ,resp.getOutputStream() ); } private String makeNumber() { Random random = new Random() ; String str = random.nextInt(9999) + "" ; StringBuffer sb = new StringBuffer() ; for (int i = 0 ; i < 4 - str.length() ; i++) { sb.append("0" ); } return sb.to String() + str; } @Override protected void do Post(HttpServletRequest req , HttpServletResponse resp ) throws ServletException, IOException { do Get(req , resp ) ; } }1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 JAVA
实现重定向
在这里插入图片描述
B一个web资源收到客户端A请求后,B他会通知客户端去访问另一个web资源,这个过叫重定向。
用户登录
1 2 3 4 public void sendRedirect (String location) throws IOException ;1 JAVA
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 @Override protected void do Get(HttpServletRequest req , HttpServletResponse resp ) throws ServletException, IOException { resp.sendRedirect("/r2/img" ) ; }12345678910 JAVA
面试题:请你聊聊重定向和转发的区别?
相同点 页面都会实现跳转
不同点 请求转发的时候,url不会产生变化 重定向的时候,url地址栏会发生变化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Override protected void do Get(HttpServletRequest req , HttpServletResponse resp ) throws ServletException, IOException { String username = req.getParameter("username" ) ; String password = req.getParameter("password" ) ; System . out.println("username:" +username+" password:" +password); resp.sendRedirect("/r2/success.jsp" ) ; } @Override protected void do Post(HttpServletRequest req , HttpServletResponse resp ) throws ServletException, IOException { do Get(req ,resp ) ; }123456789101112131415 JAVA
页面提交过来的HTTP请求包含了参数,放在URL里面了,tomcat自动解析封装在request对象里,再调用service方法,request可以根据键得到传过来的值
6.6HttpServletRequest HttpServletRequest代表客户端的请求,用户通过Http协议访问服务器,Http请求中的所有信息会被封装到HttpServletRequest,通过这个HttpServletRequest中的方法,获取客户端的所有信息。
在这里插入图片描述
获取前端传递的参数
在这里插入图片描述
请求转发 1.转发时”/“代表的是本应用程序的根目录 重定向时”/“代表的是webapps目录
2.getRequestDispatcher分成两种,可以用request调用,也可以用getServletContext()调用 不同的是request.getRequestDispatcher(url)的url可以是相对路径也可以是绝对路径。而this.getServletContext().getRequestDispatcher(url)的url只能是绝对路径。
转发是一个web应用内部的资源跳转
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 public class LoginServlet extends HttpServlet { @Override protected void do Get(HttpServletRequest req , HttpServletResponse resp ) throws ServletException, IOException { req.setCharacterEncoding("utf-8" ) ; String username = req.getParameter("username" ) ; String password = req.getParameter("password" ) ; String[] hobbys = req.getParameterValues("hobbys" ) ; System . out.println(username+" : " +password+" :" + Arrays .to String(hobbys ) ); req.getRequestDispatcher("/success.jsp" ) .forward(req,resp); } @Override protected void do Post(HttpServletRequest req , HttpServletResponse resp ) throws ServletException, IOException { do Get(req ,resp ) ; } } 1234567891011121314151617181920212223 JAVA
面试题:请你聊聊重定向和转发的区别?
相同点 页面都会实现跳转
不同点 请求转发的时候,url不会产生变化 重定向的时候,url地址栏会发生变化