使用idea创建web项目

一、创建web项目

1、打开idea软件,点击界面上的Create New Project

使用idea创建web项目

2、进入如下界面。选中 java Enterprise,配置jdk,tomcat,勾选Web Application案例,注意勾选生成web.xml文件

使用idea创建web项目

使用idea创建web项目

使用idea创建web项目

3、指定项目的名称及项目文件的保存地址

使用idea创建web项目

4、创建成功

使用idea创建web项目

5、创建class文件和lib文件夹   点击项目的WEF-INF文件夹 ,右键,New → Directory 创建两个文件夹,classes(用来存放编译后输出的class文件) 和 lib(用于存放第三方jar包)

使用idea创建web项目

使用idea创建web项目

6、配置classes文件夹路径    File → 选择 Project Structure → 选择 Module → 选择Paths → 选择 “Use module compile output path” -> 将Output path和Test output path都选择刚刚创建的classes文件夹。

使用idea创建web项目

使用idea创建web项目

7、配置lib文件夹路径   点击Paths旁边的Dependencies 点击右边的”+”号 → 选择”Jars or Directories” -> 将Output path和Test output path都选择刚刚创建的classes文件夹 → 选择”Jar Directory” 然后一路OK就行了.

使用idea创建web项目

选择刚刚创建的lib文件夹

使用idea创建web项目

使用idea创建web项目

二、tomcat项目部署

1.配置tomcat   点击Run ,选择Edit Configurations.

使用idea创建web项目

使用idea创建web项目

点击右上角的三角形,运行

使用idea创建web项目

使用idea创建web项目

三、创建servlet

  src–>New–>Create New Servlet

使用idea创建web项目

使用idea创建web项目

servlet中简单代码

/**
 * 通过urlPatterns指定映射地址或者在web.xml文件中配置
 */
@WebServlet(name = "helloServlet",urlPatterns = {"/hello"})
public class HelloServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("hello serlvet");
        request.getRequestDispatcher("/index.jsp").forward(request,response);
    }
}

3.测试   运行tomcat访问测试:http://localhost:8080/hello

使用idea创建web项目

使用idea创建web项目

访问成功~到此idea中创建web项目搞定