tomcat部署java

java文件

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;

public class hello extends HttpServlet {

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>hello</h1>");
}
}

编译

1
javac -d . -classpath E:\javalib\apache-tomcat-10.0.8\lib\servlet-api.jar .\hello.java

E:\javalib\apache-tomcat-10.0.8 切换为自己的tomcat目录

创建文件夹

E:\javalib\apache-tomcat-10.0.8\webapps中创建

大概就是这个样子

E:\javalib\apache-tomcat-10.0.8 切换为自己的tomcat目录

hello.class 是编译生成的

web.xml如下

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>hello</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

</web-app>

最后访问http://127.0.0.1:8080/first/hello