我想構建一個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>
看來你的servlet在web.xml中映射,你試圖訪問的url不匹配。如果你可以粘貼你試圖訪問的web.xml和url的一部分,這將是一件好事。 – kosa 2012-07-23 20:43:49
你必須給我們一個你的代碼樣本。 – 2012-07-23 20:46:05
我剛剛添加了代碼,非常感謝您的幫助! – BIU 2012-07-23 21:32:31