1. Servlet
1.1 Servlet简介
Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,具有独立于平台和协议的特性,主要功能在于交互式地浏览和生成数据,生成动态Web内容。
- 把实现了Sun公司开发的Servlet接口的java程序叫做Servlet
1.2 第一个Servlet程序
- 构建一个普通的Maven项目(可以删除src目录,方便都会创建子项目)(具体如何构建看我上一篇文章)
- Maven添加jsp,servlet依赖--
- 创建一个子模块(Maven的web项目)
- 父项目中(pom.xml):
- <modules>
- <module>SonMaven</module>
- </modules>
复制代码
- 子项目中(pom.xml):
- <parent>
- <artifactId>javaweb</artifactId>
- <groupId>org.example</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
复制代码
- 子项目可以直接使用父项目的jar包,就是java的exntend
- 给子项目中的webapp/WEN-INF/web.xml换成最新的,否则以后可能某些东西无法创建
可以参考如:- ```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"
- metadata-complete="true">
-
- </web-app>
- ```
复制代码
- 最好在main中创建一个java 一个resources文件夹符合maven规范以后有用

- java文件夹中创建servlet文件
- 创建一个普通java类
- 实现servlet接口,我们一般直接通过继承HttpServlet实现
- servlet接口的另外一个实现类是GenericServlet,实际上是GenericServlet实现了Servlet,而HttpServlet继承GenericServlet
- servlet接口有一些方法,GenericServlet相对几乎没变,而HttpServle则多了许多方法其中doGet和doPost方法比较重要
- void init(ServletConfig var1) throws ServletException;
- ServletConfig getServletConfig();
- void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
- String getServletInfo();
- void destroy();
复制代码 - 重写HttpServlet的doGet和doPost方法(当然其他方法也可以但是目前用不到)
- public class MyFirstServlet extends HttpServlet {
- // 由于get和post这是请求实现的不同方式,可以相互调用逻辑都一样
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- // 自己写的
- PrintWriter writer = resp.getWriter();//响应流
- writer.print("Hello,Serlvlet");
- }
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- super.doPost(req, resp);
- }
- }
复制代码
- 编写Servlet的映射
为什么需要映射:我们写的是Java程序但是需要通过浏览器访问,而浏览器需要连接web服务器,使用需要再web服务中注册我们写的Servlet,还需要一个浏览器访问路径
配置到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"
- metadata-complete="true">
- <servlet>
- <servlet-name>hello</servlet-name>
- <servlet-class>MyFirstServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>hello</servlet-name>
- <url-pattern>/hello</url-pattern>
- </servlet-mapping>
- </web-app>
复制代码 - 配置Tomcat
之前写过,不再赘述,只是强调一个出现的问题:tomcat部署时没有自己需要部署的项目:
中deployment点击加号没有出现artifact
Tomcat是一个开源的Java Servlet容器,用于运行JavaWeb应用程序。在部署应用程序之前,需要在Tomcat中进行相关的配置。 部署应用程序的最常见方式是将应用程序打成.war包。
通过这篇文章解决大概之后刷新maven后,成功出现

- 运行测试:
这个页面出自index.jsp是主项目默认页面,如果没有servlet覆盖主路径,默认主路径显示这个。我们后面细说

这是我们在web.xml 中配置的浏览器访问路径

1.3 Servlet原理
- Servlet是由web服务器调用,当Web服务器接收到一个浏览器的请求时,它会先判断请求内容——如果是静态网页数据,Web服务器将会自行处理,然后产生响应信息;如果牵涉到动态数据,Web服务器会将请求转交给Servlet容器。此时Servlet容器会找到对应的处理该请求的Servlet实例来处理,结果会送回Web服务器,再由Web服务器传回用户端。
针对同一个Servlet,Servlet容器会在第一次收到http请求时建立一个Servlet实例,然后启动一个线程。第二次收到http请求时,Servlet容器无须建立相同的Servlet实例,而是启动第二个线程来服务客户端请求。所以多线程方式不但可以提高Web应用程序的执行效率,也可以降低Web服务器的系统负担。
参考时序图:
- Web Client 向Servlet容器(Tomcat)发出Http请求;
- Servlet容器接收Web Client的请求;
- Servlet容器创建一个HttpRequest对象,将Web Client请求的信息封装到这个对象中;
- Servlet容器创建一个HttpResponse对象;
- Servlet容器调用HttpServlet对象的service方法,
- HttpRequest对象与HttpResponse对象作为参数传给 HttpServlet对象;
- HttpServlet调用HttpRequest对象的有关方法,获取Http请求信息;
- HttpServlet调用HttpResponse对象的有关方法,生成响应数据;
- Servlet容器把HttpServlet的响应结果传给Web Client;
上述来自该文章
链接:https://www.jianshu.com/p/7dcd2c689729
1.4 Mapping问题
一个servlet可以指定一个、多个、通用的路径:- <servlet-mapping>
- <servlet-name>hello</servlet-name>
- //这是一个路径,多个就是多粘贴几个<servlet-mapping>每个给的路径不同
- <url-pattern>/hello</url-pattern>
- //通用: /hello/后面使用的所有路径都到这个servlet
- //<url-pattern>/hello/*</url-pattern>
- //后面这个的意思是以.aaa结尾的都可以
- //<url-pattern>*.aaa</url-pattern>
- </servlet-mapping>
复制代码 另外当出现两个servlet路径重叠的情况,路径范围小的优先级高
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |