Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: One-Shot-HTTP-Server

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Location
    Germany
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default One-Shot-HTTP-Server

    Hey guys,

    I have just finished my first of three years in an informatics apprenticeship and I thought it would be a good idea to do something during the holidays to improve my Java and Networking skills. Thats why I started to read a book about Java Servers and Servlets, but unfortunately I'm already having some trouble. Right in the beginning of the book, there is a programme, in which a self created HTTP-Server should respond to a GET-request and it just doesnt want to work for me as it should.

    When I'm starting the programme and enter http://localhost:8080/index.html (they say you should create an index.html somewhere in your workspace, so the server has something to respond) the only output that I get in my eclipse is:

    "Request: GET / HTTP/1.1
    404 Not Found"

    Ok. So the server is at least getting the request, which can be seen in the first line "Request: GET / HTTP/1.1". But I have really no idea what I should do, to make the server respond to the request by showing the index.html, which is how its meant to work.

    I'm still a newbie with all that stuff so I hope there are some nice guys out there who can share some of their experience with me, cause I'm really not finding an answer for that problem by myself.

    Btw here's the code for that programme:

    package httpServer;

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

    public class OneShotHttpd{

    public final static int HTTP_PORT = 8080;

    public static void main(String[] args){
    try{
    ServerSocket listen = new ServerSocket(HTTP_PORT);
    Socket client = listen.accept();
    BufferedReader is = new BufferedReader(new InputStreamReader(client.getInputStream()));
    DataOutputStream os = new DataOutputStream(client.getOutputStream());
    String request = is.readLine();
    System.out.println("Request: " + request);
    StringTokenizer st = new StringTokenizer(request);
    if((st.countTokens()==3) && st.nextToken().equals("GET")){
    request = st.nextToken().substring(1);
    if(request.endsWith("/") || request.equals(""))
    request += "index.html";
    sendDocument(os, request);
    }
    else
    System.err.println("400 Bad Request");
    is.close();
    os.close();
    client.close();
    }
    catch(IOException ioe){
    System.err.println("Fehler: " + ioe.toString());
    }
    }
    public static void sendDocument(DataOutputStream out, String file) throws IOException{
    try{
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
    byte[] buf = new byte[1024];
    int len;
    while((len = in.read(buf, 0, 1024)) != -1){
    out.write(buf, 0, len);
    }
    in.close();
    }
    catch(FileNotFoundException fnfe){
    System.err.println("404 Not Found");
    }
    }
    }
    Thanks already for all your efforts!


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: One-Shot-HTTP-Server

    Quote Originally Posted by v1per1987 View Post
    Hey guys
    Right backatcha, viper: Hey, from Zaphod.


    Quote Originally Posted by v1per1987 View Post

    When I'm starting the programme and enter http://localhost:8080/index.html (they say you should create an index.html somewhere in your workspace, so the server has something to respond) the only output that I get in my eclipse is:
    Well, when I see that some one is using an IDE without having worked with this particular stuff, I'm thinking that I may not be able to help much. But it's an interesting project, so I'll give it a shot...

    First of all: Let's avoid the problems that I have always had trying to troubleshoot IDE-based projects by remote control. Maybe some Eclipse expert can help with the Eclipse part of it, but leave me out of it.

    You didn't mention what Operating System you are using, and with networking stuff that might be critical. Maybe not so much for a real simple thing like this, but if we have to get into this much deeper, well I just gotta know.

    Anyhow, for my Linux distro (Centos 5.8 for the machine within easy grasp) and for my Windows XP workstation (some day I'll take it out and let the local gun club use if for target practice, but for now there are a couple of applications that make me keep it around), here's what I did:

    1. From a command shell I navigated to a suitable place (/home/zaphod/java on the Linux box and f:\home\zaphod\java on the Windows box) and created a text file named OneShotHttpd.java. I pasted your example code directly from your post.

    2. I executed javac OneShotHttpd.java to get the OneShotHttpd.class ready for action.

    3. I created a file named index.html in that same directory. If I'm reading your example correctly, it doesn't really do much html stuff; it just spits out the text. I may want to use this as a "real" html file some day, so I entered the following into index.html:
    <html>Hello, Earthlings, from Zaphod!</html>

    Then I executed "java OneShotHttpd"

    I opened a browser (Firefox) on the same computer and entered the following in the url window:

    localhost:8080

    Try it and tell us what happens now.

    Notes:
    1. If you run it more than once (and you will, whether it works or not) you might have to clear your browser cache between runs. After you have been burned a few times running it and seeing no changes in what you thought should have been changed, you may remember this. Once (or twice) you have wasted time figuring out "why things didn't change," you might remember it more quickly next time

    2. I was already running Tomcat (it uses port 8080) on my Linux box, and I didn't want to kill it just for this little test, so I changed the HTTP_PORT definition in OneShotHttpd.java to 8081 and entered "localhost:8081" in the browser window.

    3. When you get ready to set up a "real" server, there will be a little more work in defining paths on your server machine. For example, apache configurations are almost always set up to use /var/www/html as the primary search path, but nothing about this is automatic. The server program itself creates search paths from a configuration file that is read when the server is launched. Maybe your reference material will cover things like this, but for now, keep everything out of "system" directories and have fun with your local versions! Just put your html files in the same directory from which you launch your server test programs.

    4. I never post things like this unless I have tested them, but that doesn't mean that I can guarantee that they will work for everybody. There are just too dang many things that might be involved that I simply can't troubleshoot by remote control (iptables firewall stuff, selinux, apache or other httpd already running and set up to intercept stuff like these requests, etc., etc., etc.)



    Bottom line: IWFMYMMV! (It Works For Me; Your Mileage May Vary)
    (And it's fun when it actually works!)


    Cheers!

    Z
    Last edited by Zaphod_b; July 24th, 2012 at 04:03 PM.

  3. The Following User Says Thank You to Zaphod_b For This Useful Post:

    v1per1987 (July 24th, 2012)

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Location
    Germany
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: One-Shot-HTTP-Server

    Thank you so much for your reply Zaphod!

    You're absolutely right. The problem was that I tried to do the job with Eclipse. The book that I'm reading is already about 10 years old, and the writers maybe didn't think too much about the problems that people would have when they try to start the programme with their IDEs at that time. I'm also still using Win XP btw. It still works fine for me, so I guess I'm gonna keep it for another while.

    Alright. So I put the .java file in a folder together with the index.html, compiled and launched the .class file and entered localhost:8080 in my browser... et voilą:

    "Request: GET / HTTP/1.1
    index.html"

    shows up in my DOS window and the text of the index.html is written in my browser!

    Thanks for your detailed answer and the additional information that you gave me already! That was huge!

    I'm happy that the programme works and I'm looking forward to continue and try out the extended versions of the HTTP-Server which are coming next in that book now. There are 2 more versions which implement some more functions, like more security with the folder access and header-data. I'll try them out and after that there comes a big chapter about servlets. I don't know yet if I will ever need servlets, but I think I can only profit by learning more about things like http, internet protocols, html, xml and all that stuff that come along with the servlets.

    Cheers!

    v1per1987
    Last edited by v1per1987; July 24th, 2012 at 04:25 PM.

  5. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: One-Shot-HTTP-Server

    Quote Originally Posted by v1per1987 View Post
    ...
    "Request: GET / HTTP/1.1
    index.html"

    shows up in my DOS window and the text of the index.html is written in my browser!
    Huzzah!

    The reason that I went into such excruciating detail is that I think the very first one is the hardest, and I tried to leave nothing to chance.

    Once you know that things like this can work, and you know how to test them, then, well there's simply no stopping you, right?

    Onward and upward!!!


    Cheers!

    Z

  6. The Following User Says Thank You to Zaphod_b For This Useful Post:

    v1per1987 (July 24th, 2012)

Similar Threads

  1. http client
    By ilearn-computer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 06:47 AM
  2. HTTP Status 404
    By Star_pro in forum Java Servlet
    Replies: 0
    Last Post: February 28th, 2011, 01:17 PM
  3. Replies: 6
    Last Post: September 19th, 2010, 08:33 PM
  4. HTTP Status 500 -
    By mqt in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 6th, 2010, 01:09 AM
  5. Replies: 1
    Last Post: March 31st, 2010, 09:42 PM