Skip to main content

Posts

Showing posts from 2020

JavaScript and CSS changes don't show up in my web page

 This could be a common issue you may face if you just started some web development. Sometimes you may feel that a change you made in your CSS or Javascript, never work in your browser. Practically you may waste more time troubleshooting your CSS or Javascript issues without actually understanding why it doesn't show up when you run it in the web browser. Mostly this happens when you have externalized Javascript or CSS with something like below. <link rel="stylesheet" href="mystyles.css" /> <script src="myjavascript.js"></script> The issue with this would be caching . Web browsers tend to cache any styles or javascript files in the clientside so the browser can save up bandwidth without downloading the same set of Javascript and CSS files every time you open the website. You can see this behavior if you open the developer tools' network section in the Chrome browser. Browser caching Yet, when you try to make some changes, it would

Fix XAMPP or LAMPP Error establishing a database connection for localhost

Issue: Cannot connect to the localhost DB from the local apache server running PHP. I've experienced this issue with a couple of machines and with LAMPP stack and XAMPP on both Linux and Ubuntu.  This is assuming you have correctly provided the DB username, password, and the database to the application and still getting this error when connecting to the DB. No matter what application you are trying to run Moodle, WordPress, or a PHP connection script you wrote, it doesn't connect if you give the localhost or 127.0.0.1 as the DB host. PHP DB connection error WordPress DB Connection error Solution: Make sure you have proper privileges given to the DB user in MySQL or MariaDB. Note that I'm trying to set up WordPress and my DB name is wordpress1. To check this, you have to select your DB in the PhpMyAdmin and navigate to the Privileges tab. Privileges  Once you are there, check the users. DB Users If you have already created a user account for the DB, make sure that you have a

Apply XOR operator for integers

I am writing this post to answer a question from a student who studies IT at school.  They had this MCQ question where this Python code of XORing two integers several times and finding the output.  Question a = 20 b = 30 a = a ^ b b = a ^ b a = a ^ b The question is to find the output results of a and b. If I type this code in the Python interpreter, I get the below outputs. XOR of two numbers - Hard way First I'll explain how to do this properly for the sake of completion. If you know this already, you can just skip this and follow the easy answer. First of all, it was not a computer-based exam, you have to think this through in your mind using pen and paper.  Second of all, how on earth do you apply XOR into integers? It's been a while I learned boolean algebra and I'm pretty sure boolean means true or false.  Then it kicked me, boolean also means 1s and 0s, where you can represent integers using 1s and 0s or binary and then apply the XOR. In fact, that's how actually

How to see if a website supports each TLS version in command line

This is a quick post about checking TLS version support using the command line. Of course, there are plenty of GUI tools and online services to do this. Yet, I find it is much easier to use just a simple command to check this. I'm going to use badssl.com to test this out since it provides various examples with different SSL configurations. We'll be using openssl command to check this. Openssl is a command-line tool to work with SSL connections. If you don't have it in your machine, you'll have to install it first. Let's check a website that supports TLS1.0 TLS 1.0 has been deprecated by all web browsers and servers due to security vulnerabilities in that protocol version which you shouldn't use at all. If you have this enabled in your sever/website, please disable ASAP. openssl s_client -connect tls-v1-0.badssl.com:1010 -tls1 Let's break the command to it's parts openssl : command line tool s_client : s_client is the first command of this t

How to serialize a class with static variables in Java

TL;DR You can't! Instead; Change the static variables into a none static member variable of a singleton class. By making the variables holding class to a singleton, still, it caters to the requirement of keeping your variables having a single reference for the entire application. (This is one of the ways of doing it, surely there are more other elegant ways and crude hacks of doing this depending on the situation) This is a problem that a scholar asked me for some help with. We have a simple use case where there's a configuration class that it requires to hold some values used in application-wide. So the easiest way (not the best) somebody could think would be making the variables static, so it would be only a single reference for it throughout the whole application. Let's imagine the configuration class would look like below. import java.io.Serializable ; public class BranchConfig implements Serializable { private static String branchNa

Wget download pause and continue

If you are downloading a file with the wget command, sometimes you may need to pause it and start it back from the place where you paused rather than starting from the beginning. So you don't have to re-download the entire package. Wget can do this just like downloading from a web browser. Let's say I need to download a file from the web. So I'm using the wget command as follows. Download wget <url for the file> Pause To pause the download just hit ctrl + c  the shortcut to terminate the current command. Continue This is going to be the same command but -c switch to continue from the previous download. wget -c <url for the file> Simple as that! Yeah, you can download from a web browser, but this is more fun and easier 😋 If you are lazy like me, wget saves a couple of clicks.

Java String NullPointerException safe equals check

If you compare two Strings in Java, normally you would use the equals method to compare them to see if they are similar. This will be some common use case for input validations. However, if you don't do it properly it may cause you to throw a NullPointerException. This is pretty much like common sense, but I've seen even more experienced developers make this mistake. Let's consider the following code. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class DemoApplication { final static String theStringIknow = "Hello" ; public static void myTestMethod ( String someString ) { //do not do this if ( someString . equals ( theStringIknow )) { System . out . println ( "Same same" ); } } public static void main ( String [] args ) { String testString = "Hello" ; myTestMethod ( testString ); } } In myTestMethod, I'm checking if the String in the a

How to detect page scroll to bottom of a React component

Depending on your UI framework, some of them don't really support scroll aware components. If you had to work on one of them, then this will be great for you. I had to work with one of the old UI frameworks with ReactJs. It didn't support any scroll event awareness by default. So, I had to write some custom logic to do that. Following is a sample component with scroll event awareness to detect the bottom. class MyComponent extends React.Component { constructor(props) { super (props); //atBottom state is kept to smooth things out, // to avoid again and again detecting the bottom event if we are at the bottom this .state = {atBottom : false }; // This binding is necessary to make `this` work in the callback this .handleScroll = this .handleScroll.bind( this ); } handleScroll(event) { console.log( "scrolled" ); const element = document .getElementById( 'mylist' ); if ( ! this .st

How to set terminal title in Ubuntu 18.04 LTS

In older Ubuntu versions, you could have just right-clicked on the Ubuntu Terminal window's title bar and set any title you would like. But unfortunately after Ubuntu 18.04 LTS this feature is gone. I used to love this feature because I'm multiple tabs in the single terminal window kind of a guy. I usually like to work in multiple named terminal tabs like below. By default, in newer Ubuntu version, it is showing just the current directory. Let's see how we can do this. Ubuntu prompt In ubuntu's Bash, there's an environment variable $PS1 which is responsible for the details that the command line prompts. You'll be able to echo this and see what's inside it. If I echo it it will print something like this. echo $PS1 \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ If you really want to understand what this means, you can refer this page .  Updating the termina

When NOT to use MongoDB background unique indexing

This is based on my personal experience working on MongoDB with a Java app. My Java app is a Springboot app using Spring Data MongoDB to connect with the database. And here is the maven dependency that I used. <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-data-mongodb </artifactId> </dependency> Before jumping into the problem, let me just brief about the indexes that we are using here. Indexes Indexes are set to a collection because, during my operations, I might need the collections to be queried based on these indexed fields faster and efficiently. Unique index : This could be a field that is unique for each document that you are saving in the collection. This field can be a Candidate key which would be a field where you could use to uniquely identify a document. Sometimes these indexes are created as a validation in the database level. So, you cannot insert two similar re