TOP
SSV SOFTWARE SYSTEMS REGISTER  REGISTER
Log in to check your private messages  Log in to check your private messages
START FAQ SEARCH MITGLIEDER PROFILE  Log in 
SSV-Forum
Webserver in Java schreiben …

 
Post new topic   Reply to topic    SSV-Forum Forum Index >>> DNP/2486
<<< Previous topic - Next topic >>>  
Display posts from previous:   
Author Message
kdw



Joined: 05 May 2006
Posts: 1460

PostPosted: 14.09.2009, 09:24    Post subject: Webserver in Java schreiben … Reply with quote

Hallo Forum.

Im Java6-Paket com.sun.net.httpserver ist ein vollständiger Webserver enthalten, der in eigene Anwendungen eingebettet werden kann. Hier ein Beispiel:

Code:
import java.util.Date;
import java.io.OutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;

class DateHandler implements HttpHandler
{
   public void handle( HttpExchange httpExchange ) throws IOException
   {
      httpExchange.getResponseHeaders().add( "Content-type", "text/html" );
      String response = "<b>" + new Date() + "</b> for " +
                                                    httpExchange.getRequestURI();
      httpExchange.sendResponseHeaders( 200, response.length() );
      
      OutputStream os = httpExchange.getResponseBody();
      os.write( response.getBytes() );
      os.close();
   }
}

public class HttpServerDemo
{
   public static void main( String[] args ) throws IOException
   {
      HttpServer server = HttpServer.create( new InetSocketAddress( 8080 ), 0 );
      server.createContext( "/", new DateHandler() );
      System.out.println("Starting HTTP server at TCP port 8080 ...");
      server.start();
   }
}


In diesem Beispiel wird der Java-basierte Webserver mit dem TCP-Port 8080 gestartet. Unter dem Port 80 läuft ja bereits der Standard-Webserver des DNP/2486.

Gruß

KDW

P.S: Die Vorlage zu diesem Beispiel stammt aus dem Buch „Java ist auch eine Insel“ von Christian Ullenboom. Dieses Buch sollte jedem Java-Entwickler zur Verfügung stehen.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    SSV-Forum Forum Index >>> DNP/2486 All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

SSV SOFTWARE SYSTEMS

Dünenweg 5
30419 Hannover

Fon: +49(0)511  ·  40 000-0
Fax: +49(0)511  ·  40 000-40

sales@ssv-embedded.de


Impressum    ·    Datenschutz    ·    AGB

© 2024 SSV SOFTWARE SYSTEMS GmbH. Alle Rechte vorbehalten.

ISO 9001:2015