0

我创建dynamic web project并添加了 2 个项目:

  1. 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>
    
  2. 在这样的默认包 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

可能是什么问题?

4

2 回答 2

2

从给定的示例中,您应该将 Web 应用程序作为 MyFirstServlet.war 部署到 Tomcat 中(或作为展开的目录 - 这没有区别)并将 GrettingServlet 映射到应用程序根目录- 如果您希望 servlet 处理根:

/WEB-INF/web.xml应该有这些:

<servlet>
    <servlet-name>GrettingServlet</servlet-name>
    <servlet-class>your.package.GrettingServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>GrettingServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

注意错字: “Gretting”(在 servlet 和映射中)与“Greeting”(在 JSP 形式中)

通过您的设置,您应该将浏览器指向http://localhost:8080/MyFirstServlet/GrettingServlet以访问 servlet。

如果您的想法是让 JSP 页面来处理根,那么您应该浏览到其中一个http://localhost:8080/MyFirstServlet/<yourJSPName>.jsp或将 JSP 称为index.jspdefault.jsp(请参阅<welcome-file-list/>您的 参考资料部分web.xml)。在这种情况下,我猜您的想法是显示一个 JSP,然后发布到 servlet,因此请确保您的 servlet 规范和映射是正确web.xml的(servlet 映射和 JSP 表单action属性)。

于 2012-06-24T11:32:23.010 回答
1

只需使用以下内容修改您的 web.xml,它应该可以工作

<servlet>
  <servlet-name>GrettingServlet</servlet-name>
  <servlet-class>GrettingServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>GrettingServlet</servlet-name>
  <url-pattern>/GreetingServlet</url-pattern>
</servlet-mapping>

更新 这里是整个 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>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>/GreetingServlet</url-pattern>
  </servlet-mapping>
</web-app>
于 2012-06-24T11:42:43.370 回答