web項目建立教程


前提:正確安裝了tomcat,具體可以參照我的博文tomcat for mac 安裝/開啟/關閉

  1. 選擇新建項目,選擇sdk,勾選webApplication,點擊next
    在這里插入圖片描述
  2. 填寫項目名稱,點擊finish
    在這里插入圖片描述
  3. 在WEB-INF中新建兩個文件夾classes與lib,操作:new-》Directory
    在這里插入圖片描述
  4. 點擊File->Project Structure->Modules->Paths,修改Output path和Test output path的路徑,均設置為classes所在目錄路徑,點擊apply(不要點ok)
    在這里插入圖片描述
  5. 點擊Dependencies-> + ->Libraries,選擇Application Server Libraries下的Tomcat 9.0.14,點擊Add Selected,這樣就可以導入jsp和servlet的jar包了
    在這里插入圖片描述
  6. Run-》Edit Configurations,進行設置
    在這里插入圖片描述
  7. 點擊Deployment—>+—>Artifact…,設置Application context
    在這里插入圖片描述在這里插入圖片描述
  8. 在Server界面,在Application server添加本地安裝的Tomcat目錄路徑(shift+command+g打開資源庫查找Tomcat目錄)
    在這里插入圖片描述
  9. 設置端口,若是沒有1099自己手動添加
    在這里插入圖片描述
  10. 新建一個Servlet:在src文件夾下新建名為“cn.jxs.servlet”package,並在package下新建名為HelloServlet.java的文件
    在這里插入圖片描述
  11. HelloServlet.java源碼
	package cn.jxs.servlet;
	import javax.servlet.GenericServlet;
	import javax.servlet.ServletException;
	import javax.servlet.ServletRequest;
	import javax.servlet.ServletResponse;
	import java.io.IOException;
	import java.io.OutputStream;
	public class HelloServlet extends GenericServlet{
	@Override
public void service(ServletRequest servletRequest,ServletResponse servletResponse)throws ServletException,IOException{
	OutputStream out=servletResponse.getOutputStream();
	out.write("HelloServlet!!\n--byIntellijIDEA".getBytes());
	}
}
  1. 在WEB-INF下更改web.xml文件,源碼如下
    在這里插入圖片描述
  2. Web.xml源碼
	<?xmlversion="1.0"encoding="UTF-8"?>
	<web-appxmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
	version="4.0">
	
	<servlet>
	<servlet-name>HelloServlet</servlet-name>
	<servlet-class>cn.jxs.servlet.HelloServlet</servlet-class>
	<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
	<servlet-name>HelloServlet</servlet-name>
	<url-pattern></url-pattern>
	</servlet-mapping>
</web-app>
  1. 點擊綠色按鈕運行
    在這里插入圖片描述
  2. 運行成功出現
    在這里插入圖片描述
  3. 瀏覽器上彈出界面(這個顯示的文字是在HelloServlet.java源碼中)
    在這里插入圖片描述
  4. 補充說明:顯示index.jsp想要顯示的文字
    在這里插入圖片描述
    在這里插入圖片描述
  5. 補充說明:顯示HelloServlet.java源碼想要顯示的文字
    在這里插入圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM