开发平台: macos 16, tomcat9, idea 2021
选中maven -camel-archetype-webapp
工程如图所示
点击配置tomacat server local
注意URL我这里是http://localhosot:8080/
点击deployment 添加war exploded, 注意下面的访问路径, 之后部署好了之后就通过浏览器访问这个路径
如果报错如下, 那么就是这个deployment路径设置问题
404 The origin server did not find a current representation for the target resou
最后点击运行就会出来这个界面
如果要手动设置模块编译输出路径, 就在project structure/module/paths里面设置
现在需要添加servlet-api.jar, 打开project structure/module/dependencies ,添加jars or dir, 选中tomat安装目录下lib下的servlet-api.jar
在src/main下面添加java文件夹,输入目录后下面会弹出maven, 直接点击就可以了
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet({"/test", "/get"})
public class test extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("POST Method");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("GET Method");
}
}
在main/java下新建一个test.java, 然后直接运行, 路径是/xxx/get