1

I want to do a authentication from a standalone application. Herefore i display my company's intern loginpage on an JEditorPane. At first you'll see a Login window like common. You type in Username and Password. In case of successfull login the page will change into "Login successfull". How do i differ between those windows and get the URL of the second window?

JEditorPane loginPane = new JEditorPane();
loginPane.setEditable(false);

try {
    loginPane.setPage("http://mypage.blalala.com");
} catch (IOException e) {
    loginPane.setContentType("text/html");
    loginPane.setText("<html>Could not load</html>");
}

JScrollPane scrollPane = new JScrollPane(loginPane);
JFrame f = new JFrame("Test HTML");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(scrollPane);
f.setPreferredSize(new Dimension(800, 600));
f.setVisible(true);
  • 1
    Maybe the answer [`here`](https://stackoverflow.com/a/5794823/11514534) will give you an idea how to do it. – second Aug 22 '19 at 09:07
  • @second no that isnt the answer it only gives back the components on this website. i want a response from the website in case of successfull login –  Aug 22 '19 at 11:09
  • I am not sure what you are trying to say. If you use an `HttpClient` you can send any type of request to your server and you should get a proper response from it. You might want to edit your question and describe what kind of response you are expecting from the server. – second Aug 22 '19 at 12:11
  • I think the answer from `@vincenzopalazzo` should show you the general use case, however it did not work for my sample page either. It probably needs some adjustments. Theory is that if you enter the data correctly, within the `HyperLinkListener` you should be able to generate the correct request to your server with which it should reply with a new html page. – second Aug 22 '19 at 16:54
  • In case you still need the distinction internaly for your application you will probably have to parse the url / or the content to see on which page you are (which again would leed to the initialy linked answer). – second Aug 22 '19 at 16:55

1 Answers1

1

I think you have need to the listener, this is an my example app

/*
 * This code is under license Creative Commons Attribution-ShareAlike 1.0
 * <a href="https://creativecommons.org/licenses/by-sa/1.0/legalcode"></a>
 */
package javaapplication5;

import java.awt.Dimension;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;

public class DemoAppWebSwing extends JFrame {

    private static final DemoAppWebSwing SINGLETON = new DemoAppWebSwing();

    public void init() {
        JEditorPane loginPane = new JEditorPane();
        loginPane.setEditable(false);

        try {
            loginPane.setPage("https://github.com/vincenzopalazzo");
            loginPane.addHyperlinkListener(new HyperlinkListener() {
                public void hyperlinkUpdate(HyperlinkEvent e) {
                    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                        JEditorPane pane = (JEditorPane) e.getSource();
                        if (e instanceof HTMLFrameHyperlinkEvent) {
                            HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
                            HTMLDocument doc = (HTMLDocument) pane.getDocument();
                            doc.processHTMLFrameHyperlinkEvent(evt);
                        } else {
                            try {
                                pane.setPage(e.getURL());
                            } catch (Throwable t) {
                                t.printStackTrace();
                            }
                        }
                    }
                }
            });
        } catch (IOException e) {
            loginPane.setContentType("text/html");
            loginPane.setText("<html>Could not load</html>");
        }

        JScrollPane scrollPane = new JScrollPane(loginPane);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().add(scrollPane);
        this.setPreferredSize(new Dimension(800, 600));
        this.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                SINGLETON.init();
            }
        });
    }

}

With the HyperlinkListener you can get another page when you click on link This is my Listener implementation:

loginPane.setPage("https://jsp-password-checking-unibas.herokuapp.com");
        loginPane.addHyperlinkListener(new HyperlinkListener() {
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    JEditorPane pane = (JEditorPane) e.getSource();
                    if (e instanceof HTMLFrameHyperlinkEvent) {
                        HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
                        HTMLDocument doc = (HTMLDocument) pane.getDocument();
                        doc.processHTMLFrameHyperlinkEvent(evt);
                    } else {
                        try {
                            pane.setPage(e.getURL());
                        } catch (Throwable t) {
                            t.printStackTrace();
                        }
                    }
                }
            }
        })

While discussing with another user in the comment section of this answer we came to the conclusion that the JEditorPane has certain limitations. However I looked for another solution to import a WebPage into a Java based UI. You can do this, with the use of JavaFX.

Check the link to the documentaion from oracle and/or this post for reference.

This is a basic example with JavaFX which basically does the same as the previously shown example using Swing.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class LoginScreen extends Application {

    @Override
    public void start(final Stage stage) {

        WebView browser = new WebView();
        WebEngine webEngine = browser.getEngine();
        webEngine.load("https://jsp-password-checking-unibas.herokuapp.com");

        Scene scene = new Scene(browser, 800, 600);

        stage.setTitle("Login Example");
        stage.setScene(scene);

        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
vincenzopalazzo
  • 1,487
  • 2
  • 7
  • 35
  • Sorry but this wont help me. So ill explain more Precisley above. –  Aug 22 '19 at 14:04
  • I gave this solution a try against a simple html page of mine, but I had a few problems with it. For my button I would only receive a 'normal' `HyperlinkEvent` instead of a `HTMLFrameHyperlinkEvent`. While I can extract the url out of it, my server actually expects a `post` request including the data from the fields of the page. Also the JEditorPane seems to ignore the `css` completly. Maybe you can update your example with these informations. – second Aug 22 '19 at 16:45
  • For the problem of the css inside the JEditorPane, I think this answar is a good [reference](https://stackoverflow.com/questions/4117302/swing-jeditorpane-css-capabilities), I have try the code with an my web app for login and the request post work, I will updare the example with my web app – vincenzopalazzo Aug 22 '19 at 18:51
  • It probably has some requirements on how the `html` page / `form` must be formatted for it to work properly, which implies that its not suitable for every situation (especially if you can not influence the page content). – second Aug 22 '19 at 20:36
  • Yes, but I think the JEditorPane this is an unique solution but in the my question I have add an reference for other solution – vincenzopalazzo Aug 22 '19 at 21:18
  • @vincenzopalazzo: I hope you don't mind, I have submitted an edit for your answer to improve your wording and added an example for the same usecase based on `JavaFX`. Works way better than the swing based solution, also with my login page :) – second Aug 22 '19 at 22:21
  • Thanks so much :) – vincenzopalazzo Aug 22 '19 at 22:28
  • @vincenzopalazzo: Seems that part of the edit was reverted by `@entpnerd`. Not sure why so. (Its still visibile in the edit history). I have left him a message in chat, not sure whether he sees it. – second Aug 22 '19 at 22:48
  • I don't know the reason, we hope you give us a reason here in the comments – vincenzopalazzo Aug 22 '19 at 22:51
  • No not you, so I think the we solution can help you, if yes can be add the up votes to aswar for satisfy our help – vincenzopalazzo Aug 23 '19 at 08:03
  • @XaNaX420: If you are not required to solve the problem with `swing`, give `JavaFX` a try. As mentioned the example is in the edit history. @vincenzopalazzo: I didn't get a response, but as the owner of the answer you can readd the example by rolling back `@entpnerd`'s change (or by simply readding it). – second Aug 23 '19 at 09:23
  • Answer rollback, if @entpnerd answer to we question I will modify the answer – vincenzopalazzo Aug 23 '19 at 09:29