2012-07-23 35 views
1

我想構建一個Java servlet,並且根據我的教授給我們班的指示完成了所有任務,但是出現了一個奇怪的錯誤。Java Servlet錯誤'資源不可用'

背景:我與Java EE赫利俄斯和Tomcat的工作7.

我開始在Eclipse的一個新的動態Web項目,我做了一個index.jsp頁面,只是得到了用戶的姓名,並將其發送到servlet,然後打印出Hello,[username]。代碼是教授給我們的所有示例代碼,適用於我班的其他人。

我做了一個名爲ServletHome的新Servlet,它在一個名爲servlets的包中。

當我從Eclipse運行程序時,它啓動Tomcat很好,沒有問題。我可以導航到index.jsp頁面,它看起來很好。

的問題是,當我填我的名字,我按下「提交」按鈕,我得到消息了tomcat 404錯誤:「所請求的資源(/ MyFirstServlet/ServletHome)不可用」

任何想法?

謝謝!

---編輯:代碼---

的index.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>Insert title here</title> 
</head> 
<body> 
<form action="ServletHome" method="POST"> 
    First Name: <input type="text" name="firstName" size="20"><br> 
    Last Name: <input type="text" name="lastName" size="20"> <br> 
    <br> <input type="submit" value="Submit"> 
</form> 
</body> 
</html> 

ServletHome.java:

package servlets; 

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 ServletHome extends HttpServlet { 

private static final long serialVersionUID = 1L; 
public ServletHome() { 
    super(); 
} 

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException 
{ 
} 

/** 
* The function that gets the name and returns an HTML file with Hello to them 
* @param request 
* @param response 
* @throws ServletException 
* @throws IOException 
*/ 
protected void doPost(HttpServletRequest request, 
              HttpServletResponse response) 
throws ServletException, IOException { 

    //set type of output 
    response.setContentType("text/html;charset=UTF-8"); 

    //get writer 
    PrintWriter out = response.getWriter(); 

    //get first name 
    String firstName = request.getParameter("firstName").toString(); 

    //get last name 
    String lastName = request.getParameter("lastName").toString(); 

    //write the file 
    out.println("<html>"); 
    out.println("<head>"); 
    out.println("<title>Test Title</title>"); 
    out.println("</head>"); 
    out.println("<body>"); 
    out.println("<p>Welcome, " + firstName + " " + lastName + "!</p>"); 
    out.println("</body>"); 
    out.println("</html>"); 

    out.close(); 
} 
} 

的web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE web-app 
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
     "http://java.sun.com/dtd/web-app_2_3.dtd"> 
<web-app> 
     <servlet> 
      <servlet-name>ServletHome</servlet-name> 
      <servlet-class>servlets.ServletHome</servlet-class> 
     </servlet> 
     <servlet-mapping> 
      <servlet-name>ServletHome</servlet-name> 
      <url-pattern>/servlets/*</url-pattern> 
     </servlet-mapping> 
</web-app> 
+2

看來你的servlet在web.xml中映射,你試圖訪問的url不匹配。如果你可以粘貼你試圖訪問的web.xml和url的一部分,這將是一件好事。 – kosa 2012-07-23 20:43:49

+0

你必須給我們一個你的代碼樣本。 – 2012-07-23 20:46:05

+0

我剛剛添加了代碼,非常感謝您的幫助! – BIU 2012-07-23 21:32:31

回答

5

該資源不可用,因爲:

  1. 您的操作屬性錯誤或;
  2. 您沒有正確映射您的servlet。要做到這一點,您可以:

使用@WebServlet標註,因爲你使用的是Tomcat 7:

@WebServlet(name = "ServletName", urlPatterns = { "/path/to/your/servlet/myName" }) 
public class ServletName extends HttpServlet { 
    // your code here 
} 

或者在web.xml中映射你的servlet:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" 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_3_0.xsd"> 
    <!-- more code here... --> 
    <servlet> 
     <servlet-name>ServletName</servlet-name> 
     <servlet-class>yout.package.here.ServletName</servlet-class> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>ServletName</servlet-name> 
     <url-pattern>/path/to/your/servlet/myName</url-pattern> 
    </servlet-mapping> 
    <!-- more code here... --> 
</web-app> 

另一件需要注意的事情是,你必須實現你希望servlet服務的與HTTP方法(get,post等)相對應的doXXX方法。

要通過你表單請求這個servlet,你需要設置action屬性爲:

<form action="/path/to/your/servlet/myName"> 
    <!-- form fields here... --> 
</form> 

要完成,你可以使用方法屬性,在形式選擇HTTP方法,你的瀏覽器將使用請求Servlet。如果您不提供,默認方法是得到。正如我已經說過的,如果使用get方法,則需要在servlet中實現doGet方法。

+0

真棒,非常感謝你,它現在的作品! – BIU 2012-07-24 05:27:40