Reading html source of a webpage (url) using Oracle ADF (af:richTextEditor) & Java

Sharing is Caring

How to read page source of a website ? or how to get HTML source of a webpage (URL)?

so for that, there is a simple piece of code to read HTML source of a webpage using its URL, and I have added  af:richTextEditor (ADF Faces component for rich text formatting) to show HTML form of URL source

 

    • I have a page with one input text to capture URL entered by the user, a button to fetch HTML source of that webpage and an af:richTextEditor to show source code in editing mode and in HTML mode 

 

 

    • See how to get webpage source from an URL using java and save it to an HTML file (download a webpage ), Bind inputText and richInputText to managed bean and create an actionListener on button to get the source

    // Input text Binding to get URL
    private RichInputText webPageUrlBind;
    // RickInput text Binding to set Source
    private RichTextEditor richTextBind;
    public void setWebPageUrlBind(RichInputText webPageUrlBind) {
        this.webPageUrlBind = webPageUrlBind;
    }

    public RichInputText getWebPageUrlBind() {
        return webPageUrlBind;
    }

    public void setRichTextBind(RichTextEditor richTextBind) {
        this.richTextBind = richTextBind;
    }

    public RichTextEditor getRichTextBind() {
        return richTextBind;
    }
    /**Method to get html source of url (webpage) and set this to rickTextEditor
 * @param actionEvent
 */
    public void getSourceCodeAction(ActionEvent actionEvent) {
URL url = null;

        try {
            if (webPageUrlBind.getValue() != null) {
                // Define your URL- Get value from input text using binding
url = new URL(webPageUrlBind.getValue().toString());
            }
URLConnection conn = url.openConnection();

            // open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String inputLine;
String sourceCode = "";

            //Save html file (HTML Source Code of given URL)
String fileName = "D:/web-content.html";
File file = new File(fileName);

            if (!file.exists()) {
file.createNewFile();
            }

            //use FileWriter to write file
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
System.out.println("Printing WebPage source on console, Please wait...n");
            while ((inputLine = br.readLine()) != null) {
System.out.println(inputLine);
bw.write(inputLine);
sourceCode = sourceCode.concat(inputLine);
            }

bw.close();
br.close();
richTextBind.setValue(sourceCode);
AdfFacesContext.getCurrentInstance().addPartialTarget(richTextBind);
System.out.println("nnYour file is save in D drive! Congratulations ");

        } catch (MalformedURLException e) {
e.printStackTrace();
        } catch (IOException e) {
e.printStackTrace();
        }
    }

 

html source of a webpage

 

    • Now switch RichTextMode to see HTML preview of this source, it is exactly looking like a live webpage, it shows one can practice in this ADF Faces’s rickTextEditor to learn HTML tags

 

 

  • and see in D drive of your system a file named web-content created, it contains the whole source of this webpage

 Cheers 🙂 Happy Learning

Related Posts

An Oracle ACE, Blogger, Reviewer, Technical Lead working on Oracle ADF

Leave a Reply

Your email address will not be published. Required fields are marked *