轉自:https://www.jianshu.com/p/9684e90cf7b5
Intellij idea建立javaWeb:實現JSP/Servlet
1、建立並設置javaweb工程html
1.建立javaweb工程File --> New --> Project...java
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
設置工程名字:web
![](http://static.javashuo.com/static/loading.gif)
建立完成後工程結構以下:瀏覽器
![](http://static.javashuo.com/static/loading.gif)
2. Web工程設置tomcat
2.1 在WEB-INF 目錄下點擊右鍵,New --> Directory,建立 classes 和 lib 兩個目錄jsp
![](http://static.javashuo.com/static/loading.gif)
classes目錄用於存放編譯後的class文件,lib用於存放依賴的jar包2.2 File --> Project Structure...,進入 Project Structure窗口,點擊 Modules --> 選中項目「JavaWeb」 --> 切換到 Paths 選項卡 --> 勾選 「Use module compile output path」,將 「Output path」 和 「Test output path」 都改成以前建立的classes目錄ide
![](http://static.javashuo.com/static/loading.gif)
即將後面編譯的class文件默認生成到classes目錄下2.3 點擊 Modules --> 選中項目「JavaWeb」 --> 切換到 Dependencies 選項卡 --> 點擊右邊的「+」,選擇 「JARs or directories...」,選擇建立的lib目錄測試
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
選擇Jar Directoryui
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
2.4 配置打包方式Artifacts:點擊 Artifacts選項卡,IDEA會爲該項目自動建立一個名爲「JavaWeb:war exploded」的打包方式,表示 打包成war包,而且是文件展開性的,輸出路徑爲當前項目下的 out 文件夾,保持默認便可。另外勾選下「Build on make」,表示編譯的時候就打包部署,勾選「Show content of elements」,表示顯示詳細的內容列表。this
![](http://static.javashuo.com/static/loading.gif)
3. Tomcat配置
3.1 Run -> Edit Configurations,進入「Run Configurations」窗口,點擊"+"-> Tomcat Server -> Local,建立一個新的Tomcat容器
![](http://static.javashuo.com/static/loading.gif)
3.2 在"Name"處輸入新的服務名,點擊「Application server」後面的「Configure...」,彈出Tomcat Server窗口,選擇本地安裝的Tomcat目錄 -> OK
![](http://static.javashuo.com/static/loading.gif)
3.3 在「Run Configurations」窗口的「Server」選項板中,去掉勾選「After launch」,設置「HTTP port」和「JMX port」,點擊 Apply -> OK,至此Tomcat配置完成。
![](http://static.javashuo.com/static/loading.gif)
4. JavaWeb測試4.1 Run -> Edit Configurations,進入「Run Configurations」窗口,選擇以前配置好的Tomcat,點擊「Deployment」選項卡,點擊「+」 -> 「Artifact」-> 選擇建立的web項目的Artifact...修改「Application context」-> Apply -> OK
![](http://static.javashuo.com/static/loading.gif)
說明:此處的Application context是指定本工程的根目錄4.2 在index.jsp文件中的body之間添加要顯示的內容,而後點擊「運行」的綠色三角
![](http://static.javashuo.com/static/loading.gif)
打開瀏覽器,輸入:localhost:8080/JavaWeb
![](http://static.javashuo.com/static/loading.gif)
至此,intellij idea建立並設置javaweb工程所有完成,下面是在其中編寫並運行Servlet。2、Servlet簡單實現1. 編寫servlet源文件**
在src目錄下新建HelloWorld.Java,並編寫一下代碼並進行編譯:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HelloWorld extends HttpServlet {
private String message;
@Overridepublicvoidinit()throwsServletException{ message ="Hello world, this message is from servlet!"; }@OverrideprotectedvoiddoGet(HttpServletRequest req, HttpServletResponse resp)throwsServletException, IOException{//設置響應內容類型resp.setContentType("text/html");//設置邏輯實現PrintWriter out = resp.getWriter(); out.println("
"+ message +"
"); }@Overridepublicvoiddestroy(){super.destroy(); }
}
編譯後會發如今classes目錄下生成了HelloWorld.class文件
![](http://static.javashuo.com/static/loading.gif)
2. 部署servlet
方法一:
在WEB-INF目錄下web.xml文件的標籤中添加以下內容:
HelloWorld
HelloWorld
HelloWorld
/HelloWorld
方法二:
在HelloWorld文件的類前面加上:@WebServlet("/HelloWorld")
3. 運行servlet點擊運行按鈕
![](http://static.javashuo.com/static/loading.gif)
控制檯出現successfully則tomcat服務啓動成功!打開瀏覽器輸入:localhost:8080/JavaWeb/HelloWorld便可查看servlet運行狀態了.