我刚刚开始使用 servlet。基本上我是网络项目的新手。我正在使用 eclipse 作为开发 IDE。因为我已经创建了一个动态的 web 项目,在同一时间我添加了一个 html 页面即 Main.html 具有以下 sript:_
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Main page</title>
</head>
<body>
<b>this is the first page</b>
</body>
</html>
下面是我的 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 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">
<display-name>ServletProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
我正在使用 tomcat 6.0 作为 web 容器,在 eclipse 中我只集成了 tomcat 并在其中添加了 web 项目。服务器正在正常启动。当我试图访问 Main.html
页面时,我在浏览器上没有得到任何东西,在 tomcat 服务器配置中,有两个端口,一个是管理端口,另一个是 HTML/1.1 端口值分别为 8005 和 8080。当我使用 http://localhost:8005/Main.html
时,我在网络浏览器中一无所获,但是当我使用 http://localhost:8080/Main.html
我收到一些 tomcat 错误。
有什么我想念的吗?我想不通。请帮助我。
最佳答案
编辑:啊是的,您需要使用 ServletProject 作为 url-pattern.. 尝试使用以下 URL 访问该文件:http://localhost:8080/ServletProject/Main.html
您需要在 web.xml 文件中正确配置您的应用程序,如果您想稍后使用 Java 类并将其用作 Servlet,您必须在 web.xml 中这样定义它:
<servlet>
<servlet-name>CurrencyConverter</servlet-name>
<servlet-class>servlets.Converter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CurrencyConverter</servlet-name>
<url-pattern>/convert</url-pattern>
</servlet-mapping>
url 模式定义了您以后可以访问它的路径。要访问上面代码中的 CurrencyConverter,您需要使用此 url:http://localhost:8080/convert
是的,如果您能向我们展示您遇到的错误,那就太好了..
关于java - 使用 java 使用 servlet 和 web 项目时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4162163/