本文讲述了Java教程——一文搞懂Java Servlet!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:
Java servlet是开发可以生成动态网页的Web应用程序的Java方式。Servlet位于服务器端。
什么是网络容器?
Web容器是servlet和Web服务器之间的接口。所有servlet都在Web容器内运行。在本文中,使用Apache Tomcat作为Web容器。此外,还可以使用JBoss或GlassFish等Java EE服务器来运行Java servlet,但Apache Tomcat更为轻量、更容易使用。
构建第一个Java Servlet
本文中使用的技术
Java 11
Apache Maven 3.6.3
Apache Tomcat 10.1.7
IntelliJ IDEA Community Edition(无需使用IntelliJ Idea Ultimate)
创建 Maven 项目
以批处理模式生成Maven项目。在命令行中输入以下命令:
mvn archetype:generate -B -DgroupId="com.example" -DartifactId="servlet-example-1"
可以根据需要更改groupId和artifactId。在这个示例项目中:
groupId = com.example
artifactId = servlet-example-1
项目结构
需要设置项目结构如下。
项目结构
修改POM文件
首先,从IntelliJ IDEA打开项目。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>servlet-example-1</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>servlet-example-1</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<webappDirectory>src/main/webapp</webappDirectory>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
示例项目的POM文件如上所示。接下来,讨论POM文件中的每个必需组件。
1 → 在 java servlet 中使用war(web 应用程序存档)文件。因此,我们必须将包装设置为WAR。
2 → 将Apache Maven的编码系统设置为UTF-8。这与Java servlet无关。这是为Apache Maven完成的。
3 和 4 → 将Java版本设置为 11。
5 → 为Java servlet添加所需的servlet-api依赖项。由于 Apache Tomcat具有所有必需的库,将范围设置为已提供。
6 → 使用maven-war-plugin来创建Java WAR文件。
7 → 来自内容根目录的Web应用程序目录路径。
8 → 来自内容根目录的web.xml文件路径。
Servlet App.java类源码
package com.example;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class App extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.println("Hello!");
out.close();
}
}
HttpServlet类提供了处理REST API方法(例如 GET、POST、PUT和POST)的方法。
doGet方法 → 处理GET请求的Java方法
doPost方法 → 处理POST请求的Java方法
doPut方法 → 处理PUT请求的Java方法
doDelete方法 → 处理DELETE请求的Java方法
在这个示例项目中,只使用了doGet方法。它提供了GET请求的Hello!响应。
什么是 web.xml?
web.xml文件是项目的部署描述符。此部署描述符包含有关 servlet 的信息。部署描述符告诉Web容器如何处理servlet项目中的文件。
部署描述符的内容
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.example.App</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
示例项目的Web描述符如上所示。
所有与servlet相关的标签都必须在<web-app>和</web-app>标签内使用。
<servlet>标签用于定义servlet的名称。<servlet-name>和<servlet-class>标签用于<servlet>和</servlet>标签内。<servlet-name>标签用于提供 servlet的名称。<servlet-class>标签用于为相关的servlet提供Java类。
<servlet-mapping>标签用于定义servlet的映射。<servlet-name>和<url-pattern>标签用于<servlet-mapping>和</servlet-mapping>标签内。<servlet-name>标签用于提供servlet的名称。<url-pattern>标签用于提供相关 servlet的URL模式。
部署servlet
首先,必须构建WAR文件。可以通过在终端或PowerShell中执行以下命令:
mvn clean install
此WAR文件可以部署在Apache Tomcat中。首先,必须启动Apache Tomcat。然后转到“Manager App”。在部署菜单,单击要部署的 WAR 文件部分中的选择文件按钮。然后选择WAR文件并单击部署按钮。然后,可以在Application菜单看到/servlet-example-2–1.0-SNAPSHOT。现在,可以通过向http://localhost:8080/servlet-example-2-1.0-SNAPSHOT/hello发送请求来调用servlet 。
Servlet 注释
可以用注释替换web.xml文件。
可以在web.xml中为<servlet>和<servlet-mapping>标签使用@WebServlet注解。可以使用value属性来定义servlet的URL模式。可以使用name属性来定义servlet的名称。
可以使用注释重写App.java类,如下所示。
package com.example;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(
value = "/hello",
name = "hello"
)
public class App extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.println("Hello!");
out.close();
}
}
此外,需要更改POM文件以停止搜索web.xml文件。为此,必须更改POM文件中maven-war-plugin的配置,如下所示。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
总结
本文通过示例讨论了Java Servlet的基础知识。可以创建带有web.xml文件或注释的Java servlet。可以使用Apache Tomcat来部署Java servlet。
更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。
想高效系统的学习Java编程语言,推荐大家关注一个微信公众号:Java圈子。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Java入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!