代码之家  ›  专栏  ›  技术社区  ›  M.J.

使用Java Servlet和Web项目时的问题

  •  1
  • M.J.  · 技术社区  · 14 年前

    我刚开始使用servlet。基本上,我是网络项目的新手。我使用Eclipse作为开发IDE。在其中,我创建了一个动态Web项目,在同一个项目中,我添加了一个HTML页面viz 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>
    

    我使用Tomcat6.0作为Web容器,在Eclipse中,我只集成了Tomcat,并在其中添加了Web项目。服务器正在正确启动。当我试图访问 Main.html page,浏览器上没有消息,在tomcat服务器配置中,有两个端口,一个是admin端口,另一个是html/1.1端口,值分别为8005和8080。当我使用 http://localhost:8005/Main.html 我没有得到任何一个网络浏览器,但当我使用 http://localhost:8080/Main.html 我收到一些Tomcat错误。

    我有什么东西不见了吗?我想不出来。请帮帮我。

    2 回复  |  直到 14 年前
        1
  •  3
  •   Prine    14 年前

    编辑:啊,是的,您需要使用servletproject作为URL模式。 尝试使用以下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
  •  2
  •   Adeel Ansari    14 年前

    URL应该是 http://localhost:8080/ServletProject/Main.html . 在哪里? ServletProject 是应用程序上下文。