Create a simple HTTP server with Java?

What is the easiest way to create a simple HTTP server with Java? Are there any libraries to facilitate this? I only need to respond to GET/POST , and I cannot use the application server.

What is the easiest way to accomplish this?

+64
java post get
Apr 26 2018-10-22T00:
source share
11 answers

Use Jetty . Here is a tutorial for embedding Jetty . (This is an outdated textbook .)

Jetty is fairly lightweight, but it provides a servlet container, which may run counter to your requirements against using an "application server".

You can embed the Jetty server in your application. Jetty allows you to use container options with a built-in OR servlet.

+39
Apr 26 2018-10-22T00:
source

Here is how I will do it:

  • Start listening to ServerSocket (possibly on port 80).
  • As soon as you receive a connection request, accept and transfer another thread / process (this leaves ServerSocket available for listening and accepting other connections).
  • Parse the request text (in particular, the headers where you will see if it is GET or POST, and the parameters passed.
  • Respond with your own headers ( Content-Type , etc.) and HTML.

It is useful for me to use Firebug (in Firefox) to see example headers. This is what you want to imitate.

Try this link: - Multithreaded server in Java

+25
Apr 26 2018-10-22T00:
source

The easiest Simple is a tutorial, without WEB-INF, and not any dependencies from the servlet API. Just a simple easy HTTP server in one JAR.

+21
Apr 26 2018-10-22T00:
source

If you use Sun JDK, you can use this built-in library
Take a look at this site on how to use it.

If n ot there are several open source HTTP servers here that you can embed in your software.

+7
Apr 26 '10 at 10:15
source

Java 6 has an integrated HTTP server by default.

Check here thread

By the way, if you plan to have a web service for recreation, here is a simple example using knitwear .

+4
Jul 12 '13 at 19:18
source

I am surprised that this example is'nt here:

http://hc.apache.org/httpcomponents-core-ga/httpcore/examples/org/apache/http/examples/ElementalHttpServer.java

EDIT → The above link is not available. Here is an excerpt from the POST example, followed by a link to HTTP examples.

  if (!conn.isOpen()) { Socket socket = new Socket(host.getHostName(), host.getPort()); conn.bind(socket); } BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/servlets‐examples/servlet/RequestInfoExample"); request.setEntity(requestBodies[i]); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); httpexecutor.preProcess(request, httpproc, coreContext); HttpResponse response = httpexecutor.execute(request, conn, coreContext); httpexecutor.postProcess(response, httpproc, coreContext); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity())); System.out.println("=============="); if (!connStrategy.keepAlive(response, coreContext)) { conn.close(); } else { System.out.println("Connection kept alive..."); } 

http://hc.apache.org/httpcomponents-core-ga/httpcore/examples/org/apache/http/examples/

+4
Sep 11 '13 at 16:35
source

I wrote a tutorial explaining how to write a simple HTTP server some time ago in Java. Explains what the code does and why the server is written in the way the tutorial goes. May be useful http://kcd.sytes.net/articles/simple_web_server.php

+3
Mar 19 '12 at 20:59
source

Jetty is a great way to easily embed an HTTP server. It supports its own easy way to bind handlers and is a complete J2EE application server if you need more features.

+1
Apr 26 2018-10-28T00:
source

Embedding Tomcat is relatively painless as such things go. Here is a good StackOverflow link about this.

+1
Apr 26 '10 at 10:18
source

A servlet container is definitely the way to go. If Tomcat or Jetty is too heavy for you, consider Winstone or TTiny .

0
Apr 26 '10 at 10:22
source

I just added a public repo with a server ready to start using Jetty and JDBC to start your project.

Get it from github here: https://github.com/waf04/WAF-Simple-JAVA-HTTP-MYSQL-Server.git

0
Oct. 15 '13 at 2:21
source



All Articles