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?.
Printable View
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?.
Hello 5723,
Have you started this assignment yet? We will need more details to be able to help you move forward.
The code i have is this and i was asked to write it into a .html file
Code :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(); } } }
Hello 5723,
Try this:
Code :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.
i get an error code like this in my netbeans development area
Code :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)
thanks my problem has been solved
please tell me what is a servelet and how is it created
Quote:
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