替换web.xml为webapp4.0版本和tomcat一致
<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"
metadata-complete="true">
web-app>
[Maven仓库地址](Maven Repository: Search/Browse/Explore (mvnrepository.com))
?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"
metadata-complete="true">
<servlet>
<servlet-name>helloxiaozhaoservlet-name>
<servlet-class>com.xiaozhao.HelloXiaoZhaoservlet-class>
servlet>
<servlet-mapping>
<servlet-name>helloxiaozhaoservlet-name>
<url-pattern>/hellourl-pattern>
servlet-mapping>
web-app>
package com.xiaozhao;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author 小龚
* @create 2022-08-26 17:25
*/
public class HelloXiaoZhao extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//响应类型:html
resp.setContentType("text/html");
//设置字符类型
resp.setCharacterEncoding("utf-8");
//获取响应的输出流
PrintWriter out = resp.getWriter();
out.println("");
out.println("");
out.println("老婆,你好! ");
out.println("");
out.println("");
out.println("Hello XiaoZhao!
");
out.println("");
out.println("");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
Servlet类在Sun公司有两个默认的实现类:HttpServlet,
把实现Servlet接口的java程序叫做Servlet
导入失败需要手动导入
找到Maven仓库里的文件夹
父项目中会多一个moudle
<modules>
<module>servlet01module>
modules>
子项目中会多一个parent
<parent>
<artifactId>javaweb-02-mavenartifactId>
<groupId>com.xiaozhao02groupId>
<version>Unknownversion>
parent>
父项目中的jar包子项目可以使用
son extends parent
将web.xml改为最新版本
<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"
metadata-complete="true">
web-app>
添加源代码目录和资源目录
package com.xz.servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author 小龚
* @create 2022-08-27 1:35
*/
public class HelloServlet extends HttpServlet {
//由于get或者post只是请求实现不同的方法,可以相互调用,业务逻辑都一样
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//ServletOutputStream ot = resp.getOutputStream();
PrintWriter writer = resp.getWriter();//响应流
resp.setCharacterEncoding("utf-8");
writer.print("你好小赵!");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
为什么需要映射:
我们写的是java程序,但是通过服务器访问,而浏览器需要连接web服务器,所以我们需要在web服务器中注册我们写的Servlet,还需要给他一个浏览器访问的路径。
<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"
metadata-complete="true">
<servlet>
<servlet-name>helloxiaozhaoservlet-name>
<servlet-class>com.xz.servlet.HelloServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>helloxiaozhaoservlet-name>
<url-pattern>/helloxiaozhaourl-pattern>
servlet-mapping>
web-app>
控制台输出