![](/img/trans.png)
[英]IntelliJ Idea: Deploy a simple Java servlet (no JSP) to Tomcat 7
[英]Simple Servlet deploy on Tomcat eclipse
我正在關注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>
我們的servlet calss在org.code
包中,但是您在web.xml中設置的類名是org.yasin.KathyServlet
。
此外,您在web.xml中為您的servlet命名了kathyServlet
,但是您的映射使用了名稱KathyServlet
。 Servlet名稱區分大小寫。
如果該代碼正確,則web.xml不好。 您正在使用以下方法定義Servlet類:
org.yasin.KathyServlet
而您的Servlet是:
org.code.KathyServlet
您在web.xml中使用的Servlet需要指向正確的包和Sevlet名稱。
您應該在與404錯誤相關的tomcat日志中看到ClassNotFound。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.