Skip to main content

Welcome to Geoff Hayward's Weblog

Commenting on Java, JavaFX, Java EE, Joomla, and IoT.

There are several ways to embed the root of a deployed application into a JSP page for JavaScript to phone home with. However in some server environments some methods work better than others. This short post explains the problem that load balancing can course to the Request object and gives a simple workaround for finding the application's root from its JavaScript.

The most common suggestion for embedding an application's seems to be the following. However, the following does not work when a load balancer forwards HTTPS request to the application's server via HTTP (a.k.a. SSL Off Loading).

function root() { // Bad example
       return "${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}";}";
}

When the application server receives a proxied request via HTTP the JSP will embed the HTTP scheme along with the application server's configured HTTP port.

The simple workaround is to ask JavaScript for its origin and append only the context path from the JSP. Here is a working example:

function root() { // Good example
       return window.location.origin + "${pageContext.request.contextPath}";
}

Hope this helps.


Tags: AjaxHTTPSJSP

Read

I have been trying to get the JavaFX MediaPlayer to play MP3s. Each of the MP3s, that I have been trying to play, are being retrieved over HTTPS. The java javafx.scene.media.Media class does not support the HTTPS protocol.

To workaround this problem meant temporarily storing the MP3 on the local disk. And then playing the MP3 from the temporary disk location.

URL url = new URL("https://resource.track.mp3");
        
Path mp3 = Files.createTempFile("now-playing", ".mp3");
        
try (InputStream stream = url.openStream()) {
        Files.copy(stream, mp3, StandardCopyOption.REPLACE_EXISTING);
}

Media media = new Media(mp3.toFile().toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.play();

Do you know of a better solution or workaround that will skip the need to write to disk when the media file is retrieved over HTTPS? Leave a comment if you do.



Read

Mailing List

Responsive Media

With the ResponsiveMedia plugin for Joomla it is easy to add 3rd party content from YouTube, Vimeo, and Instagram right in to any Joomla! article.

ResponsiveMedia