使用idea创建web项目
一直使用的是eclipse,有一个项目开发用的是idea,我也尝试着熟悉一下idea,先来创建一个web项目吧
1、idea下载安装使用
官方下载地址:https://www.jetbrains.com/idea/download/#section=windows
有两个版本,Ultimate和Community,我看介绍说Community版只能做基础的java开发和android开发,貌似不能开发web项目,果断下载Ultimate版本,我下载的版本是IntelliJ IDEA 2018.3.5版本
Ultimate版本只能免费使用30天,需要注册,我找了一个激活码,至少我写这篇文章的时候(2019-3-22),是可以用的,注册后有效期到2020-1-8号,先用着:
N757JE0KCT-eyJsaWNlbnNlSWQiOiJONzU3SkUwS0NUIiwibGljZW5zZWVOYW1lIjoid3UgYW5qdW4iLCJhc3NpZ25lZU5hbWUiOiIiLCJhc3NpZ25lZUVtYWlsIjoiIiwibGljZW5zZVJlc3RyaWN0aW9uIjoiRm9yIGVkdWNhdGlvbmFsIHVzZSBvbmx5IiwiY2hlY2tDb25jdXJyZW50VXNlIjpmYWxzZSwicHJvZHVjdHMiOlt7ImNvZGUiOiJJSSIsInBhaWRVcFRvIjoiMjAyMC0wMS0wNyJ9LHsiY29kZSI6IkFDIiwicGFpZFVwVG8iOiIyMDIwLTAxLTA3In0seyJjb2RlIjoiRFBOIiwicGFpZFVwVG8iOiIyMDIwLTAxLTA3In0seyJjb2RlIjoiUFMiLCJwYWlkVXBUbyI6IjIwMjAtMDEtMDcifSx7ImNvZGUiOiJHTyIsInBhaWRVcFRvIjoiMjAyMC0wMS0wNyJ9LHsiY29kZSI6IkRNIiwicGFpZFVwVG8iOiIyMDIwLTAxLTA3In0seyJjb2RlIjoiQ0wiLCJwYWlkVXBUbyI6IjIwMjAtMDEtMDcifSx7ImNvZGUiOiJSUzAiLCJwYWlkVXBUbyI6IjIwMjAtMDEtMDcifSx7ImNvZGUiOiJSQyIsInBhaWRVcFRvIjoiMjAyMC0wMS0wNyJ9LHsiY29kZSI6IlJEIiwicGFpZFVwVG8iOiIyMDIwLTAxLTA3In0seyJjb2RlIjoiUEMiLCJwYWlkVXBUbyI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fNiyJgaZ5sX7i8tmStEpLbPwvHcByuf59qJhV/bZOl8KqJBETCDJcY6O2aqhTUy+9x93ThKs1GKrRPePrWPluud7ttlgtRveit/pcBrnQcXOl1rHq7ByB8CFAxNotRUYL9IF5n3wJOgkPojMy6jetQA5Ogc8Sm7RG6vg1yow==
2、创建web项目
2.1 打开idea后,首先是Create New Project,我看网上别人有些例子是创建的Empty Project,其实没必要,创建Empty Project后续的操作很繁琐,我这里使用一个相对简单的类型
创建Java Enterprise:
Java EE version版本我选的是Java EE 6,这样对应的Servlet是3.0版本,如果使用默认的Java EE 8,那么对应的Servlet版本只能选4.0,Servlet 4.0在我的tomcat 7.0上,应该是不支持的。
勾选的话,只勾选一个Web Application,就可以了。
输入项目名称,Finish
2.2 展开左侧资源树,web/WEB-INF下创建两个文件夹:classes和lib
右键项目,Open Module Settings
Modules->Paths,修改Output path和Test Output path的值,指向刚才创建的classes文件夹:
Libraries,点击中间的加号,选择第一项java,目录选到刚才创建的lib文件夹,文件夹选好后,会让你选择这个文件夹的类型,选择第3项:Jar Directory
回到idea主界面,右上角,配置tomcat,进入Edit Configurations,默认的发布路径为demo1_war_exploded,有点长,改成demo1:
回到idea主界面,右上角,点击Debug按钮,系统默认在web目录下创建了个index.jsp,启动后,就可以访问到这个页面了,如果成功启动后,效果应该是这样的:
到此为止,web项目就创建成功,并且可以正常访问了
3、创建Servlet
下面我们再来创建一个Servlet,src目录下,先创建一个Package,然后在该Package下创建一个Servlet:
默认勾上了Create Java EE 6 annotated class这个选项,表示是以注解的方式注册Servlet,然后修改doGet方法,doGet方法中添加如下内容:
PrintWriter out = response.getWriter();
out.print("hello world");
默认系统给添加的注解是这样的:
@WebServlet(name = "Servlet1")
如果之前没有使用过idea创建过Servlet,也没有注意这个注解,八成是要被坑的(算我一个),默认没有添加value属性,会导致这个Servlet访问不了,修改这个注解,改成:
@WebServlet(name = "Servlet1",value = "/Servlet1")
Servlet最终的代码是这样的:
package com.demo1.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; import java.io.PrintWriter; @WebServlet(name = "Servlet1",value = "/Servlet1") public class Servlet1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.print("hello world"); } }
然后重启tomcat,就可以访问到这个Servlet了,最终效果如下: