2014-02-05 261 views
0

我試圖使用Eclipse和Tomcat 7.0運行一個基本的servlet,但它一直給404錯誤錯誤404的Apache Tomcat使用Eclipse

HTTP Status 404 - /ServletExample/ 

-------------------------------------------------------------------------------- 

type Status report 

message /ServletExample/ 

description The requested resource (/ServletExample/) is not available. 


-------------------------------------------------------------------------------- 

Apache Tomcat/7.0.27 

不同的代碼是: 回到Home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>My first JSP</title> 
</head> 
<body> 
    <form action="HelloServlet"> 
     Please enter a colour<br> 
     <input type="text" name="color" size="20px"> 
     <input type="submit" value="submit"> 
    </form> 
</body> 
</html> 

HelloWorld.java

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

public class HelloWorld extends HttpServlet 
{ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    { 
     // reading the user input 
     String color= request.getParameter("color"); 
     PrintWriter out = response.getWriter(); 
     out.println (
      "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" " + 
      "\"http://www.w3.org/TR/html4/loose.dtd\">\n" + 
      "<html> \n" + 
      "<head> \n" + 
      "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"> \n" + 
      "<title> My first jsp </title> \n" + 
      "</head> \n" + 
      "<body> \n" + 
      "<font size=\"12px\" color=\"" + color +"\">" + 
      "Hello World" + 
      "</font> \n" + 
      "</body> \n" + 
      "</html>"); 
    } 
} 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="WebApp_ID" version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
<servlet> 
    <servlet-name>Hello</servlet-name> 
    <servlet-class>HelloWorld</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>Hello</servlet-name> 
    <url-pattern>/HelloServlet</url-pattern> 
</servlet-mapping> 
</web-app> 

我跟着this thread

指定的所有步驟,但錯誤依然存在。 請幫忙

+0

您是否將類文件保存在類文件夾中? – SpringLearner

+1

您正在調用/ ServletExample,但您的servlet已映射到/ HelloServlet。順便說一句。該配置不需要.jsp。 –

+0

但url模式是**/HelloServlet **,所以它是正確的提交/ HelloServlet操作。 –

回答

0

我認爲你正在接觸/ServletExample/這是你的上下文。把你的web.xml:

<welcome-file-list> 
    <welcome-file>Home.jsp</welcome-file> 
</welcome-file-list> 

希望這會有所幫助。

+0

OP根本不使用任何JSP。另外,如註釋中所述,當它應該是http:// server/WebApplicationName/HelloServlet /'時,OP試圖訪問'http:// server/ServletExample'。 –

+0

謝謝!將此添加到xml確實可行。你能解釋一下問題究竟是什麼?我將來需要照顧什麼? – Abhishek

+0

@Luiggi,他的上下文是** ServletExample **,當你說'http:// server/WebApplicationName/HelloServlet /'它必須是'http:// server/ServletExample/HelloServlet /',但他想訪問一個頁面,然後在提交 - 調用servlet。 user2464622,你做的不好的是不在web.xml中定義一個默認頁面來訪問應用程序。調用'http:// server/ServletExample /'並且沒有歡迎頁面,服務器不知道在哪裏重定向請求。放一個歡迎頁面服務器確實知道該怎麼做。或者,正如Luiggi說的那樣 - 總是調用** server/context/page.jsp **,並且也可以工作。 –

0

您的web.xml中有一個網址格式/HelloServlet。所以你應該嘗試連接127.0.0.1:8080/HelloServlet,而不是127.0.0.1:8080/ServletExample/

0

添加包裹名稱在web.xml這個樣子。

<servlet-class>package.HelloWorld</servlet-class>