什么是Servlet ?
Servlet是Server Applet的简称,意思为用Java编写的服务器端的程序. 它运行在web服务器中,web服务器负责Servlet和客户的通信以及调用Servlet方法,Servlet和客户通信采用“请求/响应”的模式。
Servlet的作用
1.接收用户发送的请求 2.调用其他的java程序来处理请求 3.根据处理结果,服务器将响应返回给客户
Servlet创建和使用
● 1.创建一个类继承javax.servlet.http包下的HttpServlet ● 2.在web.xml文件中配置Servlet ● 3.运行Servlet程序
通过浏览器访问http:localhost:8080/ helloword/配置的url-pattern ● 由于客户端是通过URL地址访问web服务器中的资源,所以Servlet程序若想被外界访问,必须把servlet程序映射到一个URL地址上,这个工作在web.xml文件中使用元素和元素完成。
创建发布web项目
第一步:继承HttpServlet方法
建立DemoServlet项目,对HttpServlet对象进行继承

对于上图中错误是因为缺少相对应的servlet.jar包我们将jar包放入相对应web包中,我讲jar包放入WEB-INF中的自建lib包中,再将jar包添加为库,刷新后jar包中的HttpServlet类就可以使用了

第二步:重写HttpServlet方法
在这里我们将重写HttpServlet中的三个方法以便后续了解

第三步:文件配置
为了能使我们预定好的项目可以在服务器上正常运行,我们需要在web中的web.xml文件中进行文件地址配置

在这里进行一点知识补充
xml 属于标记语言 html是标记内容指导内容如何显示 xml可扩展标记语言 用来保存数据 保存配置文件数据 web.xml 是web程序的配置文件,保存各种配置信息,在服务器启动时,由服务器读取
最后我们利用我们事先写好的超简易前端界面进行测试

最后测试结果如下

Servlet的生命周期

|