[英]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.