2011-10-04 6 views
3

私はweb1と呼ばれるWebプロジェクトを作成し、サーブレットと呼ばれるにservlet1を追加し、web.xmlが続くようである:混乱がMyEclipseので

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <servlet> 
    <servlet-name>servlet1</servlet-name> 
    <servlet-class>servlet1</servlet-class> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>servlet1</servlet-name> 
    <url-pattern>/test</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

を私はアドレスを入力するとします。http://ブラウザでlocalhost:8080/web/test、それは動作しませんでした。何回も試みましたが、答えがありません。問題は何ですか?ありがとう!ここで コードです:

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; 


public class servlet1 extends HttpServlet { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = -6214906967399177511L; 

    /** 
    * Constructor of the object. 
    */ 
    public servlet1() { 
     super(); 
    } 

    /** 
    * Destruction of the servlet. <br> 
    */ 
    public void destroy() { 
     super.destroy(); // Just puts "destroy" string in log 
     // Put your code here 
    } 

    /** 
    * The doGet method of the servlet. <br> 
    * 
    * This method is called when a form has its tag value method equals to get. 
    * 
    * @param request the request send by the client to the server 
    * @param response the response send by the server to the client 
    * @throws ServletException if an error occurred 
    * @throws IOException if an error occurred 
    */ 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 

     response.setContentType("text/html"); 
     PrintWriter out = response.getWriter(); 
     out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 
     out.println("<HTML>"); 
     out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); 
     out.println(" <BODY>"); 
     out.print(" This is "); 
     out.print(this.getClass()); 
     out.println(", using the GET method"); 
     out.println(" </BODY>"); 
     out.println("</HTML>"); 
     out.flush(); 
     out.close(); 
    } 

    /** 
    * The doPost method of the servlet. <br> 
    * 
    * This method is called when a form has its tag value method equals to post. 
    * 
    * @param request the request send by the client to the server 
    * @param response the response send by the server to the client 
    * @throws ServletException if an error occurred 
    * @throws IOException if an error occurred 
    */ 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 

     response.setContentType("text/html"); 
     PrintWriter out = response.getWriter(); 
     out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 
     out.println("<HTML>"); 
     out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); 
     out.println(" <BODY>"); 
     out.print(" This is "); 
     out.print(this.getClass()); 
     out.println(", using the POST method"); 
     out.println(" </BODY>"); 
     out.println("</HTML>"); 
     out.flush(); 
     out.close(); 
    } 

    /** 
    * Initialization of the servlet. <br> 
    * 
    * @throws ServletException if an error occurs 
    */ 
    public void init() throws ServletException { 
     // Put your code here 
    } 

} 
+0

Webサーバーは稼働していますか?サーブレットコードを表示してください。 – adatapost

+0

Myeclipseによって作成されたデフォルトのサーブレットです。何も追加しませんでした。 –

+0

私はMyeclipseを使用していないので、何がデフォルトのサーブレットにあるのかわかりません。 – adatapost

答えて

0

まあいつものように、あなたはでsuper.init(設定)言うことを忘れてしまいました。

@Override 
    public void init(ServletConfig config) throws ServletException { 
     super.init(config);    
    } 

メソッドをオーバーライドし、親のinitを呼び出すのを忘れてしまったため、サーブレットは初期化されませんでした。

内部の情報がわからない場合は、何をしているのかを確認している場合を除き、何も上書きしないでください。