tomcat - 请求的资源未找到错误
tomcat - requested resource not found error
我是 Java servlet 之类的新手。我 运行 Eclipse Luna SR2(用于 Java EE)并将我的项目配置为使用 tomcat 8 作为服务器(在 Linux Mint 17.1 上,如果有帮助的话)。
我从文件 -> 新建 -> 动态 Web 项目创建了一个新项目。
添加 tomcat 8 服务器时,除了指定 tomcat 根目录外,我没有更改任何默认参数。
然后我导入了 servlet-api.jar 文件(来自 tomcat/lib 目录)。
我的项目中有一个 class 文件没有任何错误。因此,当我从 Eclipse 中 运行 tomcat 时,我得到 The requested resource is not available.
。我知道这是一个非常常见的错误,我很乐意提供所需的信息。
这里是 class 文件 -
package ch1;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class serv1 {
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>");
}
}
这是我项目的目录结构-
.
|-- build
| `-- classes
| `-- ch1
| `-- serv1.class
|-- .classpath
|-- .project
|-- .settings
| |-- .jsdtscope
| |-- org.eclipse.jdt.core.prefs
| |-- org.eclipse.wst.common.component
| |-- org.eclipse.wst.common.project.facet.core.xml
| |-- org.eclipse.wst.jsdt.ui.superType.container
| `-- org.eclipse.wst.jsdt.ui.superType.name
|-- src
| `-- ch1
| `-- serv1.java
`-- WebContent
|-- META-INF
| `-- MANIFEST.MF
`-- WEB-INF
`-- lib
编辑:这是截图 -
谁能帮我看看我哪里做错了?
您需要将 web.xml 文件放在 WEB-INF 目录中。如果不从 web.xml 文件
进行映射,Tomcat 对您的 servlet 一无所知
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- Name the application -->
<display-name>ch1</display-name>
<description>An example application which is used to play with some of the features of Tomcat</description>
<!-- ========================================================== -->
<!-- Servlets -->
<!-- ========================================================== -->
<!-- Simple Servlet, provide a name, class, description and map to URL /servlet/SimpleServlet -->
<servlet>
<servlet-name>Simple</servlet-name>
<servlet-class>ch1.serv1</servlet-class>
<description>This is a simple Hello World servlet</description>
</servlet>
<servlet-mapping>
<servlet-name>Simple</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
放入
<servlet-class>ch1.serv1</servlet-class>
全 class 包名
您还需要从 HttpServlet 扩展 class,然后覆盖 #doGet() 方法。
然后尝试通过 url http://localhost:8080/ch1
获取您的页面
我是 Java servlet 之类的新手。我 运行 Eclipse Luna SR2(用于 Java EE)并将我的项目配置为使用 tomcat 8 作为服务器(在 Linux Mint 17.1 上,如果有帮助的话)。
我从文件 -> 新建 -> 动态 Web 项目创建了一个新项目。
添加 tomcat 8 服务器时,除了指定 tomcat 根目录外,我没有更改任何默认参数。
然后我导入了 servlet-api.jar 文件(来自 tomcat/lib 目录)。
我的项目中有一个 class 文件没有任何错误。因此,当我从 Eclipse 中 运行 tomcat 时,我得到 The requested resource is not available.
。我知道这是一个非常常见的错误,我很乐意提供所需的信息。
这里是 class 文件 -
package ch1;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class serv1 {
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>");
}
}
这是我项目的目录结构-
.
|-- build
| `-- classes
| `-- ch1
| `-- serv1.class
|-- .classpath
|-- .project
|-- .settings
| |-- .jsdtscope
| |-- org.eclipse.jdt.core.prefs
| |-- org.eclipse.wst.common.component
| |-- org.eclipse.wst.common.project.facet.core.xml
| |-- org.eclipse.wst.jsdt.ui.superType.container
| `-- org.eclipse.wst.jsdt.ui.superType.name
|-- src
| `-- ch1
| `-- serv1.java
`-- WebContent
|-- META-INF
| `-- MANIFEST.MF
`-- WEB-INF
`-- lib
编辑:这是截图 -
谁能帮我看看我哪里做错了?
您需要将 web.xml 文件放在 WEB-INF 目录中。如果不从 web.xml 文件
进行映射,Tomcat 对您的 servlet 一无所知<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- Name the application -->
<display-name>ch1</display-name>
<description>An example application which is used to play with some of the features of Tomcat</description>
<!-- ========================================================== -->
<!-- Servlets -->
<!-- ========================================================== -->
<!-- Simple Servlet, provide a name, class, description and map to URL /servlet/SimpleServlet -->
<servlet>
<servlet-name>Simple</servlet-name>
<servlet-class>ch1.serv1</servlet-class>
<description>This is a simple Hello World servlet</description>
</servlet>
<servlet-mapping>
<servlet-name>Simple</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
放入
<servlet-class>ch1.serv1</servlet-class>
全 class 包名
您还需要从 HttpServlet 扩展 class,然后覆盖 #doGet() 方法。
然后尝试通过 url http://localhost:8080/ch1
获取您的页面