Results 1 to 5 of 5
  1. #1
    Pasha is offline Member
    Join Date
    Sep 2014
    Posts
    3
    Rep Power
    0

    Default Own server Based on com.sun.net.HttpServer, hyperlinks not working

    Hello everybody! I am trying to write simple server on java using class HttpServer. Now I have this:
    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.InetSocketAddress;
    import java.net.URI;
    import java.util.Scanner;
    
    import com.sun.net.httpserver.HttpExchange;
    import com.sun.net.httpserver.HttpHandler;
    import com.sun.net.httpserver.HttpServer;
    
    public class Test {
    
        public static void main(String[] args) throws Exception {
            HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
            server.createContext("/index.html", new MyHandler());
            server.setExecutor(null); // creates a default executor
            server.start();
        }
    
        static class MyHandler implements HttpHandler {
    		public void handle(HttpExchange t) throws IOException {
    			URI url=t.getRequestURI();
    			System.out.println(url.toString());
                String response=readFile("index.html");
                t.sendResponseHeaders(200, response.getBytes().length);
                OutputStream os = t.getResponseBody();
                os.write(response.getBytes());
                os.close();
            }
    		//
    		private static String readFile(String fileName) throws IOException
    		{
    			BufferedReader br=new BufferedReader(new FileReader(fileName));
    			String temp=br.readLine();
    			String s="";
    			while(temp!=null){
    				s+=temp;
    				temp=br.readLine();
    			}
    			return s;
    		}
    		//
    		
        }
    
    }
    index.html
    XML Code:
    <html>
    <head></head>
    <body>
    	<h1>It Works!</h1>
    	<a href="a.html">TEST</a>
    </body>
    </html>
    The problem in handle() of MyHandler class i think. There is link in index.html. When i click on that link there occurs 404 No context found for request error. What can I do to make links work?

  2. #2
    Pasha is offline Member
    Join Date
    Sep 2014
    Posts
    3
    Rep Power
    0

    Default Own server Based on com.sun.net.HttpServer, hyperlinks not working

    Hello everybody! I am trying to write simple server on java using class HttpServer. Now I have this:
    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.InetSocketAddress;
    import java.net.URI;
    import java.util.Scanner;
    
    import com.sun.net.httpserver.HttpExchange;
    import com.sun.net.httpserver.HttpHandler;
    import com.sun.net.httpserver.HttpServer;
    
    public class Test {
    
        public static void main(String[] args) throws Exception {
            HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
            server.createContext("/index.html", new MyHandler());
            server.setExecutor(null); // creates a default executor
            server.start();
        }
    
        static class MyHandler implements HttpHandler {
    		public void handle(HttpExchange t) throws IOException {
    			URI url=t.getRequestURI();
    			System.out.println(url.toString());
                String response=readFile("index.html");
                t.sendResponseHeaders(200, response.getBytes().length);
                OutputStream os = t.getResponseBody();
                os.write(response.getBytes());
                os.close();
            }
    		//
    		private static String readFile(String fileName) throws IOException
    		{
    			BufferedReader br=new BufferedReader(new FileReader(fileName));
    			String temp=br.readLine();
    			String s="";
    			while(temp!=null){
    				s+=temp;
    				temp=br.readLine();
    			}
    			return s;
    		}
    		//
    		
        }
    
    }
    index.html
    XML Code:
    <html>
    <head></head>
    <body>
    	<h1>It Works!</h1>
    	<a href="a.html">TEST</a>
    </body>
    </html>
    The problem in handle() of MyHandler class i think. There is link in index.html. When i click on that link there occurs 404 No context found for request error. What can I do to make links work?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Own server Based on com.sun.net.HttpServer, hyperlinks not working

    Once is enough. I've merged your two threads here.

    Please read the Forum Rules.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Own server Based on com.sun.net.HttpServer, hyperlinks not working

    You have created an incorrect context; try "/" for starters.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    Pasha is offline Member
    Join Date
    Sep 2014
    Posts
    3
    Rep Power
    0

    Default Re: Own server Based on com.sun.net.HttpServer, hyperlinks not working

    Quote Originally Posted by JosAH View Post
    You have created an incorrect context; try "/" for starters.

    kind regards,

    Jos
    It works!Thank you very much!

Similar Threads

  1. Replies: 5
    Last Post: 06-06-2014, 07:05 PM
  2. server based system[HELP]
    By jairoh_ in forum Networking
    Replies: 3
    Last Post: 04-20-2013, 05:25 AM
  3. com.sun.net.httpserver.HttpServer?
    By JosAH in forum Advanced Java
    Replies: 2
    Last Post: 08-08-2011, 12:30 PM
  4. Replies: 0
    Last Post: 01-28-2010, 02:46 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
  •