Servlet-01

1. What is web development

1.web通信,指的是两台计算机通过网络进行资源文件共享活动

2.web开发确保本次通信顺利执行

2. The role between two computers in the process of web communication

1.客户端计算机:用于发送请求

2.服务端计算机:提供共享资源文件

	三、资源文件:
	1.理论上来说,计算机上所有的文件都是资源共享文件
	2.分类:
			1)静态资源文件:
					【定义】:文件内容是固定的,不能随着请求变化而发生变化
					【例子】:图片 html doc xls
					【特点】:服务端计算机直接将【静态资源文件】推送给【客户端计算机】
								所有被共享的【静态资源文件】最终是在客户端计算机运行
    		2)动态资源文件:
				【定义】:文件内容可以根据用户提供需求变化而产生不同的变化内容
				【特点】:服务端计算机会调用执行【动态资源文件】,服务端计算机将【动态资源文件的运行结果】交给客户端计算机

4. In the process of WEB communication, how do two computers communicate

		1.C/S结构通信方式:
					【介绍】:C:client,客户端计算机安装特定【客户端程序】,可以帮助客户端计算机向指定服务器计算机发送请求
					
		S:server,在服务端计算机安装一种特定【资源调度软件】,接收特定客户端程序发送请求,将请求需要资源文件推送回客户端计算机上【客户端程序】


					【例子】:微信  QQ  所有大型网络游戏
					
					【优缺点】:
					
						【优点】:
								1.可以有效的保护服务端计算机
								2.可以分摊服务端计算机工作压力
						
						【缺点】:
								1.增加客户获取服务的成本
								2.增加服务端维护成本
								
					【针对客户群体】:个人用户

		2.B/S结构通信方式:				
                【介绍】:B:browser,可以帮助客户端计算机向任意服务端发送请求
					
					   S:server,在服务端计算机安装一种特定【资源调度软件】,可以接受所有浏览器发送请求。将请求的资源文件推送给客户端计算机浏览器上
							
					【优缺点】:
						
						【优点】:
								1)不会增加客户获取服务的成本
								2)不会增加服务端维护的成本
						
						【缺点】:
								1)无法有效保护服务端计算机
								2)无法通过客户端计算机分摊服务端计算机的压力
							
					【针对客户群体】:企业用户

5. Network protocol package:
1. [Definition]: Used to transmit [binary data] in the package in the network.
The network protocol package exists [fixed space] to store [specific data], which is convenient for the receiver to analyze and obtain the data

	2.【分类】:FTP  http
	
	3.【B/S通信方式中】:到目前为止B/S通信结构中,只使用http网络协议包

Six, the internal structure of the http network protocol package:

1.Http:请求协议包:
						1)是浏览器在发送请求时,负责创建的
						2)时浏览器负责将请求信息,写入到请求协议包
						3)Http请求协议包内部自上而下有四层结构
						
							【请求行】:{
											url:请求的资源文件地址
											method:浏览器采用的请求方式 
							
							}
							
							【请求头】:{
											浏览器希望获得资源文件类型【html/png】
											浏览器希望获得资源文件内容依赖字符集
											如果浏览器采用【GET方式发送请求】,本次所关联的请求参数需要保存在请求头
							}
				
							【空白行】:{       隔离层      }
							
							【请求体】:{
											如果浏览器采用【post方式发送请求】,本次关联请求参数需要保存在请求体
							}

2. Http response protocol package:

						1)在服务端计算机的【资源调度软件】准备推送文件时,负责创建的
						2)【相应协议包内容】可以由【资源调度软件】写入,可以有Java命令(response)写入
						3)Http请求协议包内部自上而下有四层结构
							【状态行】:{
											状态栏
							}
							
							【响应头】:{
											返回文件内容类型(doc text html text/html)
											返回文件内容以来字符集,浏览器需要使用这个字符集对接收内容进行解码
							}
							
							【空白行】:{       }
							
							【响应体】:{
											静态资源文件也可能是动态资源文件
											运行结果(二进制形式)
							}
							
	---------------------------------------------------------------------------	
							
								客户端计算机上浏览器管理	
								
一、如何命令浏览器向指定网站发送请求:

	1.通过FORM表单命令<form action="http://www.baidu.com">
	
	2.通过超链接命令<a href="http://www.163.com">163.com</a>
	
	3.浏览器的地址栏对象window.location="http://www.sina.com"
	<html>
		<center>
			<h1>超链接命令浏览器向服务湍发送请求</h1>
			<a href="http://www.163.com">网易</ a>
			
			<h1>FORM命令浏览器向服务湍发送请求</h1>
			
			<form action=http://www.baidu . com">
				<input type=" submit" value="i度">
			</form>
			<h1>通过地址栏对象命令浏览器向服务端发送请求</h1>
			<input type="button" value="sina" onclick="fun1()">
			
			<script type=text/javascript">
			function fun1(i
			window . location="http://www.sina.com":l
			}
			</script>
		</ center>
	</html>

​​​​ 2.
Request method [POST/GET]
​ 1. GET
method:
​ 1) When the browser is required to send a request, the number of request parameters carried cannot exceed 4k
​ 2) The browser is required to write the request parameters to [Request Head]
​ 3) Require the browser to display the request parameter information in the address bar
4) Require the browser to cache the received resource files in
memory

	2.POST方式:
			1)不会限制浏览器发送请求时,携带请求参数数量
			2)要求浏览器将请求参数写入到【请求体】
			3)要求浏览器将请求参数信息在地址栏隐藏
			4)要求浏览器对接收的文件不能保存到缓存【阅后即焚】,【动态变化的内容,车票信息,股票价格】
	
三、如何控制浏览器采用的请求方式
	1.让浏览器发送请求时,采用【GET方式】
		1)超链接标签命令,一定会要求浏览器采用【GET】
		2)location对象,一定会要求浏览器采用【GET】
		3)FORM标签命令中method属性可以控制浏览器采用GET方式发送请求
			<form action="http://www.baidu.com" method="GET">
	
	2.让浏览器发送请求时,采用POST方式】
		1.设置FORM标签中method属性等于"POST"

​​​​ 4. How to order the browser to carry the specified request parameters when sending the request​ 1. The hyperlink tag command can instruct the browser to carry the request parameters





		<a href="http://www.baidu.com?uname=nike&age=23">百度</a>
		
	2.location对象,可以命令浏览器携带请求参数
	
		window.location="http://www.baidu.com?uname=nike&age=23"
		
		以上两种凡是存在弊端。请求参数内容时固定,无法根据客户具体要求来改变
		
	3.表单域标签可以接收用户输入的内容,作为请求参数内容
		1)【表单域标签分类】:
								INPUT   select   textarea
								
		2)【表单域标签作为请求参数条件】
							
							第一个条件:表单域标签必须声明在当前FORM标签内部
							
							第二个条件:表单域标签必须声明【name属性】,这个属性作为【请求参数名】
							
							第三个条件:针对checkbook和radio,只有被选中情况下,才可以作为请求参数

​​------------------------------------------------
-----------------------
HTTP
server
:

一、【介绍】:

		1.安装在服务器计算机上一种软件
		2.可以解析来自于浏览器发送的【请求协议包】
		3.调用服务端上的被请求的【资源共享文件】
		4.负责生成一个【响应协议包】
		5.负责将【文件内容】或则【文件运行结果】写入到【响应体】
		6.负责将【响应协议包】推送给【当前浏览器】
		
二、【HTTP服务器分类】:
		1.德国SAP:
		
		2.IBM:weblogic:
		
		3.JBOSS:
		
		4.Apache:Tomcat
	
三、【Tomcat介绍】:
		1.是一个由Java应用程序编写的资源文件调度器
		
		2.可以调用服务端静态资源文件和动【态资源文件】
	
		3.Tomcat负责生成响应协议包,负责将响应协议包推送到浏览器上
		
		4.Tomcat属于Apache

Servlet specification

一、JAVASE与JAVAEE的区别
	
	1.JAVASE:Java开发基本规范
	
	2.JAVAEE:Java企业级开发规范,指定了JVM管理系统与13类系统之间交流规则,因此JAVAEE规范有13种。
	JDBC规范就是JAVAEE规范中的一种,用于设置JVM与所有的【关系型数据库管理系统】进行交流规则。
	
二、servlet规范:
	1.servlet规范是JavaEE规范中的一种
	2.servlet规范中设置JVM与Http服务器之间的交流规则
	3.交流规则:
			1)定义何种Java类可以呗Http服务器来调用的(何种Java类可以作为动态资源文件)
			
			2)Http服务器应该如何管理【动态资源文件】
			
三、何种Java类可以呗tomcat调用,可以作为网站的【动态资源文件】


​​​​ 1. In the servlet
2.
[servlet interface implementation class] must
be created

四、如何开发一个servlet实现接口类:
		1.创建要给实现类,需要继承与【HttpServlet】
		
		2.【HttpServlet】存在Tomcat中lib/servlet-api.jar  :  javax.servlet.http.HttpServlet
		
		3.根据来访的浏览器采用的请求方式,重修对应doGet或doPost
		
		4.在/WEB-INF/lib中web.xml
		
			注册Servlet接口实现类,用过Tomcat当前网站存在了动态资源文件 
		
五、Tomcat调用Servlet接口实现类的流程:
		1.浏览器向服务端发送了一次请求
		
		http://localhost:8888/myWeb/first

2. Tomcat
receives this request. First request the resource file as a dynamic resource file. Go to web.xml for file location. If there is a call, if the corresponding dynamic resource file is not found. At this time, Tomcat thinks that this request is a static resource file, and locates it from the web. If there is a return, if it has not returned a 404 status code
​​ 3.Tomcat
to web.xml according to
the positioning

		4.<servlet-name>定位<servlet-class>找到本次请求调用Servlet
		
		5.Tomcat根据浏览器采用的请求方式,调用Servlet中doGet或者doPost
		
		6.在doGet/doPost方法调用完毕后,Tomcat负责将响应包推送给浏览器 

Sixth
, servlet request path writing rules: 4 types
​ 1. Specific request path:
​ Tomcat will only call the current servlet if the [resource file address] requested by the browser is [exactly consistent] with the content in the current
servlet
Example: OneSevlet
/one.do

						http://localhost"8080/myWeb/one.do  OneSevlet会被调用

​​​​ http
://localhost"8080/myWeb/abc/one.do OneSevlet will be invoked
[
Note]: The specific request path must
start

		2.前置条件请求路径:
						如果浏览器请求的【资源文件地址】以当前servlet中<url-pattern>的内容为开头的。Tomcat才会调用当前Servlet
						
						例子:OneSevlet    <url-pattern>/abc/*</url-patttern>
						
						"*":是一个通配符,描述是一个任意长度字符串
						如果当前【资源文件地址】是以"/abc/"为开头,就表示调用的资源Oneservlet
						
						http://localhost"8080/myWeb/abc/two.do   OneSevlet会被调用
						
					【注意】:具体请求路径,必须以"/"为开头,必须以"*"为结尾
						
		3.后置条件请求路径:
						如果浏览器请求的【资源文件地址】以当前servlet中<url-pattern>的内容为结尾的。Tomcat才会调用当前Servlet
						
						例子:OneSevlet    <url-pattern>*.do</url-patttern>
						
							http://localhost:555/myWeb/abc/one.do    OneSevlet会被调用
						
							http://localhost:555/myWeb/abc/one.do?uname=mike  OneSevlet会被调用
							
							请求行:[
										url:http://localhost:555/myWeb/abc/one.do
										method:get
									]
							
							请求头:[
										uname:mike
									]
						
							【注意】:不能以"/"为开头,必须以"*"开头
							
		4.通配符请求路径:当前Servlet可以接收对当前网站所有的请求
							
							例子  OneServlet    <url-pattern>/*</url-patttern>    
						
							http://localhost:555/myWeb/mm.jpg    OneServlet
							
							http://localhost:555/myWeb/one.do   OneServlet

5.
Request path priority: [specific request path] > [precondition] > [wildcard] > [postcondition]
http
://localhost:555/myWeb/abc/
one.do

					OneServle: <url-pattern>/abc/one.do</url-patttern>
						
					TwoServlet: <url-pattern>/abc/*</url-patttern>
					
					ThreeSevlet: <url-pattern>*.do</url-patttern>
					
					FourServlet: <url-pattern>/*</url-patttern>

Seven
. Servlet instance object [life cycle]
1. In the project, all Servlet instance objects are created by Tomcat; developers have no right to create Servlet instance objects
2.
[Creation timing]:
1) 【Default】: When the first user visits this Servlet. Tomcat is responsible for creating his instance
objects

					2)【人工干预】:要求Tomcat在启动时,创建Sevlet的实例对象
					
					<servlet>
								<servlet-name>TwoServlet</servlet-name>
								<servlet-class>com.zeng.oneSevlet.controller.TwoServlet</servlet-class>
								<load-on-startup>10</load-on-startup>
					</servlet>

				3.在Tomcat运行期间,一个Servlet实现类只能被创建一个实例对象
				
				4.在Tomcat关闭时,由Tomcat负责销毁所有的Servlet实例对象


							Servlet开发时需要使用五种工具对象

1. Overview:
1. HttpServletResponse interface: responsible for writing the operation result to the response packet

	2.HttpservletRequest接口:负责读取请求协议包信息
	
	3.ServletContext接口:可以为当前网站中所有的Servlet提供【共享数据】
	
	4.Cookie:再一次会话过程中,存储浏览器在服务器端产生的私人数据
	
	5.HttpSession接口:再一次会话过程中,存储浏览器在服务器端产生的私人数据

2. HttpServletResponse interface:
1. [Introduction]:
1) An interface from the servlet specification: Tomcat is responsible for providing this interface implementation class

			2)负责在Servlet运行完毕后,将相关数据写入到【响应头】和【响应体】
			
			3)开发人员习惯于将HttpServletResponse接口修饰对象,称为【响应对象】

​​​​ 2.response.setCharacterEncoding
("utf-8"); and response.setContentType("text/html");
​​ 1
) The same point: you can set the [Character Set
]

		2)不同点:response.setContentType()还可以设置内容类型

3. HttpServletRequest interface:
1. [Introduction]:
1) From an interface in the Servlet specification, the interface implementation class is provided by Tomcat
2) The object decorated by the HttpServletRequest interface can help developers read the [request protocol package] information , equivalent to【Scanner】

			3)一般习惯于将HttpServletRequest接口修饰对象成为【请求对象】

2.
[Function]:
1) Read [Request Line] information
String
url = request.getRequest();
String method = request.getMethod()
;

			2)读取【请求头】或者【请求体】中所有的【请求参数名】
			
				request.getParameterNames(); 
				
			3)读取请求头或者请求体中【参数内容】
			
			http://localhost:8080/myWeb/one.do?uname=mike
			String value = request.getParameter("请求参数名")
			
			http://localhost:8080/myWeb/one.do?uname=mike&uname=allean
			String array [] = request.getParameterValues("请求参数名")

Guess you like

Origin blog.csdn.net/Anakin01/article/details/112717746
01