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 9 of 9

Thread: web client

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default web client

    i have been given an assignment to right a web client in java language please can i get help with the codes to implement that?.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: web client

    Hello 5723,

    Have you started this assignment yet? We will need more details to be able to help you move forward.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: web client

    The code i have is this and i was asked to write it into a .html file

    package httpconn;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.*;
    import java.net.URL;
    import java.net.URLConnection;
     
    /**
     *
     * @author HARLEY
     */
    //URLExp
    public class Main {
     
        public static void main(String[] args) {
            try {
                URL google = new URL("http://www.google.com/");
                URLConnection yc = google.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(yc    .getInputStream()));
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    System.out.println(inputLine);
                                    //File tst = new File("tst.html");
                                    //FileOutputStream out = new FileOutputStream(tst);
                }
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
    }
    Last edited by JavaPF; June 10th, 2009 at 10:58 AM. Reason: Please use [code] [/code] tags! Read my sig.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: web client

    Hello 5723,

    Try this:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.*;
    import java.net.URL;
    import java.net.URLConnection;
     
    /**
     * 
     * @author HARLEY
     * JavaProgrammingForums.com
     */
     
    public class Main {
     
        public static void main(String[] args) {
     
            String newLine = System.getProperty("line.separator");
     
            try {            
                Writer output = null;
                File file = new File("index.html");
                output = new BufferedWriter(new FileWriter(file));
     
                URL google = new URL("http://www.google.com/");
                URLConnection yc = google.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    //System.out.println(inputLine);
                    output.write(inputLine);
                    output.write(newLine);
                }
                in.close();
                output.close();
                System.out.println("File written");
     
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    The source from the webpage will be saved to index.html in the application root directory.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Junior Member
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: web client

    i get an error code like this in my netbeans development area

    java.net.ConnectException: Connection timed out: connect
            at java.net.DualStackPlainSocketImpl.connect0(Native Method)
            at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
            at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:310)
            at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:176)
            at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:163)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:154)
            at java.net.Socket.connect(Socket.java:568)
            at java.net.Socket.connect(Socket.java:518)
            at sun.net.NetworkClient.doConnect(NetworkClient.java:174)
            at sun.net.www.http.HttpClient.openServer(HttpClient.java:404)
            at sun.net.www.http.HttpClient.openServer(HttpClient.java:516)
            at sun.net.www.http.HttpClient.<init>(HttpClient.java:240)
            at sun.net.www.http.HttpClient.New(HttpClient.java:321)
            at sun.net.www.http.HttpClient.New(HttpClient.java:333)
            at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:806)
            at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:747)
            at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:672)
            at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1020)
            at httpfile.main(httpfile.java:26)

  6. #6
    Junior Member
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: web client

    thanks my problem has been solved

  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Thumbs up Re: web client

    Quote Originally Posted by 5723 View Post
    thanks my problem has been solved
    Glad I could help
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  8. #8
    Junior Member
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: web client

    please tell me what is a servelet and how is it created

  9. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: web client

    What Is a Servlet?

    A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.

    What Is a Servlet? - The Java EE 5 Tutorial

    Java Servlet tutorials,JSP tutorial,Servlet Example,Servlet tutorial,J2EE tutorials
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. How to Write a simple XMPP (Jabber) client using the Smack API
    By JavaPF in forum Java Networking Tutorials
    Replies: 35
    Last Post: August 5th, 2014, 12:59 AM
  2. Java program to open jsp page on client side instead of server side
    By khanshakil in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 8th, 2009, 06:26 AM
  3. Replies: 1
    Last Post: April 20th, 2009, 11:17 AM
  4. How to write switch statement inside if statement?
    By Rezz in forum Loops & Control Statements
    Replies: 6
    Last Post: June 11th, 2008, 11:27 AM