Tomcat 上的 Servlet:未找到请求的资源 [英] Servlets on Tomcat: Requested resource is not found

查看:30
本文介绍了Tomcat 上的 Servlet:未找到请求的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Mac OSX.这是我的第一个 Servlet.我有 Tomcat 8.0.14

I am on Mac OSX. This is my first Servlet. I have Tomcat 8.0.14

我创建了一个 Servlet,它只通过 HTTP POST 方法接受三个参数.这个是我的 Servlet:

I have created a Servlet that simply takes three parameters via HTTP POST method. This is my Servlet:

HelloWorld.java:

package com.hello.world;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HelloWorld extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {

        String fname = request.getParameter("fname");
        String mname = request.getParameter("mname");
        String lname = request.getParameter("lname");

        response.setContentType("text/html");

        try (PrintWriter out = response.getWriter()) {

            out.println("<center><h1>HelloWorld!<br></h1>");
            out.println("<h2>" + (fname != null ? fname : "") + " "
                               + (mname != null ? mname : "") + " "
                               + (lname != null ? lname : "") + "</h2></center>");

        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }
}  

代码编译正确,我将编译后的类文件放在默认的 webapps/ROOT/WEB-INF/classes 目录中:

The code compiles properly and I have placed the compiled class file in the default webapps/ROOT/WEB-INF/classes directory as:

/Library/Java/Tomcat/webapps/ROOT/WEB-INF/classes/com/hello/world/HelloWorld.class  

然后我的 web.xml 文件为:

Then I have my web.xml file as:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1"
         metadata-complete="true">

         <servlet>
             <servlet-name>HelloWorld</servlet-name>
             <servlet-class>com.hello.world.HelloWorld</servlet-class>
         </servlet>

         <servlet-mapping>
             <servlet-name>HelloWorld</servlet-name>
             <url-pattern>/controller.do</url-pattern>
         </servlet-mapping>

</web-app>   

我已将此文件放在默认的 webapps/ROOT/WEB-INF/ 目录中:

I have placed this file in the default webapps/ROOT/WEB-INF/ directory as:

/Library/Java/Tomcat/webapps/ROOT/WEB-INF/web.xml

然后我的 index.html 为:

<!DOCTYPE html>
<html>
    <head>
        <title>Hello, World!</title>
    </head>

    <body>
        <form method="POST" action="controller.do">
            <center>
                <table>
                    <tr align="right">
                        <td><label for="fname">Firstname:</label></td>
                        <td><input type="textfield" name="fname"></td>
                    </tr>
                    <tr align="right">
                        <td><label for="mname">Middlename:</label></td>
                        <td><input type="textfield" name="mname"></td>
                    </tr>
                    <tr align="right">
                        <td><label for="lname">Lastname:</label></td>
                        <td><input type="textfield" name="lname"></td>
                    </tr>
                </table>

                <input type="submit" value="Post this stupid form">
            </center>
        </form>
    </body>
</html>

我已将此文件放在默认的 webapps/ROOT/ 目录中:

I have placed this file in the default webapps/ROOT/ directory as:

/Library/Java/Tomcat/webapps/ROOT/index.html

当我输入 localhost:8080/index.html 时,HTML 会加载,我可以正确发布表单并获得所需的输出.
这工作正常.

When I type localhost:8080/index.html, the HTML loads up and I am able to post the form properly and get the desired output.
This works fine.

但是当我创建一个名为 HelloWorld 的单独目录并将这三个文件移动到其中时,我的目录结构变成了这样:

But when I create a separate directory named HelloWorld and move these three files in it, such than my directory structure becomes like this:

/Library/Tomcat/webapps/ROOT
|
|->HelloWorld
    |
    |->index.html
    |->WEB-INF
        | 
        |->web.xml
        |->classes
            |
            |->com
                |->hello
                    |->world
                        |->HelloWorld.class  

并输入 localhost:8080/HelloWorld,HTML 加载良好.但是当我发布表单时,它说

and type localhost:8080/HelloWorld, the HTML loads up fine. But when I post the form, it says

HTTP Status 404 - /HelloWorld/controller.do

type Status report

message /HelloWorld/controller.do

description The requested resource is not available.

Apache Tomcat/8.0.14  

我无法弄清楚是什么问题.是因为我创建了一个单独的目录吗?但我认为这不应该.我需要帮助.我如何让它工作?

I am not able to figure out what is the problem. Is it because I have created a separate directory? But I think that shouldn't be. I need help. How do I get this working?

推荐答案

在我看来,您在 ROOT 下有 HelloWorld 文件夹,就像 webapps/ROOT/HelloWorld/ 中的 web.xmlwebapps/ROOT/HelloWorld/WEB-INF/web.xmlwebapps/ROOT/HelloWorld/WEB-INF/classes/... 中的类它不会像这样工作.

It sounds to me like you have the HelloWorld folder under ROOT, like webapps/ROOT/HelloWorld/ with a web.xml in webapps/ROOT/HelloWorld/WEB-INF/web.xml and the classes in webapps/ROOT/HelloWorld/WEB-INF/classes/... It won't work like that.

webapps 下的每个文件夹都是它自己的独立应用程序,只能有一个 web.xml 处于活动状态.所以 ROOT 是一个 webapp,它只会读取 webapps/ROOT/WEB-INF/web.xml 中的 web.xml 和 webapps/ROOT 中的类/WEB-INF/classes/...

Each folder directly under webapps is its own self-contained app that can only have one web.xml active. So ROOT is a webapp that will only read the web.xml found at webapps/ROOT/WEB-INF/web.xml with the classes in webapps/ROOT/WEB-INF/classes/...

如果你有另一个文件夹 webapps/HelloWorld 它会读取 webapps/HelloWorld/WEB-INF/web.xml 中的 web.xml 和 中的类>webapps/HelloWorld/WEB-INF/classes/...

If you had another folder webapps/HelloWorld it would read the web.xml in webapps/HelloWorld/WEB-INF/web.xml with the classes in webapps/HelloWorld/WEB-INF/classes/...

这是正常的做法.

但会话不会跨应用共享.这样做的话,root 将拥有自己的会话,而 HelloWorld 将拥有自己的会话.

But session is not shared across apps. So doing it that way, root will have its own session, and HelloWorld its own session.

因此,如果您确实需要将 HelloWorld 置于根目录下,那么您必须在 webapps/ROOT/WEB-INF/web.xml 中的 web.xml 中定义您的 servlet.xml 并将您的类文件直接放在根目录下的 WEB-INF 中: webapps/ROOT/WEB-INF/classes/... 然后你必须定义 url-pattern在 web.xml 中作为(至少理论上我认为这会起作用):

So, if you really need HelloWorld to be under root, then you'll have to define your servlet in the web.xml in webapps/ROOT/WEB-INF/web.xml and put your class files in the WEB-INF directly under root: webapps/ROOT/WEB-INF/classes/... Then you'd have to define the url-pattern in the web.xml as (at least theoretically I think this would work):

     <servlet-mapping>
         <servlet-name>HelloWorld</servlet-name>
         <url-pattern>/HelloWorld/controller.do</url-pattern>
     </servlet-mapping>

这篇关于Tomcat 上的 Servlet:未找到请求的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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