1. 程式人生 > >Eclipse建立web專案通過Tomcat執行過程中出現的一些問題

Eclipse建立web專案通過Tomcat執行過程中出現的一些問題

使用Eclipse建立jsp web工程時,需要注意幾個方面:

Tomcat啟動

首先注意服務是否啟動,如果服務配置的是Tomcat,要確認在Server對話方塊中啟動Tomcat是否成功。如果不成功,則需要再進一步核查Tomcat是否可通過自身的啟動檔案 Startup.bat 啟動成功。如果Tomcat自身能夠啟動成功而Eclipse無法啟動Server,則是Eclipse配置出了問題。

Tomcat類庫載入

其次,需要注意在web工程中是否載入了Tomcat相關類庫。如果未在對應工程中載入,則會出現“The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path” 錯誤

對應此類問題,解決方法為如下:

1,在Package Explorer 對應工程檔案中點選右鍵,選擇 Build Path- Configure Build Path選單,開啟Property對話方塊

2,在配置對話方塊中,選擇Library tab頁面,點選“Add Library” 按鈕,開啟“Add Library”對話方塊

3,在“Add Library”對話方塊中選擇 “Server Runtime” 點選 “Next” 按鈕,選擇對應的“Tomcat v7.0 Server” runtime,點選“Finish” 按鈕完成 Tomcat 類庫的新增。

4,新增完成後,相關錯誤不存在。

相關檔案位置不正確

web工程執行需要兩個基本的檔案,一個是配置檔案,一個是jsp檔案。其中配置檔名稱為固定,需為web.xml,在Package Explorer對話方塊中,檔案web.xml位置一般位於專案工程中的 WebContent\Web-INF 路徑下

jsp檔案可自定義,在Package Explorer對話方塊中,其檔案一般位於專案工程中的WebContent 路徑下,並且該檔案需在web.xml 需設定,預設情況下,web.xml 如下配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>testa</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

如果自定義的jsp檔名稱為NewFile.jsp ,則需調整 web.xml,如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>testa</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>NewFile.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

經過如此設定,確保Eclipse開發環境能夠識別NewFile.jsp,並且在啟動服務後,開啟地址http://localhost :8080/testa  (testa 為工程名稱)可獲得網頁正確結果,否則會出現類似404 錯誤,如下所示