我创建dynamic web project
并添加了 2 个项目:
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="GrettingServlet" 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>
在这样的默认包
servlet
中(称为GrettingServlet.java
):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 GrettingServlet extends HttpServlet { private static final long serialVersionUID = 1L; public GrettingServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String firstName = request.getParameter("firstName").toString(); String lastName = request.getParameter("lastName").toString(); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet GreetingServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>Welcome " + firstName + " " + lastName + "</p>"); out.println("</body>"); out.println("</html>"); out.close(); } }
我安装了tomcat6
,所以我有Apache Software Foundation
文件夹。最后我想创建war
这个项目的文件,所以我选择了项目Export>War file
,在Destination
文本中我选择了webapps
路径中的文件夹C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\webapps
。该项目称为MyFirstServlet
. 为了查看index.jsp
服务器上的表单,我在浏览器中编写,http://localhost:8080/MyFirstServlet/
但我收到了消息
HTTP Status 404 - /MyFirstServlet/
type Status report
message /MyFirstServlet/
description The requested resource (/MyFirstServlet/) is not available.
Apache Tomcat/6.0.35
servlet 映射是这样的:
<?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>MyFirstServlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>new</description>
<display-name>GrettingServlet</display-name>
<servlet-name>GrettingServlet</servlet-name>
<servlet-class>GrettingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GrettingServlet</servlet-name>
<url-pattern>/GrettingServlet</url-pattern>
</servlet-mapping>
</web-app>
我检查了tomcat并打开了service status : started
可能是什么问题?