【问题标题】:Simple Servlet deploy on Tomcat eclipseTomcat eclipse 上的简单 Servlet 部署
【发布时间】:2012-01-01 22:01:33
【问题描述】:

我正在关注 Head First Java。

我按照他们的指示创建了一个简单的 servlet,但他们没有写如何部署它。

我正在尝试将它部署在 Tomcat 7 上,并且我已经通过 eclipse 进行了设置。

但是我遇到了 404 页面错误。

我创建了一个 web.xml 我还将类文件放在 WEB-INF/classes 中。

这是代码。

package org.code;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class KathyServlet extends HttpServlet { 

public void doGet (HttpServletRequest request, HttpServletResponse response) throws  ServletException, IOException  {
 PrintWriter out;
 String title = "PhraseOMatic has generated the following phrase.";
     response.setContentType("text/html");
 out = response.getWriter();

    out.println("<HTML><HEAD><TITLE>");
out.println("PhraseOmatic");
out.println("</TITLE></HEAD><BODY>");
out.println("<H1>" + title + "</H1>");
out.println("<P>" + PhraseOMatic2.makePhrase());
    out.println("<P><a href=\"KathyServlet\">make another phrase</a></p>");
out.println("</BODY></HTML>");

out.close();
}
}

其他java代码文件:

package org.code;

public class PhraseOMatic2 {
public static String makePhrase() {

 // make three sets of words to choose from
String[] wordListOne = {"24/7","multi-Tier","30,000 foot","B-to-B","win-win","front-     end", "web-based","pervasive", "smart", "six-sigma","critical-path", "dynamic"};

String[] wordListTwo = {"empowered", "sticky", "valued-added", "oriented", "centric", "distributed", "clustered", "branded","outside-the-box", "positioned", "networked", "focused", "leveraged", "aligned", "targeted", "shared", "cooperative", "accelerated"};

String[] wordListThree = {"process", "tipping point", "solution", "architecture", "core competency", "strategy", "mindshare", "portal", "space", "vision", "paradigm", "mission"};

// find out how many words are in each list
int oneLength = wordListOne.length;
int twoLength = wordListTwo.length;
int threeLength = wordListThree.length;

// generate three random numbers, to pull random words from each list
int rand1 = (int) (Math.random() * oneLength);
int rand2 = (int) (Math.random() * twoLength);
int rand3 = (int) (Math.random() * threeLength);

// now build a phrase
String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " +    wordListThree[rand3];

// now return it
return ("What we need is a " + phrase);
} 
}   

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_3_0.xsd" version="3.0">
<display-name>HFJse</display-name>
<servlet>
<servlet-name>kathyServlet</servlet-name>
<servlet-class>org.yasin.KathyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>KathyServlet</servlet-name>
<url-pattern>/snoop/*</url-pattern>
</servlet-mapping>
</web-app>

【问题讨论】:

    标签: java servlets


    【解决方案1】:

    我们的servlet calss在包org.code中,但是你在web.xml中设置的类名是org.yasin.KathyServlet。

    此外,您在 web.xml 中为您的 servlet 提供了名称 kathyServlet,但您的映射使用名称 KathyServlet。 Servlet 名称区分大小写。

    【讨论】:

    • 对不起,我粘贴了错误的 xml,我将 xml 更改为正确的包名称,但它仍然给出了相同的错误,但我没有注意到小写的 kathy,一旦我更改它就可以正常工作.
    【解决方案2】:

    如果该代码是正确的,那么您的 web.xml 就是错误的。您正在使用以下方法定义一个 servlet 类:

    org.yasin.KathyServlet

    你的 Servlet 是:

    org.code.KathyServlet

    您在 web.xml 中使用的 Servlet 需要指向正确的包和 Sevlet 名称。

    您应该在与您的 404 错误相关联的 tomcat 日志中看到 ClassNotFound。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-26
      • 2013-09-19
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      相关资源
      最近更新 更多