Eclipse中運行Servlet出現HTTP Status *** 的若干錯誤解決

來源:互聯網
上載者:User

這兩天一直在調Eclipse中運行Servlet的簡單程式可是會出現若干種錯誤,網上查了好多,大家東拉西扯,可是多種解決辦法沒有一種適合我,經過一整天努力,終於解決了,現撰文分享,希望廣大罵農朋友受益。

首先想說我用的是Eclipse JAVA EE版本的平台,至於MyEclipse的東東可能不適用。JDK版本是1.7的,tomcat版本是6.0的(這裡想強調下本身應該是Eclipse3.x、JDK1.6、tomcat6.0搭配或Eclipse4.x、JDK1.7、tomcat7.0搭配,但我當時稀裡糊塗安裝好了,之後用tomcat7.0代替6.0版本,運行Servlet會出現HTTP Status 500的錯誤,估計是Eclipse中外掛程式與tomcat 衝突的問題,因為關於WTP的外掛程式是下自於http://blog.csdn.net/tiansong04該部落格的,其都是基於tomcat6.0版本。所以在這裡提醒廣大和我一樣的菜鳥盆友,版本搭配省去你找錯誤一半的時間,不要步我的後塵)。

先建Dynamic Web Project,其中建立3個檔案,A.jsp:

<h1>Hello World!</h1><%String person="?";    String user=request.getParameter("user");if(user!=null) person=user;%>Welcome to WTP,<%= person%>
B.jsp:
<% String error_message="";Object error=request.getAttribute("error");if(error!=null) error_message=error.toString();%><form action="HelloServlet"><table cellspacing="4"><tr><td>Enter your user name:</td><td><input name="user" type="text" size="20"></td><td style="color: red"><%= error_message %></td></tr><tr><td></td><td><input type="submit" value="Login"></td><td></td></tr></table></form>


HelloServlet.java(Servlet檔案)

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubString url="/A.jsp";String user=request.getParameter("user");if(user==null||user.length()==0){url="/B.jsp";request.setAttribute("error", "User name must not be empty.");}ServletContext context=getServletContext();RequestDispatcher dispatcher=context.getRequestDispatcher(url);//調用RequestDispatcher類的forward方法轉移到該URL上dispatcher.forward(request, response);}


具體實現其實就是頁面的傳值的判斷。右擊HelloServlet.java檔案,使用debug on server

首先出現的是HTTP status 404~: 

原因有可能1:你的HelloServlet檔案位置有問題,可以調整。2:你的tomcat設定問題重新從Eclipse中的window->Preference->server 使用remove操作,然後重建server。3:是我看到的一個方法而且暫時解決了問題:摘自http://blog.csdn.net/alexhan_bing/article/details/2457891

如果你是按上述第3種取消注釋的方法執行,你可能會出現如下錯誤判錯“Server Tomcat v6.0 Server at localhost failed to start.”

於是你需要做如下事情: 1. 修改Tomcat6.0/conf/context.xml檔案,新加兩個屬性.
<Context  reloadable="true" privileged="true"> 
    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context> 2. 重啟Eclipse,此步非常重要,一定要做 做完後如果運行正常,那麼恭喜你,如果還出現HTTP Status404錯誤,請你看看你建立的Dynamic web project裡build/classes下是否產生了與Servlet同名的.class檔案,如果沒有,那麼你需要 檢查下功能表項目中的project 是否設定了 build Automatically,設定後重新編譯就會產生.class.此時問題也就解決了。 結果圖:

 

最後,希望此貼對大家有協助,如果有錯誤或遺漏還希望各位補充指正,為前來看貼著提供方便,少走彎路

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.