在Tomcat eclipse上部署简单的Servlet

发布于 2024-12-24 00:13:15 字数 3177 浏览 1 评论 0原文

我正在关注 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>

I'm following Head First Java.

I created a simple servlet as they instructed but they did not write how to deploy it.

I'm trying to deploy it on Tomcat 7 and i have set it up via eclipse.

However im getting a 404 page error.

i created a web.xml i also placed the class files in 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:

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我一直都在从未离去 2024-12-31 00:13:15

我们的servlet类位于org.code包中,但您在web.xml中设置的类名是org.yasin.KathyServlet

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

our servlet calss is in the package org.code, but the class name you set in the web.xml is org.yasin.KathyServlet.

Moreover, you gave the name kathyServlet to your servlet in the web.xml, but your mapping uses the name KathyServlet. Servlet names are case-sensitive.

∞觅青森が 2024-12-31 00:13:15

如果该代码是正确的,则您的 web.xml 是错误的。 定义 servlet 类

您正在使用: org.yasin.KathyServlet

,您的 Servlet 是:

org.code.KathyServlet

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

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

If that code is right you web.xml is bad. Your are defining a servlet-class using:

org.yasin.KathyServlet

and your Servlet is:

org.code.KathyServlet

The Servlet that you are using in your web.xml need to point to the correct package and Sevlet name.

You should see a ClassNotFound in your tomcat log associated with you 404 error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文