Where is the HTTP Server library for Java JDK?

Viewed 646

I wanted to run a basic HTTP server in Java SE on windows using eclipse. Googling about it made me come across multiple tutorials for it.

Nearly all of them claimed to be using inbuilt HTTP server class from the package com.sun.net.httpserver

But when I tried to browse it in eclipse , the import came in empty! the import path is there but there are no classes.

I've tried both openJDK 14 and oracle JDK 14 neither seems to have that HttpServer class and none of the tutorials tell me where is the library for it.

What am I missing?

2 Answers

I am not sure if it is a problem with your IDE. I just wrote the following code:

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

and when click Open Declaration [Command + Mouseover on HttpServer], I can browse to the following:

enter image description here

You can use HTTP servlet api for that porpose

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

And you can make an web.xml that points to your servlet out of this api

<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>

and create a class that exteends from HttpServlet. e.g.

public class <ServletName> extends HttpServlet {

and implement common functions of HttpServlet class protected void doPost and protected void doGet

these functions will behave differently on different methods.

And finnaly in your pom.xml add the maven war compiler plugin

<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>

then if you are using Eclipse you can start local server and move the created war file to

tomcat/webapps

and then restrat the tomcat. Now if you navigate to localhost you can see the deployed web application

Note: If your server running on Linux, tomcat is usually intalled in `

/usr/local/tomcat

directory. To deploy the war file you will need to copy your war file to /usr/local/tomcat and restart the tomcat using /usr/local/tomcat/bin/shutdown.sh to stop tomcat service and /usr/local/tomcat/bin/startup.sh to start it.

Hope it will help. Regards, Erik

Related