Java 网络编程----初探Servlet

打印 上一主题 下一主题

主题 882|帖子 882|积分 2646

Jave Web是java面向web开辟的相关技术,他是相关技术的统称,并不是指某一个单一的技术。
在我之前的博客中(Java网络编程----通过实现浅易聊天工具来聊聊BIO模子 https://www.cnblogs.com/jilodream/p/17405923.htm),就已经写到过java可以作为一个服务器(如TCP/UDP),接收外部的请求。如利用TCP监听端口,然后直接用web页面请求该端口,那么服务器就会接收到相关的相应,接着做好业务处理,返回相应的请求即可。但是整个的业务流程太繁琐了。我们不但要处理业务流程,还要控制请求会话,还要控制各种业务分支的处理,显然这不是我们想要的。
于是聪明的开辟者很快想到了-----解耦,业务人员只要编写相关的业务即可,不必要关心繁琐的网络细节处理,因此就诞生了servlet。开辟人员只要实现servlet,而servlet和差别的路径绑定。web请求后,由体系直接转发到各自的Servlet,并由Servlet来处理相关业务即可。
那什么是servlet呢?servlet 是Server Applet(服务器应用步伐)的简写,从名字我们就可以看出它是专门用来编写服务器端的应用步伐。我们通常用它来处理服务器中http请求的处理和相应。
它是一项很古老的技术,随java诞生之初就已经问世,许多人乃至都不知Servlet是做什么。那么问题来了,我们为什么还要学习和掌握Servlet呢?这主要是由于Servlet是javaEE的紧张组成,是java web开辟的紧张基石。我们现在项目中常用到的Jsp、Springmvc、Springboot等框架,在处理网络请求的核心技术,仍然是Servlet。我们虽然不必要再继续直面Servlet或更底层的技术举行开辟,但是Servlet毕竟是什么样的,如何执行,以及再新技术中承担什么样的角色,这个却是我们想要熟悉底层原理所必须要掌握的。
话不多说,想要利用Servlet,我们必要做两步:
1、编写Servlet相关业务代码
2、将业务代码打包放置在Tomcat中,由Tomcat来加载这些Servlet
第一步,试着来编写一个Servlet
我们首先通过IDEA 新建一个web项目,此处我们选择采用maven部署,搭建好之后项目整体的结构就如下面的文件层级树一样了
  1. E:.
  2. ├─.idea
  3. ├─.smarttomcat
  4. │  └─PureJaveServlet
  5. │      └─conf
  6. └─src
  7.     └─main
  8.         ├─java
  9.         │  └─org
  10.         │      └─example
  11.         ├─resources
  12.         └─webapp
复制代码
其中:
resources 一般是我们填写的资源信息,如图片,业务设置文件等,
webapp则会放置html、css等渲染文件,还会放一些web组件(Servlet、Filter)的设置信息。我们这里只要知道作用即可。
src/main/java则是我们的业务代码。值得注意的是,我们要引入Servlet网络编程的相关依赖包,才能举行相关的web开辟。默认的java SE是不包含这些开辟包的。
在Servlet4.0之前,我们只利用javax相关的包,而且未来对接的是Tomcat 9.x及以下版本。(防盗连接:本文首发自http://www.cnblogs.com/jilodream/ )
在Servlet5.0之后,我们只利用javax相关的包,而且未来对接的是Tomcat 10.x及以后版本。
这里我们利用高版原来学习,即jakarta版本,未来tomcat也必要利用高版本。
接着编写POM文件:
  1. 1 <?xml version="1.0" encoding="UTF-8"?>
  2. 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. 5 <modelVersion>4.0.0</modelVersion>
  6. 6
  7. 7 <groupId>org.example</groupId>
  8. 8 <artifactId>PureJaveServlet</artifactId>
  9. 9 <version>1.0-SNAPSHOT</version>
  10. 10 <name>PureJaveServlet</name>
  11. 11 <packaging>war</packaging>
  12. 12
  13. 13 <properties>
  14. 14     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. 15     <maven.compiler.target>11</maven.compiler.target>
  16. 16     <maven.compiler.source>11</maven.compiler.source>
  17. 17 </properties>
  18. 18
  19. 19 <dependencies>
  20. 20     <dependency>
  21. 21         <groupId>jakarta.servlet</groupId>
  22. 22         <artifactId>jakarta.servlet-api</artifactId>
  23. 23         <version>5.0.0</version>
  24. 24         <scope>provided</scope>
  25. 25     </dependency>
  26. 26
  27. 27     <dependency>
  28. 28         <groupId>org.projectlombok</groupId>
  29. 29         <artifactId>lombok</artifactId>
  30. 30         <version>1.18.30</version>
  31. 31     </dependency>
  32. 32     <dependency>
  33. 33         <groupId>org.apache.commons</groupId>
  34. 34         <artifactId>commons-lang3</artifactId>
  35. 35         <version>3.13.0</version>
  36. 36     </dependency>
  37. 37     
  38. 38     
  39. 39     <dependency>
  40. 40         <groupId>com.alibaba</groupId>
  41. 41         <artifactId>fastjson</artifactId>
  42. 42         <version>1.2.83</version>
  43. 43     </dependency>
  44. 44
  45. 45
  46. 46     <dependency>
  47. 47         <groupId>com.fasterxml.jackson.core</groupId>
  48. 48         <artifactId>jackson-databind</artifactId>
  49. 49         <version>2.10.0</version>
  50. 50     </dependency>
  51. 51     <dependency>
  52. 52         <groupId>io.pebbletemplates</groupId>
  53. 53         <artifactId>pebble</artifactId>
  54. 54         <version>3.1.6</version>
  55. 55     </dependency>
  56. 56     <dependency>
  57. 57         <groupId>org.apache.maven.plugins</groupId>
  58. 58         <artifactId>maven-compiler-plugin</artifactId>
  59. 59         <version>3.10.1</version>
  60. 60     </dependency>
  61. 61
  62. 62 </dependencies>
  63. 63
  64. 64 <build>
  65. 65     <plugins>
  66. 66         <plugin>
  67. 67             <groupId>org.apache.maven.plugins</groupId>
  68. 68             <artifactId>maven-war-plugin</artifactId>
  69. 69             <version>3.3.2</version>
  70. 70         </plugin>
  71. 71         <plugin>
  72. 72             <groupId>org.apache.maven.plugins</groupId>
  73. 73             <artifactId>maven-compiler-plugin</artifactId>
  74. 74             <configuration>
  75. 75                 <compilerArgs>
  76. 76                     <arg>-parameters</arg>
  77. 77                 </compilerArgs>
  78. 78             </configuration>
  79. 79         </plugin>
  80. 80     </plugins>
  81. 81 </build>
  82. 82 </project>
复制代码
 
Servlet类
  1. 1 package org.example;
  2. 2
  3. 3
  4. 4 import jakarta.servlet.ServletException;
  5. 5 import jakarta.servlet.annotation.WebServlet;
  6. 6 import jakarta.servlet.http.HttpServlet;
  7. 7 import jakarta.servlet.http.HttpServletRequest;
  8. 8 import jakarta.servlet.http.HttpServletResponse;
  9. 9
  10. 10 import java.io.IOException;
  11. 11 import java.io.PrintWriter;
  12. 12
  13. 13
  14. 14 @WebServlet(urlPatterns = "/nihao")
  15. 15 public class HiServlet extends HttpServlet {
  16. 16     @Override
  17. 17     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
  18. 18         String name = req.getParameter("name");
  19. 19         resp.setContentType("text/html");
  20. 20         PrintWriter out = resp.getWriter();
  21. 21         out.println("<html><body>");
  22. 22         out.println(String.format("<h1>Hello, %s </h1>", name));
  23. 23         out.println("</body></html>");
  24. 24         out.flush();
  25. 25     }
  26. 26 }
复制代码
 
  1. 1 package org.example;
  2. 2
  3. 3 import jakarta.servlet.annotation.WebServlet;
  4. 4 import jakarta.servlet.http.HttpServlet;
  5. 5 import jakarta.servlet.http.HttpServletRequest;
  6. 6 import jakarta.servlet.http.HttpServletResponse;
  7. 7
  8. 8 import java.io.IOException;
  9. 9 import java.io.PrintWriter;
  10. 10
  11. 11 /**
  12. 12  * @discription
  13. 13  */
  14. 14 @WebServlet(urlPatterns = "/bye")
  15. 15 public class ByeServlet extends HttpServlet {
  16. 16     @Override
  17. 17     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  18. 18         String name = req.getParameter("name");
  19. 19         resp.setContentType("text/html");
  20. 20         PrintWriter out = resp.getWriter();
  21. 21         out.println("<html><body>");
  22. 22         out.println(String.format("<h1>bye bye, %s </h1>", name));
  23. 23         out.println("</body></html>");
  24. 24         out.flush();
  25. 25     }
  26. 26 }
复制代码
代码部分就竣事了,我们观察代码可以发现两点:
1、所有的Sevlet都要继续自HttpServlet。HttpServlet是一个抽象类,我们必要复写抽象类中的抽象方法,以保证未来Tomcat等web服务器在加载Servlet时,可以按照统一的规范查找,执行。
我们在Servlet中重写了doGet() 方法,体现处理该servlet路径下的get请求,同理还可以重写doPost doDelete doPut等方法,来处理对应的请求类型。
这里我们很简单,直接返回一段相应的html。
2、我们并没有写main方法,而是在Pom中标记我们的工程必要打包成一个war包。
第二步,设置tomcat
没有main方法,我们如何启动我们的java步伐呢?我们通常是将其设置到tomcat的指定路径中,启动tomcat后,tomcat会加载war包中servlet的相关类,举行处理。
因此我们会将tomcat如许的web服务器称之为Servlet容器。
我们首先从tomcat官网上(https://tomcat.apache.org/whichversion.html)下载一个与我们对应的servlet版本匹配的tomcat版本。
下载到当地之后解压即可。
接着我们为了方便在IDEA中下载一个smart tomcat的组件,将该组件关联好servlet代码和tomcat服务器即可。
关键设置如下:
Tomcat server: 选择我们下载好的tomcat服务器,如果没有下拉选项就选择"Congure..."手动加一下。
Deployment dirctory:部署文件夹,(防盗连接:本文首发自http://www.cnblogs.com/jilodream/ )该设置指向前文项目结构树种的webapp。
Use classpath of module:选择当前项目
Context path:选择上下文路径(实在就是url的前缀路径),按照url规则随便填,我这里叫填的是/biubiubiu
server port:Servlet的服务器端口,默认填8080
admin port:tomcat的管理端口   默认填8005,一般就是用于停掉tomcat(实在一般也不用)。
之后我们通过IDEA:拉起tomcat,加载servlet相关类和资源。
下令行输入如下:
  1. ....<br>15-Nov-2024 10:34:38.099 信息 [main] org.apache.coyote.AbstractProtocol.start 开始协议处理句柄["http-nio-8080"]
  2. 15-Nov-2024 10:34:39.831 信息 [main] org.apache.catalina.startup.Catalina.start [4858]毫秒后服务器启动
  3. http://localhost:8080/biubiubiu
复制代码
执行结果如下:

有人会觉得通过下载并设置tomcat有点贫苦,我们如果想debug代码的话,就更贫苦了,有没有简单点的办法:
实在除了下载tomcat,我们还可以通过代码的形式直接拉起tomcat。思路如下:
首先通过maven加载对应tomcat依赖,然后在main方法中创建tomcat实例,而且指定tomcat所必要的设置信息,如资源和class路径。然后通过start()方法启动tomcat实例就可以了。
代码如下:
新搭建一个java web项目,Servlet类和工程结构我们保持不变还是和原来一样
POM文件
  1. 1 <?xml version="1.0" encoding="UTF-8"?>
  2. 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. 5     <modelVersion>4.0.0</modelVersion>
  6. 6
  7. 7     <groupId>com.example</groupId>
  8. 8     <artifactId>demotom</artifactId>
  9. 9     <version>1.0-SNAPSHOT</version>
  10. 10     <name>demotom</name>
  11. 11     <packaging>war</packaging>
  12. 12
  13. 13     <properties>
  14. 14         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. 15         <maven.compiler.target>11</maven.compiler.target>
  16. 16         <maven.compiler.source>11</maven.compiler.source>
  17. 17         <tomcat.version>10.0.0</tomcat.version>
  18. 18     </properties>
  19. 19
  20. 20     <dependencies>
  21. 21
  22. 22
  23. 23         <dependency>
  24. 24             <groupId>org.apache.tomcat.embed</groupId>
  25. 25             <artifactId>tomcat-embed-core</artifactId>
  26. 26             <version>${tomcat.version}</version>
  27. 27             <scope>provided</scope>
  28. 28         </dependency>
  29. 29         <dependency>
  30. 30             <groupId>org.apache.tomcat.embed</groupId>
  31. 31             <artifactId>tomcat-embed-jasper</artifactId>
  32. 32             <version>${tomcat.version}</version>
  33. 33             <scope>provided</scope>
  34. 34         </dependency>
  35. 35     </dependencies>
  36. 36
  37. 37     <build>
  38. 38         <plugins>
  39. 39             <plugin>
  40. 40                 <groupId>org.apache.maven.plugins</groupId>
  41. 41                 <artifactId>maven-war-plugin</artifactId>
  42. 42                 <version>3.3.2</version>
  43. 43             </plugin>
  44. 44         </plugins>
  45. 45     </build>
  46. 46 </project>
复制代码
主类:
  1. 1 package com.example.demotom;
  2. 2
  3. 3 import org.apache.catalina.Context;
  4. 4 import org.apache.catalina.LifecycleException;
  5. 5 import org.apache.catalina.WebResourceRoot;
  6. 6 import org.apache.catalina.startup.Tomcat;
  7. 7 import org.apache.catalina.webresources.DirResourceSet;
  8. 8 import org.apache.catalina.webresources.StandardRoot;
  9. 9
  10. 10 import java.io.File;
  11. 11
  12. 12 /**
  13. 13  * @discription
  14. 14  */
  15. 15 public class TomMain {
  16. 16     public static void main(String[] args) throws LifecycleException {
  17. 17         Tomcat tomcat = new Tomcat();
  18. 18         tomcat.setPort(Integer.getInteger("port", 8080));
  19. 19         tomcat.getConnector();
  20. 20         String docBase = new File("src/main/webapp").getAbsolutePath();
  21. 21         Context ctx = tomcat.addContext("", docBase);
  22. 22         WebResourceRoot resources = new StandardRoot(ctx);
  23. 23         String base = new File("target/classes").getAbsolutePath();
  24. 24         resources.addJarResources(new DirResourceSet(resources, "/WEB-INF/classes", base, "/"));
  25. 25         ctx.setResources(resources);
  26. 26         tomcat.start();
  27. 27         tomcat.getServer().await();
  28. 28     }
  29. 29 }
复制代码
启动后控制台输出如下,我们可以看到8080端口已经被监听:
  1. "C:\Program Files\Java\jdk-11\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:51854,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath ....
  2. Connected to the target VM, address: '127.0.0.1:51854', transport: 'socket'
  3. 11月 15, 2024 10:47:54 上午 org.apache.coyote.AbstractProtocol init
  4. 信息: Initializing ProtocolHandler ["http-nio-8080"]
  5. 11月 15, 2024 10:47:54 上午 org.apache.catalina.core.StandardService startInternal
  6. 信息: Starting service [Tomcat]
  7. 11月 15, 2024 10:47:54 上午 org.apache.catalina.core.StandardEngine startInternal
  8. 信息: Starting Servlet engine: [Apache Tomcat/10.0.0]
  9. 11月 15, 2024 10:47:56 上午 org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
  10. 警告: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [1,753] milliseconds.
  11. 11月 15, 2024 10:47:56 上午 org.apache.coyote.AbstractProtocol start
  12. 信息: Starting ProtocolHandler ["http-nio-8080"]
复制代码
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

万有斥力

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表