1.Servlet技术
1.1 什么是servlet?
- Servlet 是 JavaEE 规范之一。规范就是接口
- Servlet 就 JavaWeb 三大组件之一。三大组件分别是:Servlet 程序、Filter 过滤器、Listener 监听器。
- Servlet 是运行在服务器上的一个 java 小程序,它可以接收客户端发送过来的请求,并响应数据给客户端。
1.2手动取实现一个servlet程序
- 1、编写一个类去实现 Servlet 接口
- 2、实现 service 方法,处理请求,并响应数据
- 3、到 web.xml 中去配置
public class HelloServlet implements Servlet {
/**
* service是专门用来处理响应和请求的
* @param servletRequest
* @param servletResponse
* @throws ServletException
* @throws IOException
*/
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println ("service 被访问了...");
- web.xml 中的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- servlet标签给Tomcat服务器配置Servlet程序-->
<servlet>
<!--servlet-name标签给servlet程序取一个别名(一般是类名)-->
<servlet-name>HelloServlet</servlet-name>
<!--servlet-class是Servlet程序的全类名-->
<servlet-class>com.guigu.servlet.HelloServlet</servlet-class>
</servlet>
<!-- servlet-mapping标签给程序配置访问地址 -->
<servlet-mapping>
<!-- servlet-name标签的作用是告诉服务器 我当前匹配的地址给哪个Servlet程序使用 -->
<servlet-name>HelloServlet</servlet-name>
<!-- url-pattern标签的配置访问地址
/ 斜杠在服务器解析时表示地址为:http://ip:port/工程路径
/hello 表示地址为 :http://ip:port/工程路径/hello
-->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
注意: url-pattern 中配置的路径要以斜杠打头
1.3 Servlet 的生命周期
Servlet 生命周期可被定义为从创建直到毁灭的整个过程。以下是 Servlet 遵循的过程:
- 执行servlet构造器
- Servlet 通过调用 init () 方法进行初始化。 Servlet 调用
注意:第一、二步,是在第一次访问,的时候创建 servlet程序的时候会调用
- service() 方法来处理客户端的请求。
每次访问都会调用
- Servlet 通过调用 destroy() 方法终止(结束)。 最后,Servlet 是由 JVM 的垃圾回收器进行垃圾回收的.
在 web工程停止的时候会调用
1.4 GET 和POST的请求分发处理
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println ("service 被访问了...");
HttpServletRequest httpServletRequest= (HttpServletRequest) servletRequest;
//获取请求的方式
String method = httpServletRequest.getMethod ();
if ("GET".equals (method)){
doGet ();
}else if ("POST".equals (method)){
doPost ();
}
}
public void doGet(){
System.out.println ("doGet请求方式");
System.out.println ("doGet请求方式");
}
public void doPost(){
System.out.println ("doPost请求方式");
System.out.println ("doPost请求方式");
}
1.5 通过继承 HttpServlet 实现 Servlet 程
- 一般在实际项目开发中,都是使用继承 HttpServlet 类的方式去实现 Servlet 程序。
- 编写一个类去继承 HttpServlet 类
- 根据业务需要重写 doGet 或 doPost 方法
- 到 web.xml 中的配置 Servlet 程序的访问地址
public class HelloServlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println ("HelloSrevlet2 doGet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println ("HelloSrevlet2 doPost");
}
}
web.xml中的配置:
<servlet>
<servlet-name>HelloServlet2</servlet-name>
<servlet-class>com.atguigu.servlet.HelloServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet2</servlet-name>
<url-pattern>/hello2</url-pattern>
</servlet-mapping>
1.6 Servlet 类的继承体系
2.ServletConfig 类
-
ServletConfig 类从类名上来看,就知道是 Servlet 程序的配置信息类。
-
Servlet 程序和 ServletConfig 对象都是由 Tomcat 负责创建,我们负责使用。
-
Servlet 程序默认是第一次访问的时候创建,ServletConfig 是每个 Servlet 程序创建时,就创建一个对应的
ServletConfig 对象2.1 ServletConfig类的三大作用
1、可以获取 Servlet 程序的别名 servlet-name 的值
2、获取初始化参数 init-param
3、获取 ServletContext 对象
web.xml 中的配置:
<!-- servlet标签给Tomcat服务器配置Servlet程序-->
<servlet>
<!--servlet-name标签给servlet程序取一个别名(一般是类名)-->
<servlet-name>HelloServlet</servlet-name>
<!--servlet-class是Servlet程序的全类名-->
<servlet-class>com.guigu.servlet.HelloServlet</servlet-class>
<init-param>
<!-- 是参数名-->
<param-name>username</param-name>
<!-- 是参数值-->
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/test</param-value>
</init-param>
</servlet>
<!-- servlet-mapping标签给程序配置访问地址 -->
<servlet-mapping>
<!-- servlet-name标签的作用是告诉服务器 我当前匹配的地址给哪个Servlet程序使用 -->
<servlet-name>HelloServlet</servlet-name>
<!-- url-pattern标签的配置访问地址
/ 斜杠在服务器解析时表示地址为:http://ip:port/工程路径
/hello 表示地址为 :http://ip:port/工程路径/hello
-->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
Servlet中的代码:
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println ("init 初始化方法");
// 1. 可以获取servlet程序的别名servlet-name的值
System.out.println ("HelloServlet程序的别名是:"+servletConfig.getServletName ());
// 2. 获取初始化参数 init-param
System.out.println ("初始化参数username的值是:"+servletConfig.getInitParameter ("username"));
// 3. 获取ServletContecxt对象
System.out.println ("初始化参数url的值是:"+servletConfig.getServletContext ());
}
注意:
3.ServletContext 类
3.1 什么是 ServletContext类?
- ServletContext 是一个接口,它表示 Servlet 上下文对象
- 一个 web 工程,只有一个ServletContext 对象实例。
- ServletContext 对象是一个域对象。
- ServletContext 是在web 工程部署启动的时候创建。在 web 工程停止的时候销毁
域对象:域对象,是可以像 Map 一样存取数据的对象,叫域对象。这里的域指的是存取数据的操作范围,整个 web 工程。
3.2 ServletContext 类的四个作用
- 获取 web.xml 中配置的上下文参数 context-param
- 获取当前的工程路径,格式: /工程路径
- 获取工程部署后在服务器硬盘上的绝对路径
- 像 Map一样存取数据
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 获取web.xml中配置文件的上下文参数 context-param
ServletContext context = getServletConfig ().getServletContext ();
String username = context.getInitParameter ("username");
System.out.println ("context-param参数username值是:"+username);
System.out.println ("context-param参数password值是:"+context.getInitParameter ("password"));
// 2. 获取当前的工程路径 格式:工程路径
System.out.println ("当前的工程路径:"+context.getContextPath ());
// 3. 获取工程部署后在服务器硬盘上的绝对路径
/*
斜杠服务解析器地址为:http://ip:port/工程名/ 映射到IDEA代码的web目录
*/
System.out.println ("工程部署额真实路径:"+context.getRealPath ("/"));
<!-- context-param-->
<context-param>
<param-name>username</param-name>
<param-value>context</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>root</param-value>
</context-param>
ContextServlet1 代码:
public class ContextServlet1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 4. 像Map一样存取数据
//获取servletContext对象
ServletContext context =getServletContext ();
System.out.println (context);
System.out.println ("保存之前:context中获取数据key1的值是:"+context.getAttribute ("key1"));
context.setAttribute ("key1","value1");
System.out.println ("context1中获取数据key1的值是:"+context.getAttribute ("key1"));
}
ContextServlet2 代码:
public class ContextServlet2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext ();
System.out.println (context);
System.out.println ("Context2中获取数据key1的值是:"+context.getAttribute ("key1"));
}
4.HTTP协议
4.1请求的 HTTP 协议格式
- 客户端给服务器发送数据叫请求。
- 服务器给客户端回传数据叫响应。
- 请求又分为 GET 请求,和 POST 请求两种:
GET 请求
1、请求行
(1) 请求的方式 GET
(2) 请求的资源路径[+?+请求参数]
(3) 请求的协议的版本号 HTTP/1.1
2、请求头
key : value 组成 不同的键值对,表示不同的含义。
POST 请求
1、请求行
(1) 请求的方式 POST
(2) 请求的资源路径[+?+请求参数]
(3) 请求的协议的版本号 HTTP/1.1
2、请求头 1) key : value 不同的请求头,有不同的含义
空行
3、请求体 ===>>>就是发送给服务器的数据
4.2常用请求头的说明
- Accept: 表示客户端可以接收的数据类型
- Accpet-Languege: 表示客户端可以接收的语言类型
- User-Agent: 表示客户端浏览器的信息
- Host:表示请求时的服务器 ip 和端口号
4.3. 哪些是 GET 请求,哪些是 POST 请求
GET 请求有哪些:
- form 标签 method=get
- a 标签
- link 标签引入 css
- Script 标签引入 js 文件
- img 标签引入图片
- iframe 引入 html 页面
- 在浏览器地址栏中输入地址后敲回车
POST 请求有哪些:
- form 标签
4.4 响应的 HTTP 协议格式
1、响应行
(1) 响应的协议和版本号
(2) 响应状态码
(3) 响应状态描述符
2、响应头
(1) key : value 不同的响应头,有其不同含义
空行
3、响应体 ---->>> 就是回传给客户端的数据
4.5常用的响应码说明
200 表示请求成功
302 表示请求重定向
404 表示请求服务器已经收到了,但是你要的数据不存在(请求地址错误)
500 表示服务器已经收到请求,但是服务器内部错误(代码错误)