手工在tomcat目錄中建立個人項目
先下載安裝好tomcat。
tomcat下的目錄如下:
在apache-tomcat-xxxx\webapps\新建自己的目錄,如demo01,
則demo01中也需要WEB-INFO文件夾,classes目錄、lib目錄。(手動創建即可)
web.xml文件: 存放在WEB-INFO目錄下。、
classes目錄: 存放字節碼文件。(創建個空的就行。)
lib目錄: 存放運行時需要的一些jar包。(沒有特殊需求,創建個空的就行)
註意: apache-tomcat-xxxx目錄下的lib目錄存放的jar文件,tomcat下所有項目共享,而demo01中的lib目錄中的jar,只能demo01使用。這是局部和整體的關系。
在demo01目錄下,創建index.jsp文件,默認會訪問這個名字的文件。比如如下:
<html> <head> <title>ppp</title> </head> <body> kkkkdd <% out.print("hello world!"); %> </body> </html>
開啟tomcat。
在瀏覽器中輸入:http://localhost:8080/demo01/
得到如下結果:
若在瀏覽器中輸入:localhost:8080
註意:
在ie中直接這樣輸入會進入搜索引擎,必須完整輸入:http://localhost:8080
在chrome,它會自動加上http://,可以進入tomcat首頁,也就是默認進入apache-tomcat-xxx\webapps\ROOT,打開默認的index.jsp。
如果不想默認打開index.jsp,打算打開其他頁面的話,可以如下設置:
修改對應項目的web.xml文件。
如輸入localhost:8080,則要修改ROOT目錄下的WEB-INFO下的web.xml文件;其他項目修改它的項目目錄下WEB-INFO目錄下的web.xml即可。
修改方法為,在web.xml中增加如下內容:(按照自己的需求稍作修改即可)
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.xhtml</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
//這段代碼的意思是,設置進入這個目錄的默認初始頁面,有index.html就打開,沒有就往下找。
虛擬路徑:
在瀏覽器中輸入:http://localhost:8080,它實際打開的是服務器中 webapps\ROOT,也就是說進入服務器根目錄時,自動轉入webapps,因為沒有指定項目名,所以默認訪問ROOT項目。這是虛擬路徑的例子。
假如我們輸入:http://localhost:8080/demo01/,也就是說進入demo01項目,而demo01項目沒在webapps目錄下,我們可以利用虛擬路徑技術,自動轉向其他路徑。
我們可以修改配置文件,訪問在webapps以外的目錄中的web項目。
方法一:(需要重啟tomcat)
conf/server.xml中配置
host標簽中,插入這段代碼:
<Context docBase="D:\tomcat\apache-tomcat-7.0.56\demo01" path="/demo01" /> //從/demo01轉入D:\tomcat\apache-tomcat-7.0.56\demo01。
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> <Context docBase="D:\tomcat\apache-tomcat-7.0.56\demo01" path="/demo01" />
方法二:(不需要重啟tomcat)
apache-tomcat-xxx\conf\Catalina\localhost
中新建文件: “項目名.xml”,
並新增一行:
<Context docBase="D:\tomcat\apache-tomcat-7.0.56\demo01" path="/demo01" />
手工在tomcat目錄中建立個人項目