7

Eclipse Juno で初めての HelloWorld サーブレットを作成し、それを J2EE Preview Server で表示しようとしています。

これは私のサーブレットクラスです:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloWorld
 */
public class HelloWorld extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloWorld() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        pw.println("<html>");
        pw.println("<head><title>Hello World</title></title>");
        pw.println("<body>");
        pw.println("<h1>Hello World</h1>");
        pw.println("</body></html>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

そして、これはEclipseによって自動的に生成された私の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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>HelloWorld</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>
  <servlet>
    <description></description>
    <display-name>HelloWorld</display-name>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>HelloWorld</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
  </servlet-mapping>
</web-app>

「Run on Server」>「J2EE Preview」を選択すると、次のようになります。

エラー 404 - 見つかりません

このサーバーには、この要求に一致または処理されたコンテキストがありませんでした。このサーバーに認識されているコンテキストは次のとおりです。

ハローワールド(/ハローワールド)

私はどこで間違っていますか?

4

2 に答える 2

1

「J2EE Preview」サーバーでは、コンテキスト パスはプロジェクトの名前です。サーバーを起動すると、利用可能なすべてのコンテキストパスが一覧表示されます。

たとえば、アプリケーションの名前が「app1」の場合、URL は「http://localhost:8080/app1/HelloWorld」になります。

于 2012-10-11T07:12:12.190 に答える
0

私は同じ問題を抱えていて、それを機能させることができませんでした。Hello World JSP ページだけの、はるかに単純なアプリケーションを作成しました。

このプロセスは非常に基本的なものです。Web 動的プロジェクトを作成し、WebContent ディレクトリの下に index.jsp ファイルを作成します。これは、次のように実行してアプリケーションを起動するのに十分なはずです -> サーバーで実行 -> J2EE プレビューですが、常に次のようになります。

404 not found このサーバーには、この要求に一致または処理されたコンテキストがありません。このサーバーが認識しているコンテキストは次のとおりです: test(/test)

eclipse juno は indigo ほど安定していないと聞きました。indigo の Java EE バージョンをダウンロードしたところまったく同じで、問題なく正常に動作しました。

編集:JBossやGlassfishな​​どの別のアプリケーションサーバーをダウンロードして、それらのアプリケーションで実行しようとすると、問題が解決するはずです。

この問題を修正するには、ワークスペースにあるすべてのファイルとフォルダーを削除し、.metadata フォルダーとそのすべてのコンテンツを削除します。Eclipse を開始して再試行すると、うまくいく場合があります。

これがあなたを助けることを願っています。よろしく!

于 2012-11-28T18:50:49.257 に答える