JSCH / NOTES</>↗JSch26 Apr 2025
When using JSch (Java Secure Channel) to establish SSH connections in your Java application, it’s essential to properly manage resources like Session and Channel objects to prevent resource leaks. Cleanup involves explicitly closing all channels and disconnecting the session once the work is complete. Here is how you can handle session and channel cleanup in […]
UTIL PACKAGE / NOTES</>↗Util Package26 Apr 2025
In Java, the Optional class provides methods for handling values that may or may not be present. The methods ifPresent and ifPresentOrElse are particularly useful for executing code conditionally based on whether a value is present in the Optional. 1. Using ifPresent The ifPresent method executes a Consumer when the value is present (i.e., it’s […]
UTIL PACKAGE / NOTES</>↗Util Package26 Apr 2025
In Java, you can throw exceptions when an Optional is empty using the orElseThrow method. This method accepts a Supplier that provides the exception to be thrown if the Optional is empty. Here’s the syntax and an example: package org.kodejava.util; import java.util.Optional; public class Main { public static void main(String[] args) { Optional<String> optionalValue = […]
JSCH / NOTES</>↗JSch25 Apr 2025
To execute a simple command over SSH using JSch (Java Secure Channel), you can use the ChannelExec channel provided by the JSch library. Here’s how you can achieve that: Maven Dependency: If you’re using Maven, include the JSch library in your pom.xml file: <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> <!– Use the latest version –> </dependency> Code […]
SERVLET / NOTES</>↗Servlet25 Apr 2025
In Jakarta EE (formerly Java EE), you can use listeners like ServletContextListener to manage the lifecycle of a web application’s ServletContext. It provides hooks to execute logic during the initialization and destruction stages of the application. This can be useful for resource initialization, cleanup, or logging purposes. Here’s how you can use ServletContextListener: 1. Implementing […]
SERVLET / NOTES</>↗Servlet25 Apr 2025
Handling exceptions and errors in Jakarta Servlets involves several approaches, ranging from defining error-handling configurations in the deployment descriptor (web.xml) to using annotations and specific coding practices. Below are the typical ways to handle errors and exceptions in Jakarta Servlets: 1. Using web.xml for Declarative Exception Handling In the web.xml file, you can define error […]