IDEA简单配置maven、tomcat
配置maven,
1、选择Create New Project
2、Maven->Project SDK->Create from archetype->(右键选中)maven-archetype-webapp->next
3、GroupId(一般为域名的反写)->ArtifactId->next
4、Maven home directory(设置maven所在目录)->User setting file(设置setting位置,在maven/conf/setting.xml)->Local repository(本地库)->next
5、直接Finish
6、点击Import Changes
配置tomcat
1、File(左上角)->Settings->Build,Execution,Deployment->Application Servers-> +(添加tomcat) -> Tomcat Server->配置Tomcat路径
2、View->Tool bar(在导航上显示工具)
Add Configuration-> + (不选Templates) -> Tomcat Server ->Local ->配置
3、打开Deployment-> + -> Artifacts -> xxx:war exploded -> Ok -> Application context: /xx(项目名称) -> Ok
4、File->Project Structure->Modules
5、在src(右键 new Folder)下新建test目录,在test目录下新建java目录和resources目录,在main目录下新建java目录和resources目录。
6、选中main下面java目录,然后点击Mark as后面的Sources,选中main下面的resources目录,点击Mark as后面的Rresources,选中test目录,点击Mark as后面的Tests。
7、打开Dependencies
8.选中右边的+ -> Libraries -> Tomcat xxx -> Add Selected -> OK
即完成tomcat配置
9.测试(编写一个Servlet)
目录结构如下:
Student代码:
package com.maven.servlet;
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") //一定要加“/”,之前忘记加/,tomcat启动一直失败,不知道错误在哪,浪费很长时间才找到是忘记加 "/"
public class Student extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().print("success");
}
}
开启Tomcat