Apache tomcat 和 servlet(初学者)- 404 错误,也许 类 没有找到?
Apache tomcat and servlet (beginner) - 404 error, maybe classes are not found?
我是 servlet 的初学者,对于 "hello world style" servlet,我安装了 apache tomcat 并在其 webapps
目录下创建了以下内容:
servletdir
|- index.xhtml
|- WEB-INF
|- web.xml
|- classes
|- mypackage
|- ServletClass.class
已初始化所有内容以便我可以在本地主机上访问我的 servlet:
web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>mypackage.ServletClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/servleturl/*</url-pattern>
</servlet-mapping>
</web-app>
并且 index.xhtml 包含一个具有 action="/servleturl" method="get"
的表单
理论上,当我启动 tomcat 时,我现在应该能够通过 localhost:8080/servletdir/index.xhtml
访问 servlet 和 index.xhtml 文件并在那里提交表单。达到索引没有问题,但是当我提交表单时,我收到 servlet 的 404 错误,说它 "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.".
我的 servlet class 应该不是问题,它扩展了 HttpServlet
并覆盖了 doGet
以通过 ServletResponse
参数的 getWriter().println()
。 servlet class 中的错误会导致 404 吗?是我的目录结构还是web.xml错了?
我尝试过多种目录结构和解决方案,包括:
- WEB-INF 中的扁平目录结构和 class 不在包中(没有 "classes" 目录)
- 没有 WEB-INF,一切都直接在 servlet 目录中
- 将 WEB-INF 和 index.xhtml 包装在名为 "WebContent"
的目录中
- 保留"classes"目录但不使用包,将class个文件直接放入classes*
我关注了多种资源,包括一些教程和此处的许多问题,它们与我的问题没有直接关系,但包含一些信息,但由于我是这项技术的新手,我无法推断出哪个资源是好的和不好的,这就是为什么我几乎尝试了我发现的所有东西。
上面描述的目录结构直接在磁盘上作为这个结构,而不是在 war 文件中,但由于我可以访问某些部分,我想这不是问题所在。
*:请注意,每次表示包的目录结构发生变化时,我都会在文件的第一行使用相应的 "package" 重新编译 java 文件,这不是问题
将 action="/servleturl" method="get"
更改为 action="/servletdir/servleturl" method="get"
或 action="servleturl" method="get"
。
对于 action="/servleturl"
,您正在尝试访问根相对路径,但缺少上下文路径 servletdir
。
如果您使用 action="servleturl"
,则不需要将上下文路径放在 action
中,因为它将从当前的 url
中获取
我是 servlet 的初学者,对于 "hello world style" servlet,我安装了 apache tomcat 并在其 webapps
目录下创建了以下内容:
servletdir
|- index.xhtml
|- WEB-INF
|- web.xml
|- classes
|- mypackage
|- ServletClass.class
已初始化所有内容以便我可以在本地主机上访问我的 servlet:
web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>mypackage.ServletClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/servleturl/*</url-pattern>
</servlet-mapping>
</web-app>
并且 index.xhtml 包含一个具有 action="/servleturl" method="get"
理论上,当我启动 tomcat 时,我现在应该能够通过 localhost:8080/servletdir/index.xhtml
访问 servlet 和 index.xhtml 文件并在那里提交表单。达到索引没有问题,但是当我提交表单时,我收到 servlet 的 404 错误,说它 "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.".
我的 servlet class 应该不是问题,它扩展了 HttpServlet
并覆盖了 doGet
以通过 ServletResponse
参数的 getWriter().println()
。 servlet class 中的错误会导致 404 吗?是我的目录结构还是web.xml错了?
我尝试过多种目录结构和解决方案,包括:
- WEB-INF 中的扁平目录结构和 class 不在包中(没有 "classes" 目录)
- 没有 WEB-INF,一切都直接在 servlet 目录中
- 将 WEB-INF 和 index.xhtml 包装在名为 "WebContent" 的目录中
- 保留"classes"目录但不使用包,将class个文件直接放入classes*
我关注了多种资源,包括一些教程和此处的许多问题,它们与我的问题没有直接关系,但包含一些信息,但由于我是这项技术的新手,我无法推断出哪个资源是好的和不好的,这就是为什么我几乎尝试了我发现的所有东西。
上面描述的目录结构直接在磁盘上作为这个结构,而不是在 war 文件中,但由于我可以访问某些部分,我想这不是问题所在。
*:请注意,每次表示包的目录结构发生变化时,我都会在文件的第一行使用相应的 "package" 重新编译 java 文件,这不是问题
将 action="/servleturl" method="get"
更改为 action="/servletdir/servleturl" method="get"
或 action="servleturl" method="get"
。
对于 action="/servleturl"
,您正在尝试访问根相对路径,但缺少上下文路径 servletdir
。
如果您使用 action="servleturl"
,则不需要将上下文路径放在 action
中,因为它将从当前的 url