Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since Java SE 6, there's a builtin HTTP server in <strike>Sun</strike> Oracle JRE. The <a href="http://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/package-summary.html" rel="noreferrer"><code>com.sun.net.httpserver</code> package summary</a> outlines the involved classes and contains examples.</p> <p>Here's a kickoff example copypasted from their docs, you can just copy'n'paste'n'run it on Java 6+.</p> <blockquote> <pre><code>package com.stackoverflow.q3732109; import java.io.IOException; import java.io.OutputStream; import java.net.InetSocketAddress; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; public class Test { public static void main(String[] args) throws Exception { HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0); server.createContext("/test", new MyHandler()); server.setExecutor(null); // creates a default executor server.start(); } static class MyHandler implements HttpHandler { @Override public void handle(HttpExchange t) throws IOException { String response = "This is the response"; t.sendResponseHeaders(200, response.length()); OutputStream os = t.getResponseBody(); os.write(response.getBytes()); os.close(); } } } </code></pre> </blockquote> <p>Noted should be that the <code>response.length()</code> part in their example is bad, it should have been <code>response.getBytes().length</code>. Even then, the <code>getBytes()</code> method must explicitly specify the charset which you then specify in the response header. Alas, albeit misguiding to starters, it's after all just a basic kickoff example.</p> <p>Execute it and go to <a href="http://localhost:8000/test" rel="noreferrer">http://localhost:8000/test</a> and you'll see the following response:</p> <blockquote> <p>This is the response</p> </blockquote> <hr> <p>As to using <code>com.sun.*</code> classes, do note that this is, in contrary to what some developers think, absolutely not forbidden by the well known FAQ <a href="http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html" rel="noreferrer">Why Developers Should Not Write Programs That Call 'sun' Packages</a>. That FAQ concerns the <code>sun.*</code> package (such as <code>sun.misc.BASE64Encoder</code>) for internal usage by the Oracle JRE (which would thus kill your application when you run it on a different JRE), not the <code>com.sun.*</code> package. Sun/Oracle also just develop software on top of the Java SE API themselves like as every other company such as Apache and so on. Using <code>com.sun.*</code> classes is only discouraged (but not forbidden) when it concerns an <strong>implementation</strong> of a certain Java API, such as GlassFish (Java EE impl), Mojarra (JSF impl), Jersey (JAX-RS impl), etc.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload