Java, JBoss

How to extract/unzip a WAR file in Java?

WAR stands for ‘Web application ARchive’. A WAR file nothing but a JAR file used to distribute a collection of JSPs, Servlets, Classes, XML files, tag libraries, static web pages (HTML and related files) and other resources that together constitute a web application.

In order to extract/unzip a WAR file, use the following JAR command:

jar -xvf yourWARfileName.war
IntelliJ, JBoss

JBoss Remote Debugging Using IntelliJ

1. Stop the JBoss server if it’s running already.

2. Un-comment following line in JBoss/bin/run.conf.bat file
set “JAVA_OPTS=%JAVA_OPTS% -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n”

3. Start the JBoss server using the command prompt by following these steps:
a. Open a command prompt in JBoss/bin directory
b. Type in ‘run.bat’ and hit Enter
c. This will start the JBoss server. The command prompt will need to stay open for the remainder of the session.

4. Add a new ‘Remote’ debug configuration in IntelliJ by following these steps:
a. Click on ‘Edit Configurations’ in IntelliJ
b. Click on green ‘+’ icon to add a new configuration
c. From the list, select ‘Remote’
d. Specify a name to your new configuration (Example: JBoss)
e. Specify the port number from your ‘JBoss/bin/run.conf.bat’ file. It’s 8787 by default
f. Leave all other fields as defaults.
g. Hit ‘Apply’ and then ‘Ok’
h. You will notice a new configuration with the name you provided in the drop down in IntelliJ
i. Click on green ‘Debug JBoss’ icon next to the drop down
j. You will notice a message saying “Connected to the target VM, address: ‘localhost:8787’, transport: ‘socket'” in the IntelliJ console

5. You can now debug as usual, set break points, and inspect variables.

Java

How to generate a random number within a range in Java

Here is a standard way to generate a random number within a specific range in Java.

Please note that in the below code, the top value (max) is exclusive and the min value is inclusive.

    public static int generateRandomNumber(int max, int min) {

        Random rand = new Random();
        int randomNum = rand.nextInt(max - min) + min;
        return randomNum;
    }

Here is the code to generate a random number with max and min value inclusive.

    public static int generateRandomNumber(int max, int min) {

        Random rand = new Random();
        int randomNum = rand.nextInt((max - min) + 1) + min;
        return randomNum;
    }
Java

How to add or subtract time from Date in Java

Sample code to add 30 seconds to Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.SECOND, 30);
        Date addThirtySeconds = cal.getTime();
        System.out.println("New Date and Time: " + addThirtySeconds);

Sample code to subtract 30 seconds from Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.SECOND, -30);
        Date thirtySecondsBack = cal.getTime();
        System.out.println("New Date and Time: " + thirtySecondsBack);

Sample code to add 2 minutes to Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.MINUTE, 2);
        Date addTwoMinutes = cal.getTime();
        System.out.println("New Date and Time: " + addTwoMinutes);

Sample code to subtract 2 minutes from Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.MINUTE, -2);
        Date twoMinutesBack = cal.getTime();
        System.out.println("New Date and Time: " + twoMinutesBack);

Sample code to add an hour to Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.HOUR, 1);
        Date addAnHour = cal.getTime();
        System.out.println("New Date and Time: " + addAnHour);

Sample code to subtract an hour from Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.HOUR, -1);
        Date anHourBack = cal.getTime();
        System.out.println("New Date and Time: " + anHourBack);

Sample code to add 3 days to Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.DATE, 3);
        Date addThreeDays = cal.getTime();
        System.out.println("New Date and Time: " + addThreeDays);

Sample code to subtract 3 days from Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.DATE, -3);
        Date threeDaysBack = cal.getTime();
        System.out.println("New Date and Time: " + threeDaysBack);

Sample code to add 2 months to Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.MONTH, 2);
        Date addTwoMonths = cal.getTime();
        System.out.println("New Date and Time: " + addTwoMonths);

Sample code to subtract 2 months from Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.MONTH, -2);
        Date twoMonthsBack = cal.getTime();
        System.out.println("New Date and Time: " + twoMonthsBack);

Sample code to add 3 years to Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.YEAR, 3);
        Date addThreeYears = cal.getTime();
        System.out.println("New Date and Time: " + addThreeYears);

Sample code to subtract 3 years from Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.YEAR, -3);
        Date threeYearsBack = cal.getTime();
        System.out.println("New Date and Time: " + threeYearsBack);
JavaScript, Python

How to call Python function from JavaScript?

Well, there ain’t any special mechanism to do it really. In order to make this happen, all you need to understand is client server mechanism. JavaScript is client side, whereas Python is server side. That is, JavaScript is executed on client’s browser, and Python is executed on the server. Therefore, JavaScript cannot call Python function directly. All JavaScript can do is issue HTTP requests to server, and then server can handle those HTTP requests.

The following code snippet sends HTTP request to server:

 var xmlhttp;
    if (window.XMLHttpRequest) {
        // IE7+, Firefox, Chrome, Opera, Safari support XMLHttpRequest object
        xmlhttp = new XMLHttpRequest();
    } else {
        // IE6, IE5 doesn't support XMLHttpRequest object
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET","/servlet/Welcome.do?firstName=Devesh&lastName=Sharma",true);
    xmlhttp.send();

Now, on the server side you need to handle this request. That is, in Python file you need to catch these information being sent so that you can use it to call appropriate function. This is how to grab the sent parameters in Python file:

request.getParameter("firstName")
request.getParameter("lastName")
JavaScript

How to send an AJAX HTTP POST request to server

We send an AJAX HTTP request to server using XMLHttpRequest object. The XMLHttpRequest object can be used to send data to a server in scenarios where we want to update parts of a web page, without reloading the whole page.

All modern browsers like Firefox, Chrome, Safari, etc have built-in XMLHttpRequest object. However, some old versions of IE doesn’t support XMLHttpRequest object. If the browser doesn’t support XMLHttpRequest object, we use ActiveXObject to send request to the server.

The following code snippet sends HTTP POST request to server:

    var xmlhttp;
    if (window.XMLHttpRequest) {
        // IE7+, Firefox, Chrome, Opera, Safari support XMLHttpRequest object
        xmlhttp = new XMLHttpRequest();
    } else {
        // IE6, IE5 doesn't support XMLHttpRequest object
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("POST","/servlet/Welcome.do",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("username=tester&password=123");

We use open() and send() methods of the XMLHttpRequest object to send a request to a server.

The above example send POST request to a server. POST is always more secure and robust than GET. Moreover, using POST request you can send a large amount of data because POST doesn’t have any size limitations. However, if you are sending some trivial information to server you can use GET request. Please refer this post to learn how to send GET request to server using AJAX.