Skip to content

Commit 13de2fe

Browse files
author
cicadasmile
committed
JavaEE基础(01):Servlet实现方式,生命周期执行过程
0 parents  commit 13de2fe

8 files changed

Lines changed: 242 additions & 0 deletions

File tree

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 参考文章
2+
3+
[JavaEE基础(01):Servlet实现方式,生命周期执行过程](https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484304&idx=1&sn=dd6b6852e35031dd07f70d441f3ddc85&chksm=fdf45728ca83de3e158597030cf46b1677eccf533f9e1412690cd64b0ec0cce544711ceabddb&token=1248678182&lang=zh_CN#rd)<br/>
4+
5+
持续更新中...
6+
7+
## 项目简介
8+
9+
Java基础类型,容器,并发,IO流,面向对象,Web编程等代码总结。
10+
11+
## 关于作者
12+
【<b>公众号:知了一笑</b>】 【<b><a href="https://www.zhihu.com/people/cicadasmile/columns">知乎专栏</a></b>】<br/>
13+
<img width="255px" height="255px" src="https://avatars0.githubusercontent.com/u/50793885?s=460&v=4"/><br/>
14+
15+
## 推荐项目
16+
17+
|项目名称|GitHub地址|GitEE地址|推荐指数|
18+
|:---|:---|:---|:---|
19+
|SpringCloud微服务架构实战综合案例|[GitHub·点这里](https://github.com/cicadasmile/husky-spring-cloud)|[GitEE·点这里](https://gitee.com/cicadasmile/husky-spring-cloud)|☆☆☆☆☆|
20+
|SpringCloud微服务基础组件案例详解|[GitHub·点这里](https://github.com/cicadasmile/spring-cloud-base)|[GitEE·点这里](https://gitee.com/cicadasmile/spring-cloud-base)|☆☆☆|
21+
|SpringCloud实现分库分表模式下数据库实时扩容|[GitHub·点这里](https://github.com/cicadasmile/cloud-shard-jdbc)|[GitEE·点这里](https://gitee.com/cicadasmile/cloud-shard-jdbc)|☆☆☆☆☆|
22+
|SpringBoot框架基础应用入门到进阶|[GitHub·点这里](https://github.com/cicadasmile/spring-boot-base)|[GitEE·点这里](https://gitee.com/cicadasmile/spring-boot-base)|☆☆☆☆|
23+
|SpringBoot框架整合开发常用中间件|[GitHub·点这里](https://github.com/cicadasmile/middle-ware-parent)|[GitEE·点这里](https://gitee.com/cicadasmile/middle-ware-parent)|☆☆☆☆☆|
24+
|Spring+Mvc框架基础案例详解|[GitHub·点这里](https://github.com/cicadasmile/spring-mvc-parent)|[GitEE·点这里](https://gitee.com/cicadasmile/spring-mvc-parent)|☆☆|
25+
|Java描述常用设计模式,算法,数据结构|[GitHub·点这里](https://github.com/cicadasmile/model-arithmetic-parent)|[GitEE·点这里](https://gitee.com/cicadasmile/model-arithmetic-parent)|☆☆☆☆☆|
26+
|Linux系统基础、运维,常用操作积累|[GitHub·点这里](https://github.com/cicadasmile/linux-system-base)|[GitEE·点这里](https://gitee.com/cicadasmile/linux-system-base)|☆☆☆|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-ee-base-parent</artifactId>
7+
<groupId>com.java.ee.parent</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<groupId>com.node01.servlet</groupId>
12+
<artifactId>node01-servlet-life</artifactId>
13+
<packaging>war</packaging>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>1.8</maven.compiler.source>
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
<jstl.version>1.2</jstl.version>
20+
<servlet-api.version>2.5</servlet-api.version>
21+
<jsp-api.version>2.0</jsp-api.version>
22+
</properties>
23+
24+
<dependencies>
25+
<!-- JSP相关 依赖包-->
26+
<dependency>
27+
<groupId>jstl</groupId>
28+
<artifactId>jstl</artifactId>
29+
<version>${jstl.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>javax.servlet</groupId>
33+
<artifactId>jsp-api</artifactId>
34+
<version>${jsp-api.version}</version>
35+
<scope>provided</scope>
36+
</dependency>
37+
<!-- servlet依赖包 -->
38+
<dependency>
39+
<groupId>javax.servlet</groupId>
40+
<artifactId>servlet-api</artifactId>
41+
<version>${servlet-api.version}</version>
42+
<scope>provided</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>junit</groupId>
46+
<artifactId>junit</artifactId>
47+
<version>4.12</version>
48+
</dependency>
49+
</dependencies>
50+
51+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.node01.servlet.impl;
2+
3+
import javax.servlet.ServletException;
4+
import javax.servlet.http.HttpServlet;
5+
import javax.servlet.http.HttpServletRequest;
6+
import javax.servlet.http.HttpServletResponse;
7+
import java.io.IOException;
8+
9+
public class ServletOneImpl extends HttpServlet {
10+
@Override
11+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
12+
throws ServletException, IOException {
13+
response.setContentType("text/html;charset=utf-8");
14+
response.getWriter().print("执行:doGet");
15+
}
16+
17+
@Override
18+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
19+
throws ServletException, IOException {
20+
response.setContentType("text/html;charset=utf-8");
21+
response.getWriter().print("执行:doPost");
22+
}
23+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.node01.servlet.impl;
2+
3+
import javax.servlet.*;
4+
import javax.servlet.http.HttpServletResponse;
5+
import java.io.IOException;
6+
7+
public class ServletThreeImpl implements Servlet {
8+
9+
@Override
10+
public void init(ServletConfig servletConfig) throws ServletException {
11+
servletConfig.getServletName();
12+
System.out.println("init 被调用...");
13+
}
14+
@Override
15+
public void service(ServletRequest servletRequest, ServletResponse servletResponse)
16+
throws ServletException, IOException {
17+
System.out.println("ThreadId:"+Thread.currentThread().getId());
18+
System.out.println("service 被调用...");
19+
HttpServletResponse response = (HttpServletResponse)servletResponse ;
20+
response.getWriter().print("Servlet.Life");
21+
}
22+
// 关闭 Tomcat 服务时可以看到控制台打印
23+
@Override
24+
public void destroy() {
25+
System.out.println("destroy 被调用...");
26+
}
27+
@Override
28+
public ServletConfig getServletConfig() {
29+
System.out.println("getServletConfig 被调用...");
30+
return null;
31+
}
32+
@Override
33+
public String getServletInfo() {
34+
System.out.println("getServletInfo 被调用...");
35+
return null;
36+
}
37+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.node01.servlet.impl;
2+
3+
import javax.servlet.GenericServlet;
4+
import javax.servlet.ServletException;
5+
import javax.servlet.ServletRequest;
6+
import javax.servlet.ServletResponse;
7+
import javax.servlet.http.HttpServletResponse;
8+
import java.io.IOException;
9+
/**
10+
* Servlet实现方式二
11+
*/
12+
public class ServletTwoImpl extends GenericServlet {
13+
@Override
14+
public void service(ServletRequest servletRequest, ServletResponse servletResponse)
15+
throws ServletException, IOException {
16+
HttpServletResponse response = (HttpServletResponse)servletResponse ;
17+
response.setContentType("text/html;charset=utf-8");
18+
response.getWriter().print("执行:service");
19+
}
20+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://java.sun.com/xml/ns/javaee"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
5+
id="WebApp_ID" version="2.5">
6+
7+
<display-name>frame_servlet</display-name>
8+
<context-param>
9+
<param-name>encoding</param-name>
10+
<param-value>UTF-8</param-value>
11+
</context-param>
12+
<!-- 参数配置 -->
13+
<context-param>
14+
<param-name>my-name</param-name>
15+
<param-value>cicada</param-value>
16+
</context-param>
17+
<context-param>
18+
<param-name>time</param-name>
19+
<param-value>2019-11-13</param-value>
20+
</context-param>
21+
22+
<!-- Servlet配置 -->
23+
<!-- 请求:http://localhost:6003/servletOneImpl -->
24+
<servlet>
25+
<servlet-name>servletOneImpl</servlet-name>
26+
<servlet-class>com.node01.servlet.impl.ServletOneImpl</servlet-class>
27+
</servlet>
28+
<servlet-mapping>
29+
<servlet-name>servletOneImpl</servlet-name>
30+
<url-pattern>/servletOneImpl</url-pattern>
31+
</servlet-mapping>
32+
33+
<servlet>
34+
<servlet-name>servletTwoImpl</servlet-name>
35+
<servlet-class>com.node01.servlet.impl.ServletTwoImpl</servlet-class>
36+
</servlet>
37+
<servlet-mapping>
38+
<servlet-name>servletTwoImpl</servlet-name>
39+
<url-pattern>/servletTwoImpl</url-pattern>
40+
</servlet-mapping>
41+
42+
<servlet>
43+
<servlet-name>servletThreeImpl</servlet-name>
44+
<servlet-class>com.node01.servlet.impl.ServletThreeImpl</servlet-class>
45+
</servlet>
46+
<servlet-mapping>
47+
<servlet-name>servletThreeImpl</servlet-name>
48+
<url-pattern>/servletThreeImpl</url-pattern>
49+
</servlet-mapping>
50+
51+
</web-app>

java-se-base-parent/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-base-parent</artifactId>
7+
<groupId>com.java.parent</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<groupId>com.java.se.parent</groupId>
12+
<artifactId>java-se-base-parent</artifactId>
13+
<packaging>pom</packaging>
14+
15+
16+
</project>

pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.java.parent</groupId>
7+
<artifactId>java-base-parent</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<packaging>pom</packaging>
10+
11+
<modules>
12+
<!-- JavaSE 基础模块 -->
13+
<module>java-se-base-parent</module>
14+
<!-- JavaEE 基础模块 -->
15+
<module>java-ee-base-parent</module>
16+
</modules>
17+
18+
</project>

0 commit comments

Comments
 (0)