jsp程序设计(三)-初试jsp程序设计(2)-Servlet简介

一、什么是Servlet

Servlet 是一个用 Java 编写的程序,此程序在服务器上运行以处理客户端请求

二、接口和类

1、javax.servlet 包中的接口

2、javax.servlet 包中的类

ServletInputStream 用于从客户端读取二进制数据

ServletOutputStream 用于将二进制数据发送到客户端;

HttpServletResponse 接口 提供 HTTP 响应

三、Servlet 的生命周期

Servlet 生命周期就是指创建 Servlet 实例之后其存在的时间以及何时消失

1、生命周期的 3个方法为:

<shape id="_x0000_s1026" style="MARGIN-TOP: 7.8pt; Z-INDEX: 1; LEFT: 0px; MARGIN-LEFT: 0px; WIDTH: 81pt; POSITION: absolute; HEIGHT: 23.4pt; TEXT-ALIGN: left; mso-wrap-style: none; mso-position-horizontal: absolute; mso-position-vertical: absolute; v-text-anchor: middle" strokeweight="1.25pt" strokecolor="#036" fillcolor="#3cf" type="#_x0000_t176"><fill type="gradientRadial" focussize="" focusposition=".5,.5"></fill><shadow opacity=".5" offset2=",2pt" offset=",3pt" on="t"></shadow><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center; mso-layout-grid-align: none" align="center"><strong><span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 20.0pt; mso-fareast-font-family: 黑体; mso-bidi-font-family: 'Times New Roman'">init()<p></p></span></strong></p> </div> </td> </tr></tbody></table></textbox></shape><shape id="_x0000_s1028" style="MARGIN-TOP: 7.8pt; Z-INDEX: 3; LEFT: 0px; MARGIN-LEFT: 252pt; WIDTH: 81pt; POSITION: absolute; HEIGHT: 23.4pt; TEXT-ALIGN: left; mso-wrap-style: none; mso-position-horizontal: absolute; mso-position-vertical: absolute; v-text-anchor: middle" strokeweight="1.25pt" strokecolor="#036" fillcolor="#3cf" type="#_x0000_t176"><fill type="gradientRadial" focussize="" focusposition=".5,.5"></fill><shadow opacity=".5" offset2=",2pt" offset=",3pt" on="t"></shadow><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center; mso-layout-grid-align: none" align="center"><strong><span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 20.0pt; mso-fareast-font-family: 黑体; mso-bidi-font-family: 'Times New Roman'">destroy()<p></p></span></strong></p> </div> </td> </tr></tbody></table></textbox></shape><shape id="_x0000_s1027" style="MARGIN-TOP: 7.8pt; Z-INDEX: 2; LEFT: 0px; MARGIN-LEFT: 126pt; WIDTH: 81pt; POSITION: absolute; HEIGHT: 23.4pt; TEXT-ALIGN: left; mso-wrap-style: none; mso-position-horizontal: absolute; mso-position-vertical: absolute; v-text-anchor: middle" strokeweight="1.25pt" strokecolor="#036" fillcolor="#3cf" type="#_x0000_t176"><fill type="gradientRadial" focussize="" focusposition=".5,.5"></fill><shadow opacity=".5" offset2=",2pt" offset=",3pt" on="t"></shadow><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center; mso-layout-grid-align: none" align="center"><strong><span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 20.0pt; mso-fareast-font-family: 黑体; mso-bidi-font-family: 'Times New Roman'">service()<p></p></span></strong></p> </div> </td> </tr></tbody></table></textbox></shape>


<stroke endarrow="block"></stroke><stroke endarrow="block"></stroke>

2、生命周期的各个阶段为

实例化 Servlet 容器创建 Servlet 的实例

初始化 该容器调用 init() 方法

服 务 如果请求 Servlet,则容器调用 service() 方法

销 毁 销毁实例之前调用 destroy() 方法

不可用 销毁实例并标记为垃圾收集

四、HTTP Servlet 基础知识

public void doGet(HttpServletRequest req,HttpServletResponse res) public void doPost(HttpServletRequest req,HttpServletResponse res)

doGet() 方法处理客户端作出的 GET 请求。doPost() 方法处理客户端作出的 POST 请求。

五、Servlet 的部署

web.xml文件中配置Servlet

<fill type="gradient" focus="100%"></fill><lock grouping="t" v:ext="edit"></lock><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 27pt; TEXT-INDENT: -27pt; mso-layout-grid-align: none"><span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 19.0pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'"><span style="mso-spacerun: yes"> </span>&lt;servlet&gt;<p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 27pt; TEXT-INDENT: -27pt; mso-layout-grid-align: none"><span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 19.0pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'"><span style="mso-spacerun: yes"> </span>&lt;servlet-name&gt;clientservlet&lt;/servlet-name&gt;<p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 27pt; TEXT-INDENT: -27pt; mso-layout-grid-align: none"><span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 19.0pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'"><span style="mso-spacerun: yes"> </span>&lt;servlet-class&gt;chapter2.ClientServlet&lt;/servlet-class&gt;<p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 27pt; TEXT-INDENT: -27pt; mso-layout-grid-align: none"><span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 19.0pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'"><span style="mso-spacerun: yes"> </span>&lt;/servlet&gt;<p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 27pt; TEXT-INDENT: -27pt; mso-layout-grid-align: none"><span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 19.0pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'"><span style="mso-spacerun: yes"> </span>&lt;servlet-mapping&gt;<p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 27pt; TEXT-INDENT: -27pt; mso-layout-grid-align: none"><span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 19.0pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'"><span style="mso-spacerun: yes"> </span>&lt;servlet-name&gt;clientservlet&lt;/servlet-name&gt;<p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 27pt; TEXT-INDENT: -27pt; mso-layout-grid-align: none"><span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 19.0pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'"><span style="mso-spacerun: yes"> </span>&lt;url-pattern&gt;/clientservlet&lt;/url-pattern&gt;</span><span lang="EN-US" style="FONT-SIZE: 19pt; COLOR: black; FONT-FAMILY: Arial; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 27pt; TEXT-INDENT: -27pt; mso-layout-grid-align: none"><span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 19.0pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'"><span style="mso-spacerun: yes"> </span>&lt;/servlet-mapping&gt;<p></p></span></p> </div> </td> </tr></tbody></table></textbox><shapetype id="_x0000_t202" path="m0,0l0,21600,21600,21600,21600,0xe" o:spt="202" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><path o:connecttype="rect" gradientshapeok="t"></path></shapetype><shape id="_x0000_s1034" style="MARGIN-TOP: 60pt; Z-INDEX: 9; MARGIN-LEFT: 162pt; WIDTH: 96.35pt; POSITION: absolute; HEIGHT: 23.4pt; mso-wrap-style: none; mso-position-horizontal: absolute; mso-position-vertical: absolute; v-text-anchor: top-baseline" strokeweight="1.25pt" fillcolor="#fc0" type="#_x0000_t202"><fill type="gradient" focus="100%"></fill><shadow opacity=".5" offset2="2pt,2pt" offset="3pt,3pt" on="t"></shadow><textbox style="mso-fit-shape-to-text: t"><table style="WIDTH: 128px; HEIGHT: 21px" cellspacing="0" cellpadding="0" width="128"><tbody><tr> <td style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: 宋体; mso-bidi-font-size: 18.0pt; mso-ansi-language: ZH-CN; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial">两个名称必须相同</span><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 18.0pt; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: ZH-CN"><p></p></span></p> </div> </td> </tr></tbody></table></textbox></shape><stroke endarrow="block"></stroke>

HttpServlet 类提供一个抽象类以创建 HTTP Servlet,实现其两个方法:

3、javax.servlet.http 包的接口

HttpServletRequest 接口 提供 HTTP 请求信息

ServletConfig 接口在初始化的过程中由 Servlet 容器使用;

ServletContext 接口 Servlet 用于获取来自其容器的信息的方法;

ServletRequest 接口 向服务器请求信息;

ServletResponse 接口 响应客户端请求

你可能感兴趣的:(servlet)