2016-03-26 11 views
2

サーブレットとJSPの新機能です。私はEclipse JEEバージョンで小さなアプリケーションをやった。しかし、私が実行しているときに、TomcatがEclipseに正しくインストールされていても、 'http:404:resource not found'エラーが表示されます。ここで私が使用していたコードは次のとおりです。HTTP 404リソースが見つかりません

これは私のServletDemo.java

package com.advancejava; 

import java.io.IOException; 
import java.io.PrintWriter; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

/** 
    * Servlet implementation class ServletDemo 
*/ 
@WebServlet(description = "ServletDemo", urlPatterns = { "/ServletDemo" }) 

    public class ServletDemo extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

/** 
* @see HttpServlet#HttpServlet() 
*/ 
public ServletDemo() { 
    super(); 
    // TODO Auto-generated constructor stub 
} 
protected void processRequest(HttpServletRequest request, 
     HttpServletResponse response)throws ServletException, IOException { 
    response.setContentType("text/html"); 
    try(PrintWriter pw = response.getWriter()) { 
     pw.println("<!DOCTYPE html>"); 
     pw.println("<html>"); 
     pw.println("<head>"); 
     pw.println("<body>"); 
     pw.println("<h1>Servlet Demo"+request.getContextPath()+"</h1>"); 
     pw.println("</body>"); 
     pw.println("</html>"); 
    } 
} 
/** 
* @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(); 
    try{ 
     pw.println("<html>"); 
     pw.println("<head>"); 
     pw.println("<title>Servlet</title>"); 
     pw.println("</head>"); 
     pw.println("<body>"); 
     pw.println("<p>First DemoServlet</p>"); 
     pw.println("</body>"); 
     pw.println("</html>"); 
    } 
    finally{ 
     System.out.println("This is my Servlet Page"); 
    } 
} 

}

web.xmlの

<?xml version="1.0" encoding="ISO-8859-1"?> 

    <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" 
metadata-complete="true" version="3.0"> 
<display-name>ServletDemo</display-name> 
<welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
</welcome-file-list> 
<servlet> 
    <servlet-name>ServletDemo</servlet-name> 
    <servlet-class>com.advancejava.ServletDemo</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>ServletDemo</servlet-name> 
    <url-pattern>/ServletDemo</url-pattern> 
</servlet-mapping></web-app> 

私のindex.html

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="ISO-8859-1"> 
    <title>Insert title here</title> 
    </head> 
    <body> 
     Advance java 
      <a href="com.advancejava.ServletDemo"></a> 
    </body> 
</html> 
です

間違いがある場合は、f彼らを訂正する方法を教えてください。

+0

この行が ''ではないかどうかは、 ''にする必要があります。それ以外の場合は、ページのユーザに関連するサーブレットのディレクトリ構造を見せたい場合には、前記サーブレットのための 'url-pattern'を作成することに意味がありません。さらに、web.xmlと一緒にアノテーションを使用すると、混乱の原因になります。誰も使用してください:-) –

+0

http:// localhost:8080/AdvanceJavaPractice/servlet/com.advancejava.ServletDemo –

+0

残念ながら、それを 'Something'に置き換えると 'href'を見てください最初のスラッシュがありません。これで、 'localhost:8080/index.html'のように' index.html'を開き、 'Something'をクリックすると、ブラウザのアドレスは' localhost:8080/ServletDeo'になります –

答えて

2

は、すでに

@WebServlet(description = "ServletDemo", urlPatterns = { "/ServletDemo" }) 

でサーブレットを宣言したので、あなたのweb.xmlに二度宣言していません。


サーブレットへのURLが

http://localhost:8080/{contextPath}/ServletDemo 

あなたはもう一つ


あり日食使用した場合のcontextPathは、デフォルトでは、あなたのプロジェクト名になりますが、メタデータの完全削除する必要があります= "true"をweb.xmlに追加します。それ以外の場合は動作しません。

<?xml version="1.0" encoding="ISO-8859-1"?> 

<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" 
version="3.0"> 
<display-name>ServletDemo</display-name> 
<welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 
</web-app> 
+0

私はこれらの2つの例外を取得していますjava.lang.ClassNotFoundException:ServletDemo とjavax.servlet.ServletException:サーブレットクラスのインスタンス化エラーServletDemo –

+0

すべてのコードをコピーし、上記の変更を行った、それは動作するはずです。 –

+0

エラーに変更はありません。http 404リソースが見つかりませんでした。エラーです。 web.xmlのコードをここに示したように変更しました。しかし、エラーの変更はありません –

0

前の返信:インデックスページは「my index.html」または「index.html」である必要があります。 サーブレット内にあるものを正しく実行する必要があります。本当にそれを2回投稿

index.htmlを

が問題です。私はこれが解決されてうれしいです。それは私にとっても役に立ちます。おかげ

+0

インデックスを混同して申し訳ありません。htmlの –

関連する問題