简体   繁体   English

在Tomcat Eclipse上部署简单的Servlet

[英]Simple Servlet deploy on Tomcat eclipse

I'm following Head First Java. 我正在关注Head First Java。

I created a simple servlet as they instructed but they did not write how to deploy it. 我按照他们的指示创建了一个简单的servlet,但是他们没有编写如何部署它。

I'm trying to deploy it on Tomcat 7 and i have set it up via eclipse. 我正在尝试将其部署在Tomcat 7上,并且已经通过eclipse进行了设置。

However im getting a 404 page error. 但是,我收到404页面错误。

i created a web.xml i also placed the class files in WEB-INF/classes. 我创建了一个web.xml,还将类文件放在WEB-INF / classes中。

Here is the code. 这是代码。

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();
}
}

other java code file: 其他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: 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>

our servlet calss is in the package org.code , but the class name you set in the web.xml is org.yasin.KathyServlet . 我们的servlet calss在org.code包中,但是您在web.xml中设置的类名是org.yasin.KathyServlet

Moreover, you gave the name kathyServlet to your servlet in the web.xml, but your mapping uses the name KathyServlet . 此外,您在web.xml中为您的servlet命名了kathyServlet ,但是您的映射使用了名称KathyServlet Servlet names are case-sensitive. Servlet名称区分大小写。

If that code is right you web.xml is bad. 如果该代码正确,则web.xml不好。 Your are defining a servlet-class using: 您正在使用以下方法定义Servlet类:

org.yasin.KathyServlet org.yasin.KathyServlet

and your Servlet is: 而您的Servlet是:

org.code.KathyServlet org.code.KathyServlet

The Servlet that you are using in your web.xml need to point to the correct package and Sevlet name. 您在web.xml中使用的Servlet需要指向正确的包和Sevlet名称。

You should see a ClassNotFound in your tomcat log associated with you 404 error. 您应该在与404错误相关的tomcat日志中看到ClassNotFound。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM