Menu

How to get the last path segment of an URL using Java

With any programming language, we come at some point where we need the last segment of the URL to manage and moderate the request and response. Hence with this sample Java program, we will see how we can get the last segment of the URL or URI after a slash (/).

public class JavaSubStringSample {

	public static void main(String[] args) {
		String path = "http://localhost/customize";
		String finalName = path.substring(path. lastIndexOf('/'));
		System.out.println(finalName);
	}
}


The above program will return the last segment of a given URL from the path variable. i.e. /customize. You may also remove the special charecter slash by adding +1 after lastIndexOf method in substring method.


Java LastIndexOf() - rashid jorvee
Screenshot of result



Get page or file name from URL using JavaScript

Read the path of the current browsing page from DOM using the below JavaScript statement. Let's assume the URL of the page is "https://rashidjorvee.blogspot.com/2020/07/remote-debugger-in-eclipse.html"

var path =window.location.pathname;


After running the above statement value of path will return "/2020/07/remote-debugger-in-eclipse.html"

Now, split the path with slash(/) to get the last occurrence, which will be the final page name URL.

var page = path.split("/").pop();

After running the above statement the value of the page will be "remote-debugger-in-eclipse.html"

Now, if we want to remove the extension .html then run the below statement which will manipulate the value of the page variable.

var name = page.substr(0, page.lastIndexOf('.'));

After running the above statement the value gets assigned to the name variable "remote-debugger-in-eclipse"

What is interface in java?

An interface is a complete abstraction, which helps us expose the methods that can be invoked by the outer world that derive the interface(subclasses). In general terms, the interface is for hiding the data.

Types of Interface


Functional interface: A interface that has only one abstract method. It may have other static and default methods. you can leave the name while implementing the name of the method because an interface contains only one abstract method.

Marker interface: A interface without any method. This is to add on some special functionality to the class so the compiler can treat that class in different ways.

Generic interface: A interface that is general like a class and has variables, methods, and constructors.