1、新建项目
2、填写项目名称
3、项目创建成功后的目录初始结构
4、配置
(1)在WEB-INF目录下创建classes和lib目录
classes :用于存放编译后的class
lib :用于存放依赖包
(2)File->Project structure配置compiler output(也可以用Ctrl+Alt+Shift+S快捷键打开Project structure)
(3)接着配置依赖包
到现在基本都配置好了
5、编写一个Servlet
(1)创建Servlet类
填写相应的信息
(2)编写代码
代码如下:
package com.brilliantZC;
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(name = "ServletText",urlPatterns = "/servletdemo")
public class ServletText extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("This is a simple servlet!");
}
}
(3)启动Tomcat
(4)访问http://localhost:8080/servletdemo