Results 1 to 10 of 10

Thread: Java webserver.

  1. #1
    whiteface is offline Member
    Join Date
    Sep 2012
    Location
    Northen Sweden
    Posts
    5
    Rep Power
    0

    Unhappy Java webserver.

    Hello!

    I'm writing a webserver in java and i have encountered a problem, the webserver works when i run it in netbeans, but when i make it into a .jar file and run it it doesn't work!? the resources is inside of the .jar file (/pytteweb/resources/) and the program is called pytteweb, what am i doing wrong? The code to locate the resources is:

    Java Code:
    String path = null;
            URL url = this.getClass().getResource("/pytteweb/resources/");
            try {
                path = url.getPath();
            } catch (Exception e) {
            }
    the path is when the .jar file is run: file:/C:/Users/Michael/Documents/Skola/2011/Datakommunikation/webserver_test/pytteweb/dist/pytteweb.jar!/pytteweb/resources/
    the path is when it's run in netbeans: /C:/Users/Michael/Documents/Skola/2011/Datakommunikation/webserver_test/pytteweb/build/classes/pytteweb/resources/

    please help me, i've been working with this for weeks now!!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: Java webserver.

    What exception are you getting?
    What are you trying to do when you get that exception?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    whiteface is offline Member
    Join Date
    Sep 2012
    Location
    Northen Sweden
    Posts
    5
    Rep Power
    0

    Default Re: Java webserver.

    Hello and thanks for responding!

    I'm not getting any exeptiosn at all!? but when i run the program as a .jar file the server isn't loading any pages, as it does when i run it in netbeans!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: Java webserver.

    How do you know?
    Remember, we haven't a clue what this is supposed to be doing.
    You;re question is essentially "it doesn't work", but you haven't explained what it should do.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    whiteface is offline Member
    Join Date
    Sep 2012
    Location
    Northen Sweden
    Posts
    5
    Rep Power
    0

    Default Re: Java webserver.

    Ah! Because i check the path variable with a JFrame in different parts of the program. Ok, the part of the program that i belive is not working is supposed to locate the resources folder inside the pytteweb.jar file, so the pytteweb webserver can load the files from ~/pytteweb/resources/ and using a DataOutputStream display them on the clients webclient. This works when the program is run in the editor, but not when running the .jar file.

    The code for the method is:

    Java Code:
    public void handleClientRequest() throws IOException {
            String path = null;
            URL url = this.getClass().getResource("/pytteweb/resources/");
            try {
                path = url.getPath();
            } catch (Exception e) {
                JFrame framee = new JFrame("path error test");
                Container panee = framee.getContentPane();
            
                JLabel labele = new JLabel(path + " not created, error: " + e);
                panee.add(labele);
            
                framee.pack();
                framee.setVisible(true);
            }
            
            JFrame frame0 = new JFrame("path test");
            Container pane0 = frame0.getContentPane();
            
            JLabel label0 = new JLabel(path + " is the path");
            pane0.add(label0);
            
            frame0.pack();
            frame0.setVisible(true);
                    
            DataOutputStream output = new DataOutputStream(mSocket.getOutputStream());
            BufferedReader input = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
            
            String header = input.readLine();
            String temp = header;
            header = header.toUpperCase();
            
            if (header.startsWith("GET")) {
                int requestStart = 0;
                int requestEnd = 0;
                
                for (int a = 0; a < temp.length(); a++) {
                    if (temp.charAt(a) == ' ' && requestStart != 0) {
                        requestEnd = a;
                        break;
                    }
                    if (temp.charAt(a) == ' ' && requestStart == 0) {
                        requestStart = a;
                    }
                }
                
                path = path + temp.substring(requestStart + 2, requestEnd);
                
                System.out.println(path);
                
                this.sendFile(output, path);
            }
            
            else if (header.startsWith("HEAD")) {
                String message = null;
                File theFile = new File(path);
                output.writeUTF(this.http_message(200));
                output.writeUTF("Content-length: " + theFile.length() + "\n");
                output.writeUTF("Content-Type: ");
                if (path.endsWith("html") || path.endsWith("htm")) {
                    output.writeUTF("text/html");
                } else if (path.endsWith("java")) {
                    output.writeUTF("text/plain");
                } else if (path.endsWith("jpeg") || path.endsWith("jpg") || path.endsWith("gif") || path.endsWith("png")) {
                    output.writeUTF("image/gif");
                } else {
                    output.writeUTF("text/html");
                }
                
                output.writeUTF("\n");
            } else {
                output.writeUTF(this.http_message(400));
            }
            
            output.flush();
        }
    Last edited by whiteface; 09-11-2012 at 11:12 AM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: Java webserver.

    A JFrame?
    Seems a bit of overkill.
    Why not simply log it, or even Sysout.println()?
    By trying to monitor the code via Swing you are opening up a load of other potential failure points that wouldn't exist by simply println()ing.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    whiteface is offline Member
    Join Date
    Sep 2012
    Location
    Northen Sweden
    Posts
    5
    Rep Power
    0

    Default Re: Java webserver.

    Yes, i use the JFrame for debugging purposes, it's not going to be in the final version of the program.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: Java webserver.

    Well, the first thing I would do is take that out and replace it with some normal debugging.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    whiteface is offline Member
    Join Date
    Sep 2012
    Location
    Northen Sweden
    Posts
    5
    Rep Power
    0

    Default Re: Java webserver.

    Yes, but i had the same problem with my program before i put in the windows, so ramoving the windows won't change anything! The program doesn't load the .html files in the resource folder inside of the pytteweb.jar file!

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: Java webserver.

    So, use some proper logging then copy and paste the logged output here so we can at least see where in the code it is going.
    Without that we have nothing to go on.
    JFrames are a terrible debugging solution because you cannot easily copy and paste the output of a run through your code, especially if each LogJFrame is a separate window.

    This goes back to "How do you know".
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. list files from URL on webserver
    By deepali1234 in forum Advanced Java
    Replies: 2
    Last Post: 12-20-2011, 08:28 PM
  2. Uploading an image in an applet to the webserver
    By Mathi_r in forum Java Applets
    Replies: 1
    Last Post: 06-10-2011, 03:24 AM
  3. Replies: 9
    Last Post: 05-30-2010, 03:17 PM
  4. Replies: 1
    Last Post: 10-21-2008, 09:21 AM
  5. Redirecting HTTP request to another webserver
    By barunva in forum Networking
    Replies: 0
    Last Post: 10-21-2008, 08:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •