简体   繁体   English

Ubuntu14.04 html 上的 Tomcat7 可以工作,但 servlet 出现 404 错误

[英]Tomcat7 on Ubuntu14.04 html works but 404 error for servlets

I'm new to servlets.我是 servlet 的新手。 I've installed Tomcat7 in Ubuntu14.04 and the service runs fine.我在 Ubuntu14.04 中安装了 Tomcat7,服务运行良好。 But it gives 404(not found) error for servlet ( http://localhost:8080/hello/HelloServlet ).但是它为 servlet ( http://localhost:8080/hello/HelloServlet ) 提供了 404(not found) 错误。 The html within the application runs fine.应用程序中的 html 运行良好。 So are the Tomcat managerial tools. Tomcat 管理工具也是如此。

Any idea of what I'm doing wrong?知道我做错了什么吗?

Tomcat webapps structure: /var/lib/tomcat7/webapps Tomcat webapps结构: /var/lib/tomcat7/webapps

webapps
|-- ROOT
    |-- hello
    |   |-- index.html
    |   `-- WEB-INF
    |       |-- classes
    |       |   |-- HelloServlet.class
    |       |   `-- HelloServlet.java
    |       |-- lib
    |       `-- web.xml
    |-- index.html
    `-- META-INF
        `-- context.xml

web.xml:网页.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">
<servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>HelloServlet</servlet-class>
</servlet>

<servlet-mapping>
     <servlet-name>HelloServlet</servlet-name>
     <url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>

HelloServlet.java: HelloServlet.java:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;

@WebServlet(name="HelloServlet", urlPatterns={"/HelloServlet"})

public class HelloServlet extends HttpServlet{
  private String message;
  public void init() throws ServletException{
      message = "Hello World";
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("<h1>" + message + "</h1>");
  }
  public void destroy(){

  }
}

Finally I got it running :)最后我让它运行了:)

It was the directory structuring that was creating the problem.是目录结构造成了问题。 The application directory should be just outside the 'webapps/ROOT' directory.应用程序目录应该在“webapps/ROOT”目录之外。 To be specific, just put the application directory directly under the 'webapps' (Seems to be specific case for Tomcat7 and Ubuntu 14.04LTS)具体来说,将应用程序目录直接放在'webapps'下(似乎是Tomcat7和Ubuntu 14.04LTS的具体情况)

The corrected structure:修正后的结构:

webapps
|-- ROOT
`-- hello
    |-- index.html
    `-- WEB-INF
        |-- classes
        |   |-- HelloServlet.class
        |   `-- HelloServlet.java
        |-- lib
        `-- web.xml

The previous structure (with error):之前的结构(有错误):

webapps
|-- ROOT
    |-- hello
    |   |-- index.html
    |   `-- WEB-INF
    |       |-- classes
    |       |   |-- HelloServlet.class
    |       |   `-- HelloServlet.java
    |       |-- lib
    |       `-- web.xml
    |-- index.html
    `-- META-INF
        `-- context.xml

The java source file should not be in the /WEB-INF folder. java 源文件不应位于 /WEB-INF 文件夹中。 Servlet is a regular java file and should be located in the /src/... folder. Servlet 是一个常规的 java 文件,应该位于 /src/... 文件夹中。 also, once you annotate the class as @WebServlet - u can remove the web.xml file ( although it has many other usage, using it for 'servlet-mapping' can be ignored for simplification).此外,一旦您将该类注释为@WebServlet - 您可以删除 web.xml 文件(尽管它有许多其他用途,为了简化可以忽略将其用于“servlet-mapping”)。

├───build
│   └───classes
│       └───pkg
│               HelloServlet.class
│
├───src
│   └───pkg
│           HelloServlet.java
│
└───WebContent
    ├───META-INF
    │       MANIFEST.MF
    │
    └───WEB-INF
        └───lib

你的java类文件位置不对,把你的java类文件改成src文件夹。

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

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