简体   繁体   English

虽然tomcat工作正常,但servlet应用程序不起作用

[英]servlet application does not work, although tomcat works correctly

I have installed Apache Tomcat on ubuntu under tomcat directory. 我在tomcat目录下的ubuntu上安装了Apache Tomcat。 I set CATALINA_HOME environment variable for a single session via export. 我通过导出为单个会话设置了CATALINA_HOME环境变量。 Then I started tomcat from the command line via $CATALINA_HOME/bin/startup.sh . 然后我通过$CATALINA_HOME/bin/startup.sh从命令行启动了tomcat。 And everything worked well. 一切都运作良好。 I saw tomcat official page and assume that the server is installed correctly. 我看到tomcat官方页面并假设服务器安装正确。 Then I created a webb application with a single servlet under 然后我创建了一个带有单个servlet的webb应用程序

$CATALINA_HOME/webaps/apress/ 

directory. 目录。

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet
{
    public void doGet(HttpServletRequest request,
          HttpServletResponse response)
          throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>hello, world</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1> hello world</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

Then I compiled the file 然后我编译了文件

javac -cp HelloWorldServlet.java -d ./classes -cp $CATALINA_HOME/bin/servlet-api.jar

Compillation was successful and the class file was put into the classes subdirectory of the root directory of the application. 编译成功,类文件被放入应用程序根目录的classes子目录中。

Then I edited web.xml file. 然后我编辑了web.xml文件。

<?xml version ="1.0" encoding ="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     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_3_0.xsd" version="3.0">
    <display-name>Chapter 2</display-name>
    <description> Apress demo</description>
    <servlet>
        <servlet-name>helloworld</servlet-name>
        <servlet-class>HelloWorldServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloworld</servlet-name>
        <url-pattern>/hello.html</url-pattern>
    </servlet-mapping>
</web-app>

Then I restartd Tomcat. 然后我重新启动了Tomcat。 But when I try to access via 但是当我尝试访问via时

http://localhost:8080/apress/hello.html

I get Http status 404 message. 我得到Http状态404消息。 What I have done wrong? 我做错了什么?

我建议您首先尝试在Netbeans或Eclipse等IDE中构建应用程序,然后研究自动生成的配置文件。

Look in your tomcat directory there is a logs directory. 在tomcat目录中查看一个logs目录。 Stop tomcat. 停止tomcat。 Delete all the log files and restart tomcat and hit that URL. 删除所有日志文件并重新启动tomcat并点击该URL。 Check the logs (mainly access log and catalina.out). 检查日志(主要是访问日志和catalina.out)。 Look for errors in there first. 首先在那里寻找错误。

You may also want to consider using @WebServlet instead of editing the web.xml, modern versions of tomcat can identify classes annotated with this and automatically register them as servlets. 您可能还想考虑使用@WebServlet而不是编辑web.xml,现代版本的tomcat可以识别用它注释的类并自动将它们注册为servlet。

@WebServlet(name="HelloWorld", urlPatterns={"/hello.html"})
public class HelloWorldServlet extends HttpServlet {

}

Also, how are you deploying this servlet, as a WAR file? 另外,您如何将此servlet部署为WAR文件?

Two possible things. 两件事可能。 According to what you wrote, the directory you created the application was: 根据您所写的内容,您创建应用程序的目录是:

$CATALINA_HOME/webaps/apress/

It's supposed to be 它应该是

$CATALINA_HOME/webapps/apress/

But i guess it is just a typeO. 但我想这只是一种类型。

the second thing is that you should copy the compiled file 第二件事是你应该复制编译的文件

HelloWorldServlet.class 

to

apress/WEB-INF/classes

directory, or directly compile the java file into this directory 目录,或直接将java文件编译到此目录中

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

相关问题 在带有eclipse的tomcat servlet中不起作用 - It does not work in tomcat servlet with eclipse Java应用程序后操作可在Eclipse Tomcat中使用,但不适用于本地Tomcat服务器 - Java application post action works in Eclipse Tomcat but does not work on local Tomcat server Spring servlet 适用于 IntelliJ,但不适用于 tomcat - Spring servlet works on IntelliJ but doesn't work on tomcat 新的Servlet 3.0全局错误页面功能在Tomcat 7上不起作用 - New Servlet 3.0 global error page feature does not work on Tomcat 7 为什么在独立应用程序运行时servlet抛出FileNotFoundException? - Why does servlet throw FileNotFoundException, while standalone application works? 应用程序可在Wildfly上运行,但不能在tomcat上运行 - Application works on wildfly but not on tomcat JAX-WS MTOM在Tomcat上不起作用,但是如果由Endpoint发布,则可以起作用 - JAX-WS MTOM does not work on Tomcat but works if published by Endpoint 雄猫 在Eclipse中工作,但在部署时不工作 - Tomcat. Works in Eclipse, but does not work when deployed 自定义servlet在Tomcat 6上提供404,在7上运行良好 - Custom servlet gives 404 on Tomcat 6, works fine on 7 Tomcat servlet 在本地机器上工作,在服务器上中断 - Tomcat servlet works on local machine, breaks on server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM