【问题标题】:problem when using servlet and web project with java使用 java 的 servlet 和 web 项目时出现问题
【发布时间】:2011-05-08 21:50:39
【问题描述】:

我刚刚开始使用 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 错误。

有什么我想念的吗?我无法弄清楚。请帮帮我。

【问题讨论】:

  • 能否分享一下错误代码/错误详情?
  • @Faisal : 当我使用上面提到的第二个 URL 时,我得到一些 HTTP 状态 404 错误

标签: java html tomcat servlets


【解决方案1】:

编辑:啊,是的,你需要使用 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

是的,如果您能向我们展示您遇到的错误,那就太好了..

【讨论】:

    【解决方案2】:

    网址应为http://localhost:8080/ServletProject/Main.html。其中ServletProject 是您的应用程序上下文。

    【讨论】:

    • 啊,你打败了我,但如果他在 Eclipse 中选择“右键单击 > 运行方式 > 在服务器上运行” - 它会打开具有正确 URL 的浏览器并正常工作。
    • @JoseK:是的。但我对 Eclipse 不是很熟悉。顺便说一句,我的赞成票在哪里;)。
    • 老兄,你打败了我的答案,你想让我奖励你 :) OP 还没有确认这有效,嗯
    • @JoseK:好的!这意味着你不确定你的答案,这可能会让你迟到;)
    • 不——我正在撰写一些关于“ServletProject”似乎是 Eclipse 在创建项目时将 Web 应用程序上下文自动填充到 web.xml 中的理论。那是我看到新答案的时候。
    猜你喜欢
    • 1970-01-01
    • 2011-08-17
    • 2019-11-19
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 2011-04-20
    相关资源
    最近更新 更多