【发布时间】:2012-11-15 14:36:09
【问题描述】:
我对 Web 编程非常陌生,我正在尝试编写一个简单的应用程序来从输入表单中收集数据并通过 HTTPServletRequest 将其传递给 servlet。我知道每个 servlet 都需要映射到 web.xml 内的特定 URL。我的web.xml 如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>InputFormTest</display-name>
<welcome-file-list>
<welcome-file>InputForm.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>InputFormTest</display-name>
<servlet-name>InputFormTest</servlet-name>
<servlet-class>com.jsp.core.InputFormTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>InputFormTest</servlet-name>
<url-pattern>/InputFormTest</url-pattern>
</servlet-mapping>
</web-app>
InputForm.jsp 显示正确。我已经使用 Eclipse 的上下文菜单添加了 InputFormTest servlet,即 New -> Servlet,它具有以下代码:
package jsp.core;
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;
/**
* Servlet implementation class InputFormTest
*/
public class InputFormTest extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>InputFormTest</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>User inserted: " + request.getParameter("id") + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
我有两个问题:
-
首先,在 Eclipse 中运行应用程序后,我收到以下错误
HTTP 状态 404 - /InputFormTest
输入状态报告
消息/InputFormTest
说明请求的资源不可用。
Apache Tomcat/6.0.36
-
其次,在
李>web.xml中更改<url-pattern>/InputFormTest</url-pattern>不会影响上述输出,这很奇怪,因为我认为message /InputFormTest应该反映web.xml中的设置
任何提示表示赞赏。
【问题讨论】:
标签: eclipse jsp tomcat servlets