SimpleServer.java



package xmlshare;

import java.io.*;
import java.net.*;
import java.util.*;

/** ver simple server */
public class SimpleServer {
	
   /** port number */
    public static final int PORT=8080;
	
   /** Source File */
    private File sourceFile;

   /** public directory */
    private String prefix;

   /** constructor */
    public SimpleServer() {
        prefix = "public" + File.separator;
    }

   /** receive request from client */
    public void getRequest( BufferedReader in ) {

       String message = null;
       try {
           String first = in.readLine();
           StringTokenizer st = new StringTokenizer( first );
           String method = st.nextToken();
           String url = st.nextToken();
           StringTokenizer st2 =new StringTokenizer( url, "/");
           String fileName = null;
           while( st2.hasMoreTokens() ){
                   fileName = st2.nextToken();
           }
           if( fileName == null || fileName.equals( "" ) )
               fileName = "list.xml";
           sourceFile = new File( prefix + fileName );

           System.out.println( method + ":" + url );
        }
	catch( Exception e ){
             System.err.println( e.toString() );
        }
    }

  /* send response */
    public void putResponse( OutputStreamWriter out,
                             BufferedReader source ) {
         try {
             String status = "HTTP/1.0 200 Document follows"+ '\r' + '\n';
             out.write( status, 0, status.length() );
             String header = "Content-Type: text/plain" +'\r' + '\n';
             out.write( header, 0, header.length() );
             String emptyLine = "" + '\r'+ '\n';
             out.write( emptyLine, 0, emptyLine.length() );

             StringBuffer sb = new StringBuffer( "" );
             String line = null;
             while( (line = source.readLine()) != null ){
                 sb.append( line + '\n' );
             }
             String message = sb.toString();
             out.write( message, 0, message.length() );
             out.flush();
         }
         catch( Exception e ) {
             System.err.println( e.toString() );
         }
    }

  /** loop */
    public void loop(){
        try{
            ServerSocket ss = new ServerSocket( PORT );
            while( true ){
                Socket sock = ss.accept();
                BufferedReader in
                   = new BufferedReader(
                       new InputStreamReader(
                          sock.getInputStream() ) );
                getRequest( in );

                OutputStreamWriter out
                   = new OutputStreamWriter(
                        sock.getOutputStream() );
                BufferedReader source
                   = new BufferedReader(
                        new FileReader( sourceFile ) );
                putResponse( out, source );
                source.close();
                sock.close();
            }
        }
        catch( Exception e ){
             System.err.println( e.toString() );
        }
    }
	
  /** start here */
    public static void main( String[] argv ){
        try {
            SimpleServer server = new SimpleServer();
            server.loop();
        }
        catch( Exception e ){
             System.err.println( e.toString() );
        }
    }
}