com.sun.net.httpserver sınıfını kullanarak basit bir http sunucusu yazmaya çalışıyorum. Başlangıçta tarayıcıya html dosyası (index.html) gönderiyorum, ancak harici bir css dosyasının nasıl ekleneceğini bilmiyorum. Css kodu html dosyasının içine yerleştirildiğinde çalışır. Biliyorum, bu tarayıcı css dosyası için sunucu soran bir istek göndermeli, ancak bu isteği nasıl alacağından emin değilim ve bu dosyayı tarayıcıya geri göndereceğim. Yardımcı olabilseydim, kodumun bir parçasını eklerim.com.sun.net.httpserver kullanarak css dosyasını nasıl ekleyebilirim?
private void startServer()
{
try
{
server = HttpServer.create(new InetSocketAddress(8000), 0);
}
catch (IOException e)
{
System.err.println("Exception in class : " + e.getMessage());
}
server.createContext("/", new indexHandler());
server.setExecutor(null);
server.start();
}
private static class indexHandler implements HttpHandler
{
public void handle(HttpExchange httpExchange) throws IOException
{
Headers header = httpExchange.getResponseHeaders();
header.add("Content-Type", "text/html");
sendIndexFile(httpExchange);
}
}
static private void sendIndexFile(HttpExchange httpExchange) throws IOException
{
File indexFile = new File(getIndexFilePath());
byte [] indexFileByteArray = new byte[(int)indexFile.length()];
BufferedInputStream requestStream = new BufferedInputStream(new FileInputStream(indexFile));
requestStream.read(indexFileByteArray, 0, indexFileByteArray.length);
httpExchange.sendResponseHeaders(200, indexFile.length());
OutputStream responseStream = httpExchange.getResponseBody();
responseStream.write(indexFileByteArray, 0, indexFileByteArray.length);
responseStream.close();
}
bu kod satırı ne 'server.createContext ("/", yeni indexHandler());'? –
"/" yolu ile ilişkili bir http içeriği oluşturur. Bu yolun tüm istekleri indexHandler nesnesi tarafından ele alınır. – bizkhit
Bir HTTP sunucusu yazmak istiyorsanız, bir HTTP isteği ile yanıtı arasındaki ilişkinin nasıl olduğunu anlamanız gerekir. Size söylüyorum, bu bir öğretici olacaktır. – Raedwald