简体   繁体   English

部署时tomcat7 404错误

[英]tomcat7 404 error when deploying

I'm trying to reproduce the solution given in this question but I'm still getting the same error. 我正在尝试重现此问题中给出的解决方案,但仍然遇到相同的错误。

Tomcat 7 keeps giving me a 404. What am I doing wrong? Tomcat 7一直给我一个404。我在做什么错?

This is the servlet code: 这是servlet代码:

edu@ubuntu:~/ch1$ cat ch1servlet.java
package com.example;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ch1servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    PrintWriter out = response.getWriter();
    java.util.Date today = new java.util.Date();
    out.println("<html> " +"<body>" +"<h1 align=center>HF\'s Chapter1 Servlet</h1>" +" " + "<br>" + today + "</body>" + "</html>");
    }
}

Which was compiled with: 使用以下命令编译:

edu@ubuntu:~/ch1/WEB-INF$ javac -classpath /usr/share/tomcat7/lib/servlet-api.jar -d classes ../ch1servlet.java
edu@ubuntu:~/ch1/WEB-INF$ ls classes/com/example/ch1servlet.class
classes/com/example/ch1servlet.class

This is the content of web.xml 这是web.xml的内容

edu@ubuntu:~/ch1/WEB-INF$ cat web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
>
    <servlet>
        <servlet-name>Chapter1 Servlet</servlet-name>
        <servlet-class>com.example.ch1servlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Chapter1 Servlet</servlet-name>
        <url-pattern>/Serv1</url-pattern>
    </servlet-mapping>
</web-app>

Then I copied the ch1 directory to $CATALINA_HOME/webapps 然后我将ch1目录复制到$ CATALINA_HOME / webapps

edu@ubuntu:~/ch1$ cd ..
edu@ubuntu:~$ ps -aux | grep tomcat
tomcat7   1053  0.3 11.3 2557684 85272 ?       Sl   03:12   0:51 /usr/lib/jvm/java-8-oracle/bin/java -Djava.util.logging.config.file=/var/lib/tomcat7/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.awt.headless=true -Xmx128m -XX:+UseConcMarkSweepGC -Djava.endorsed.dirs=/usr/share/tomcat7/endorsed -classpath /usr/share/tomca 7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar -Dcatalina.base=/var/lib/tomcat7 -Dcatalina.home=/usr/share/tomcat7 -Djava.io.tmpdir=/tmp/tomcat7-tomcat7-tmp org.apache.catalina.startup.Bootstrap start
edu       2259  0.0  0.1  11744   916 pts/1    S+   07:24   0:00 grep --color=auto tomcat
edu@ubuntu:~$ sudo mkdir /usr/share/tomcat7/webapps
edu@ubuntu:~$ sudo cp -rf ch1/ /usr/share/tomcat7/webapps/
edu@ubuntu:~$ ls /usr/share/tomcat7/webapps/
ch1
edu@ubuntu:~$

When I try to access 192.168.1.45:8080/ch1/Serv1 当我尝试访问192.168.1.45:8080/ch1/Serv1时

I get the infamous 404 "The requested resource (/ch1/Serv1/) is not available" 我得到了臭名昭著的404“请求的资源(/ ch1 / Serv1 /)不可用”

This is the version of java I'm using: 这是我正在使用的Java版本:

edu@ubuntu:~$ java -version
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

I repeated quite the same process from different tutorials about a dozen of times and I always get stuck in the same point. 我在不同的教程中重复了相同的过程大约十二次,而且我总是陷入同一点。

复制文件并修改web.xml之后,您必须重新启动tomcat才能加载新的web.xml文件。

Ok this is kind of anoying. 好的,这有点烦人。

I copied the directory in CATALINA_BASE/webapps instead of CATALINA_HOME/webapps where every single tutorial I found told me to. 我将目录复制到CATALINA_BASE / webapps中,而不是CATALINA_HOME / webapps中,我发现每个教程都告诉我该目录。

It worked. 有效。 I just would like to know why. 我只是想知道为什么。

I was wondering why there was no webapps directory in CATALINA_HOME and I had to create it. 我想知道为什么CATALINA_HOME中没有webapps目录,所以我不得不创建它。

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

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