Java Blog

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

class HttpRequest
{
    private Socket ClientConn;
    public HttpRequest(Socket ClientConn) throws Exception
    {
        this.ClientConn=ClientConn;

    }
    public void process() throws Exception
    {
        DataInputStream din=new DataInputStream(ClientConn.getInputStream());

        OutputStream ot=ClientConn.getOutputStream();
        BufferedOutputStream out=new BufferedOutputStream(ot);

        String request=din.readLine().trim();
        System.out.println(request);
        StringTokenizer st=new StringTokenizer(request);

        String header=st.nextToken();

        if(header.equals(\"GET\"))
        {
            String fileName=st.nextToken();
            FileInputStream fin=null;
            boolean fileExist=true;
            try
            {
                fin=new FileInputStream(fileName);
            }
            catch(Exception ex)
            {
                fileExist=false;
            }

            String ServerLine=\"Simple HTTP Server\";
            String StatusLine=null;
            String ContentTypeLine=null;
            String ContentLengthLine=null;
            String ContentBody=null;

            if(fileExist)
            {
                StatusLine=\"HTTP/1.0 200 OK\";
                ContentTypeLine=\"Content-type: text/html\";
                ContentLengthLine=\"Content-Length: \"+ (new Integer(fin.available()).toString());                
            }
            else
            {
                StatusLine = \"HTTP/1.0 200 OK\";
                ContentTypeLine=\"Content-type: text/html\";
                ContentBody = \"<HTML>\" + 
                                \"<HEAD><TITLE>404 Not Found</TITLE></HEAD>\" +
                            \"<BODY>404 Not Found\" +                                               
                                \"</BODY></HTML>\" ;
                ContentLengthLine=(new Integer(ContentBody.length()).toString());
            }

            out.write(StatusLine.getBytes());
            out.write( ServerLine.getBytes());
            out.write(ContentTypeLine.getBytes());
            out.write( ContentLengthLine.getBytes());

            if(fileExist)    
            {

                byte[] buffer = new byte[1024] ;
                int bytes = 0 ;
                while ((bytes = fin.read(buffer)) != -1 ) 
                    {
                    out.write(buffer, 0, bytes);
                    for(int iCount=0;iCount<bytes;iCount++)
                    {
                        int temp=buffer[iCount];
                        System.out.print((char)temp);
                    }
                }    

                fin.close();
            }
            else
            {
                out.write(ContentBody.getBytes());
            }

                            out.close();
                                ClientConn.close();


        }

    }
}
class HttpServer
{
    public static void main(String args[]) throws Exception
    {
        ServerSocket soc=new ServerSocket(5217);
        while(true)
        {
            Socket inSoc=soc.accept();
            HttpRequest request=new HttpRequest(inSoc);
            request.process();
        }


    }
}

    Related Post:
  1. Program to show the use of some Character Methods

  2. Program, which creates a three thread. One thread display the numbers from 1 to 5, second thread display the square root of that number etc

  3. Program of an applet to display alternate filled and empty circle

  4. An applet program to perform selection sort

  5. An applet program to find maximum of three numbers using textField

  6. Simple Applet which will show life cycle of an Applet

  7. Program to show an example of creating String objects from Character Arrays

  8. Program to show an example of using a Non-Static Nested Class

  9. Program which creates an Array of character. Make one function with one argument as a character and that function throw a user defined exception

  10. Develop a CORBA application that takes a string from client and returns whether it is palindrome

  11. Program to display Floyds Triangle of a numbers

  12. Program of concatenating and Buffering files

  13. An applet program that displays random dot

  14. Program to perform Merge Sort

  15. Program to read the ballots and count the votes cast for each candidate using an array variable count

  16. Program in CORBA which takes a long sentence from the client (at least 7 words), passes to the server, server separates out each word

  17. Corba program to encrypt the text that you want to send on the client side. The server side decrypts it after receiving. Append server time.

  18. Program to search an element in an array using Linear Search

  19. Socket program of HTTP Server

  20. Program to show insertion of StringBuffer, String objects and other Basic Data Types into a StringBuffer object

 
 
Didn't find what you were looking for? Find more on Socket program of HTTP Server