Eclipse for Java EE軟體操作集錦(二),eclipseee

來源:互聯網
上載者:User

Eclipse for Java EE軟體操作集錦(二),eclipseee

看本文章之前請確保已經瞭解eclipse建立web工程,如果有疑問請查看本系列文章第一篇 eclipse軟體操作集錦(一)

1.我們添加一個servlet 配置一下web.xml測試一下是否能正常顯示。
:

 


servlet和web.xml檔案內容如下。

1 package com.vastsum; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class HelloServlet extends HttpServlet{11 //覆蓋doGet方法12 @Override13 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {14 // TODO Auto-generated method stub15 //向瀏覽器輸出內容16 resp.getWriter().write("test servlet");17 }18 }View Code

對應XML代碼

1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 8 <servlet> 9 <servlet-name>HelloServlet</servlet-name>10 <servlet-class>com.vastsum.HelloServlet</servlet-class>11 </servlet>12 13 <servlet-mapping>14 <servlet-name>HelloServlet</servlet-name>15 <url-pattern>/hello</url-pattern>16 </servlet-mapping>17 18 </web-app>View Code

添加完成之後,點擊運行

 

運行之後會出錯,出錯:

 

2.接下來解決一下出錯的問題:
出錯是因為產生的class檔案的目錄不對,我們看一下工作空間項目根目錄下class的目錄。

發現產生的class檔案並沒有下WEB-INF檔案夾下,而是在build檔案夾下,接下來我們要進行如下設定。
首先我們要刪除發布的MyWeb。

 

 然後設定運行時,發布到tomcat伺服器對應目錄,:

修改以上兩項參數,意思是將項目發布到tomcat伺服器裡面。

最後我們選擇項目,右鍵點擊 Build Path -> Configure Build Path ,然後:

 

 這是控制Java檔案要產生到正確的目錄裡面。

3.修改過檔案夾位置之後。再次運行web項目。
然後在瀏覽器地址欄輸入:http://localhost:8080/MyWeb/hello 可以正常訪問到網站了。

**********************************************************************
一些常見問題的解決方案
1、用Eclipse建立一個web項目沒有自動產生web.xml
建立項目點擊:

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.