Simple Http Server : Server « Network Protocol « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Network Protocol » Server 




Simple Http Server
        
/* $Id: SimpleHttpServer.java 124 2010-09-23 16:25:36Z [email protected] $ */

//package djudge.utils;

import java.net.ServerSocket;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;


interface SimpleHttpServerDataProvider
{
  public String getHtmlPage(String query);
}

public class SimpleHttpServer
{

  final SimpleHttpServerDataProvider dataProvider;
  
  public SimpleHttpServer(SimpleHttpServerDataProvider dataProvider, int port)
  {
    
    class SocketProcessor implements Runnable
    {
      private Socket s;
      private InputStream is;
      private OutputStream os;
      private SimpleHttpServerDataProvider dataProvider;

      private SocketProcessor(Socket s, SimpleHttpServerDataProvider provthrows Throwable
      {
        this.dataProvider = prov;
        this.s = s;
        this.is = s.getInputStream();
        this.os = s.getOutputStream();
      }

      public void run()
      {
        try
        {
          readInputHeaders();
          writeResponse("");
        catch (Throwable t)
        {
          /*do nothing*/
        finally
        {
          try
          {
            s.close();
          catch (Throwable t)
          {
            /*do nothing*/
          }
        }
        
      }

      private void writeResponse(String sthrows Throwable
      {
        String response = "HTTP/1.1 200 OK\r\n"
            "Server: DJudge.http\r\n"
            "Content-Type: text/html\r\n" "Content-Length: "
            + s.length() "\r\n" "Connection: close\r\n\r\n";
        String result = response + dataProvider.getHtmlPage("");
        os.write(result.getBytes());
        os.flush();
      }

      private void readInputHeaders() throws Throwable
      {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        while (true)
        {
          String s = br.readLine();
          if (s == null || s.trim().length() == 0)
          {
            break;
          }
          
        }
      }
    }
    
    
    this.dataProvider = dataProvider;
    try
    {
        ServerSocket ss = new ServerSocket(port);
        
        while (true)
        {
          Socket s = ss.accept();
          
          new Thread(new SocketProcessor(s, dataProvider)).start();
        }
    }
    catch (Exception e)
    {
      
    }
    catch (Throwable e)
    {
      
    }
  }
  
}

   
    
    
    
    
    
    
    
  














Related examples in the same category
1.A generic framework for a flexible, multi-threaded server
2.Server allows connections on socket 6123Server allows connections on socket 6123
3.This server displays messages to a single clientThis server displays messages to a single client
4.The client can specify information to control the output of a serverThe client can specify information to control the output of a server
5.A server can use specialized streams to deliver typed dataA server can use specialized streams to deliver typed data
6.Serve entire objects using ObjectOutputStreamServe entire objects using ObjectOutputStream
7.A multithreaded serverA multithreaded server
8.Base class to build multithreaded servers easily
9.Manage a pool of threads for clients
10.Client estimates the speed of the network connection to the server
11.This server retrieves the time using the RFC867 protocol.This server retrieves the time using the RFC867 protocol.
12.Quote Server
13.Logging Server based on SocketServer
14.Client and Server Demo
15.Reflector
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.