我这里安装的是
IntelliJ IDEA 2018.3.4 x64
tomcat apache-tomcat-9.0.12
maven 3.6.0
点击createNew project
选择Maven 点击Next
GroupId(一般填公司名) 和ArtifactId 随便填写也可填写一致的名称也可,继续点击Next
点击Finish,之后稍等片刻,即可看到项目创建目录
因为在开始创建过程中没有选择用骨架创建,所以才会出来上面这种目录结构
接下来就开始创建web项目了
右键点击Maven_text目录,出现Add Framework Support,点击这个
勾选 Web Application,点击OK
点击之后就能在项目里面看到web文件夹
之后先配置好pom.xml下的web项目依赖包:
在pom.xml中添加如下代码
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> </dependency> </dependencies>
写好之后点击 下面蓝色的字 Enable Auto-Import
这个时候你就能在右边看到自动加载的Maven包了
然后在src/main/java/目录下新建一个servlet
Name任意起个test_Servlet,点击OK
编写test_Serlet.java
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.annotation.WebServlet; import java.io.IOException; import java.io.PrintWriter; @WebServlet(name="test_Servlet") public class test_Servlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置网页响应类型 response.setContentType("text/html"); //实现具体操作 PrintWriter out = response.getWriter(); out.println("This is a test servlet page"); } }
编写web.xml
<?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"> <servlet> <servlet-name>test_Servlet</servlet-name> <servlet-class>test_Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>test_Servlet</servlet-name> <url-pattern>/test_Servlet</url-pattern> </servlet-mapping> </web-app>
发现annotation包是红色的,没有导入,不用管它,配置tomcat容器
点击 Add Configuration
点击+号,找到最下面的tomcat Server 点击Local
进行配置,Name随便起
进入项目设置里
点击Moudle点击最右边的+号,点击2,添加tomcat9.0.12,点击OK
或者添加tomcat的jar包也可
最后发布运行
这个内容有点单一哈,,更改