0%

JavaWeb开发之Servlet

说明

  • 本编开始学习JavaWeb开发的内容,环境信息如下:IDEA,Maven,win10,java version "1.8.0_381"

搭建Servlet

在IDEA中新建webapp

image-20231129101815913

  • 注意maven设置为本地

image-20231129102658617

  • 在根目录新建:src\main\javasrc\main\resourcessrc\main\webapp

  • 在pom.xml中加入依赖文件

1
2
3
4
5
6
7
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
  • 手动新建src\main\webapp\WEB-INF\web.xml
1
2
3
4
5
6
<?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">
</web-app>
  • src-mian-java 新建一个包名为xyz.shi.servlet , 然后在报名下新建MyServlet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package xyz.shi.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;

@WebServlet("/hello")
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get请求");
resp.getWriter().write("hello world");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post请求");
resp.getWriter().write("你好");

}
}


  • @WebServlet()会根据某些GET请求会生效, 然后里面写的 /hello,表示当请求的url是 /hello 的请求才会生效
  • 重写了父类中的get和post方法
  • 虽然打war 复制到Tomcat的webapps里面运行是一种方法,但是这种方法比较麻烦.所以我更推荐使用Smart Tomcat

image-20231130163237555.png

  • 打开编辑配置

image-20231130163329491

  • 并选择本地tomcat的根目录,设置路径等,端口不用改

image-20231201154747879

  • 点击运行按钮

image-20231130163627514

  • 出现如下信息说明启动成功
1
2
3
30-Nov-2023 16:36:32.793 信息 [main] org.apache.coyote.AbstractProtocol.start 开始协议处理句柄["http-nio-8080"]
30-Nov-2023 16:36:32.807 信息 [main] org.apache.catalina.startup.Catalina.start [594]毫秒后服务器启动
http://localhost:8080/WebTest
  • 浏览器访问http://localhost:8080/WebTest/hello

image-20231130163758294

  • MyServlet中的WebServlet("/hello") 的注解去掉
1
2
3
4
5
6
7
8
9
10
11
12
ackage xyz.shi.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;

public class MyServlet extends HttpServlet {
@Override
....
  • 在web.xml文件下,配置Servlet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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">
<servlet>
<!--servlet命名-->
<servlet-name>MyServlet</servlet-name>
<!--servlet类的全限定名(路径+文件名)-->
<servlet-class>xyz.shi.servlet.MyServlet</servlet-class>
</servlet>
<!--定义Servlet的请求映射-->
<servlet-mapping>
<!--要映射的servlet名,与上方定义的servlet-name一致-->
<servlet-name>MyServlet</servlet-name>
<!--请求映射url,必须以/开头-->
<!--之后通过项目上下文路径+该路径,就能访问FirstServlet类-->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>

  • 重写运行,在浏览器中访问http://localhost:8080/WebTest/hello

总结

有两种运行Servlet的方式

  • 第一种是在类中直接加上WebServlet("/hello")的标识,然后启动
  • 第二种是在web.xml中进行配置,推荐第二种方式更加方便管理