Tomcat 7一直给我404.我做错了什么? [英] Tomcat 7 keeps giving me a 404. What am I doing wrong?

查看:104
本文介绍了Tomcat 7一直给我404.我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个servlet。这是它的代码。

this is my first servlet ever. here is it's code.

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>");
    }
}

我使用此命令编译它
javac -classpath /usr/share/tomcat7/common/lib/servlet-api.jar -d classes src / Ch1servlet.java

然后我放了我的WEB-INF文件夹中的classes文件夹中的 .class 文件。

I compiled it using this command javac -classpath /usr/share/tomcat7/common/lib/servlet-api.jar -d classes src/Ch1servlet.java
I then put the .class file in the classes folder in my WEB-INF folder.

这是我的web.xml

Here is my web.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
    <servlet>
        <servlet-name>Chapter1 Servlet</servlet-name>
        <servlet-class>Ch1Servlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Chapter1 Servlet</servlet-name>
        <url-pattern>/Serv1</url-pattern>
    </servlet-mapping>
</web-app>

Tomcat7在 http://127.0.0.1上给我一个404: 8080 / ch1 / Serv1 / 请求的资源(/ ch1 / Serv1 /)不可用。

Tomcat7 keeps giving me a 404 on http://127.0.0.1:8080/ch1/Serv1/ saying The requested resource (/ch1/Serv1/) is not available.

文件树:

File Tree:

我在这里做错了什么?

推荐答案

您应该将servlet类放在一个包中。无包装servlet是否有效取决于较旧的Tomcat和JVM版本的特定组合。如果你在书/教程中看到这个例子,那肯定是过时了。

You should put servlet classes in a package. Whether packageless servlets works depend on the specific combination of an older Tomcat and JVM version. If you see this example in a book/tutorial, then it is surely far outdated.

package com.example;

// ...

public class Ch1Servlet extends HttpServlet {
    // ...
}

你应该有一个 /com/example/Ch1Servlet.java 文件。编译如下

You should have a /com/example/Ch1Servlet.java file. Compile it as follows

javac -classpath /usr/share/tomcat7/common/lib/servlet-api.jar -d classes src/com/example/Ch1servlet.java

(I但是想知道常见 lib在那里做什么,这对于Tomcat 4.x / 5.x来说是典型的,但是从Tomcat 6开始它就不存在了。如果你手动改变了Tomcat的结构为了遵循过时教程的说明,撤消吧!)

(I however wonder what the common lib is doing there, this was typical for Tomcat 4.x/5.x, but it's not present since Tomcat 6. If you manually changed Tomcat's structure in order to follow the instructions of an outdated tutorial, undo it!)

放入 com 包含您在webapp的 / WEB-INF / classes 文件夹中的生成类的文件夹。所以你必须有 /WEB-INF/classes/com/example/Ch1Servlet.class

Put the com folder with the generated class by its entirity in /WEB-INF/classes folder of your webapp. So you must have a /WEB-INF/classes/com/example/Ch1Servlet.class.

然后,编辑 /WEB-INF/web.xml 以在< servlet-class>中指定servlet类的完全限定名称(FQN) / code>:

Then, edit your /WEB-INF/web.xml to specify the fully qualified name (FQN) of the servlet class in <servlet-class>:

<?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>

(请注意我修复了根声明以符合Tomcat 7支持的servlet版本,否则它将回归到最低兼容性模式)

这篇关于Tomcat 7一直给我404.我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆