【问题标题】:Where is the HTTP Server library for Java JDK?Java JDK 的 HTTP Server 库在哪里?
【发布时间】:2020-06-22 05:59:59
【问题描述】:

我想在 Windows 上使用 eclipse 在 Java SE 中运行一个基本的 HTTP 服务器。谷歌搜索让我发现了多个教程。

几乎所有人都声称使用包 com.sun.net.httpserver 中的内置 HTTP 服务器类

但是当我尝试在 eclipse 中浏览它时,导入是空的!导入路径在那里,但没有类。

我已经尝试过 openJDK 14 和 oracle JDK 14 似乎都没有 HttpServer 类,并且没有任何教程告诉我它的库在哪里。

我错过了什么?

【问题讨论】:

    标签: java eclipse http server


    【解决方案1】:

    我不确定这是否是您的 IDE 的问题。我刚刚写了以下代码:

    import com.sun.net.httpserver.HttpServer;
    public class Main {
        public static void main(String[] args) {
            HttpServer httpServer;
        }
    }
    

    当点击 Open Declaration [Command + Mouseover on HttpServer] 时,我可以浏览到以下内容:

    【讨论】:

      【解决方案2】:

      你可以使用 HTTP servlet api 来处理这个任务

      <dependency>
              <groupId>org.apache.tomcat</groupId>
              <artifactId>servlet-api</artifactId>
              <version>6.0.53</version>
      </dependency>
      

      你可以用这个api创建一个指向你的servlet的web.xml

      <servlet>
          <servlet-name> <ServletName> </servlet-name>
          <servlet-class><package>.<ServletName></servlet-class>
        </servlet>
        <servlet-mapping>
          <servlet-name><ServletName></servlet-name>
          <url-pattern>/<ServletName></url-pattern>
        </servlet-mapping>
      

      并创建一个从 HttpServlet 扩展的类。例如

      public class <ServletName> extends HttpServlet {
      

      并实现HttpServletprotected void doPost和protected void doGet的常用功能

      这些函数在不同的方法上会有不同的表现。

      最后在你的 pom.xml 中添加 maven war 编译器插件

      <plugins>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-compiler-plugin</artifactId>
                      <version>3.8.0</version>
                      <configuration>
                          <source>1.8</source>
                          <target>1.8</target>
                      </configuration>
                  </plugin>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-war-plugin</artifactId>
                      <version>3.2.3</version>
                      <configuration>
                          <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
                          <webResources>
                              <resource>
                                  <directory>${project.build.sourceDirectory}/main/java</directory>
                                  <excludes>
                                      <exclude>
                                          **/*.java
                                      </exclude>
                                  </excludes>
                                  <targetPath>WEB-INF/classes</targetPath>
                              </resource>
                          </webResources>
                          <archive>
                      </configuration>
                  </plugin>
              </plugins>
      

      如果您使用的是 Eclipse,您可以启动本地服务器并将创建的 war 文件移动到

      tomcat/webapps

      然后重新启动 tomcat。现在,如果您导航到 localhost,您可以看到已部署的 Web 应用程序

      注意:如果你的服务器运行在Linux上,tomcat通常安装在`

      /usr/local/tomcat

      目录。要部署战争文件,您需要将战争文件复制到 /usr/local/tomcat 并使用 /usr/local/tomcat/bin/shutdown.sh 重新启动 tomcat 以停止 tomcat 服务和 /usr/local/tomcat/bin /startup.sh 启动它。

      希望它会有所帮助。问候,埃里克

      【讨论】:

      • 这只是指出了一个外部依赖。虽然它仍然可能有用,但并不能真正回答问题。
      • 2020 年的 tomcat 6.0.53 参考?
      猜你喜欢
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      • 2019-03-30
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多