微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Tomcat 7一直给我一个404我在做什么错?

如何解决Tomcat 7一直给我一个404我在做什么错?

您应该将servlet类放在包中。无包servlet是否起作用取决于旧版Tomcat和JVM版本的特定组合。如果您在书/教程中看到此示例,那么它肯定已经过时了。

package com.example;

// ...

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

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

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

(I however wonder what thecommon 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, it!)

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.

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>

(please note that I fixed the root declaration as well to comply Tomcat 7 supported servlet version, it would otherwise fall back to least compatibility modus)

解决方法

这是我有史以来的第一个servlet。这是代码。

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
然后将.class文件放入WEB-INF文件夹中的classes文件夹中。

这是我的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一直给我一个404的http://127.0.0.1:8080/ch1/Serv1/说法The requested resource (/ch1/Serv1/) is not available.

文件树: File Tree

我在这里做错了什么?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。