[[oktatas:programozás:java|< Java]]
====== Java web ======
* **Szerző:** Sallai András
* Copyright (c) 2014, Sallai András
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]]
* Web: https://szit.hu
===== Webtartalom lekérése =====
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
class Program01
{
public static void main(String args[])
{
InputStreamReader stream = null;
BufferedReader reader = null;
try
{
URL url = new URL("http://szit.hu");
URLConnection con = url.openConnection();
stream = new InputStreamReader(con.getInputStream());
reader = new BufferedReader(stream);
System.out.println(reader.readLine());
System.out.println(reader.readLine());
}catch(MalformedURLException e)
{
System.out.println("Hibás URL");
}catch(IOException e)
{
System.out.println("Hiba a kapcsolatban");
}finally
{
try{
reader.close();
stream.close();
}catch(IOException e){}
}
}
}
===== Kivétel szétválasztással =====
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
class Program01 {
InputStreamReader stream = null;
BufferedReader reader = null;
public void getWebpage() {
try {
tryGetWebpage();
}catch(MalformedURLException e) {
System.out.println("Hibás URL");
}catch(IOException e) {
System.out.println("Hiba a kapcsolatban");
}finally {
try{
reader.close();
stream.close();
}catch(IOException e){}
}
}
public void tryGetWebpage() throws MalformedURLException,
IOException {
URL url = new URL("http://szit.hu");
URLConnection con = url.openConnection();
stream = new InputStreamReader(con.getInputStream());
reader = new BufferedReader(stream);
System.out.println(reader.readLine());
System.out.println(reader.readLine());
}
public static void main(String args[]) {
Program01 program01 = new Program01();
program01.getWebpage();
}
}
===== Egész weblap beolvasása =====
For ciklussal:
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
System.out.println(line);
}
While ciklussal:
String line = null;
while ( (line = reader.readLine()) != null ) {
System.out.println(line);
}
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
class Program01 {
InputStreamReader stream = null;
BufferedReader reader = null;
public void getWebpage() {
try {
tryGetWebpage();
}catch(MalformedURLException e) {
System.out.println("Hibás URL");
}catch(IOException e) {
System.out.println("Hiba a kapcsolatban");
}finally {
try{
reader.close();
stream.close();
}catch(IOException e){}
}
}
public void tryGetWebpage() throws MalformedURLException,
IOException {
URL url = new URL("http://szit.hu");
URLConnection con = url.openConnection();
stream = new InputStreamReader(con.getInputStream());
reader = new BufferedReader(stream);
System.out.println(reader.readLine());
System.out.println(reader.readLine());
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
System.out.println(line);
}
}
public static void main(String args[]) {
Program01 program01 = new Program01();
program01.getWebpage();
}
}
===== Linkek =====
* http://stackoverflow.com/questions/5939732/jeditorpane-is-removing-css-font-styles
* http://kickjava.com/src/javax/swing/text/html/CSS.java.htm