April 8, 2020

vim : arrow keys adds characters in editor

I had this problem sometimes on my application server, which can sometimes be old machines. When I tried to use arrow keys in insert mode in vim editor the following characters are being inserted in the editor:


  • for ↓ I get B
  • for ↑ I get A
  • for ← I get D
  • for → I get C
...and it's super annoying.

From my reading, this is how Vi behaves.

However, VIM is the successor to Vi. By default, it set to be in Vi-compatible mode, which includes this behavior for the arrow keys.

To remove the compatible mode, create a file named .vimrc in home directory add this line to the top of the file:

vim  ~/.vimrc
set nocompatible

Save the file and this should fix the problem.

fedora / Linux - Add new monitor resolution

Sometimes when you plug in your new monitor, the best fit resolution for it doesn't come out as one of the option in the Display Setting.

Use these commands to add new resolution to your monitor
gtf 1920 1024 60
xrandr --newmode "1920x1024_60.00"  163.83  1920 2040 2248 2576  1024 1025 1028 1060  -HSync +Vsync
xrandr --addmode DP-1 "1920x1024_60.00"

November 4, 2017

Stop relogin everytime pushing to github repo

Instead, lets permanently authenticating with Git repositories using username and password.
Run following command to enable credential caching:

$ git config credential.helper store

Afterwards, make some changes, commit and try with git push.

No more relogin.

October 20, 2017

Automatically back up redmine installation

# required settings
DB_USERNAME=''
DB_PASSWORD=''
DB_NAME=''
REDMINE_ROOT='' # e.g. /home/peter/rails/redmine.commanigy.com'
BACKUP_ROOT='' # e.g. /home/peter/backups (will be created)

# optional settings
GS_ROOT='gs://redmine-backup'
GS_FILENAME='backup_'`date +%Y%m%d`'.tar.gz'

echo 'Setting up directories'
mkdir $BACKUP_ROOT/redmine/db -p
mkdir $BACKUP_ROOT/redmine/files -p

echo 'Backing up database'
/usr/bin/mysqldump -u $DB_USERNAME --password=$DB_PASSWORD $DB_NAME | gzip > $BACKUP_ROOT/redmine/db/`date +%Y%m%d`.gz

echo 'Backing up attachments'
rsync -a $REDMINE_ROOT/files/ $BACKUP_ROOT/redmine/files/

echo 'Packing into single archive'
tar -czPf $GS_FILENAME $BACKUP_ROOT/redmine/

echo 'Creating bucket on Google Storage (if not already created)'
gsutil mb $GS_ROOT

echo 'Copying backup archive to Google Storage'
gsutil cp $GS_FILENAME $GS_ROOT

echo 'Cleanup by removing archive'
rm $GS_FILENAME

echo 'Backup complete'

October 17, 2017

Removing unread and not starred email in Gmail

Search using the following :

in:inbox AND is:unread AND -in:starred

September 30, 2017

SSH : Remote host identification has changed

So you have recently connected to a remote Server A via ssh.

The Server A's key is then saved into a file in your local PC, typically $HOME/.ssh/known_hosts

It may looks like this :

serverA ecdsa-sha2-nistp256 AAAAE2VjZHNhLXasdasdasdmlzdHAyNTYAAAAIbm
lzddddHAyNTYAAABBBNrIVAhYMcnUasdasdsdoMv7gtf8nMHghgYzVcdddkzbDM79C
81qswhYdd8L9VX+pNjg+asdasdasdasdasdasdxgooCk=

Imagine, suddenly, the remote Server A changed their key.
As a result, the key you have in your local PC, inside known_hosts becomes invalid.

The moment you try to ssh to Server A again, you will be prompted with :

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
a7:a8:f2:97:94:33:58:b7:9d:bc:e0:a6:6b:f7:0a:29.
Please contact your system administrator.
Add correct host key in /home/ramesh/.ssh/known_hosts to get rid of this message.
Offending key in /home/ramesh/.ssh/known_hosts: 6
Permission denied (publickey,password).
The solution is to edit the file known_hosts and remove the existing key of Server A,
using your preferred text editor i.e. vi or vim or nano or notepad

When you try to ssh to Server A again, the new key of Server A will be saved into 
your local file known_hosts.

September 25, 2017

SSH SCP with Java

Good library to use is Java Secure Channel (JSch) from http://www.jcraft.com/jsch

Java doc : http://epaul.github.io/jsch-documentation/simple.javadoc/

Source code : https://github.com/ePaul/jsch-documentation

September 14, 2017

Undo bad commit

Sometimes when you did commit like below, you have forgoten where you are and accidentally committed every local changes under the current folder tree, including some that you don't want to. Urrgh!

Example : svn commit -m 'blabla'

For example, above commit results in revision 101.

You want to get back the repo to what was in 100.

You run :
1. Go to the same folder when you did the commit

2. Merge to the previous revision i.e. 100
svn merge -c -100 .

3. Check what changes this one will take place
svn stat

4. Commit
svn commit -m 'sorry Im so dumb before but I learned now'

Now you have the new revision i.e. 102 is now back to what was in 100

September 5, 2017

MySQL : Conditional If-else when selection returns empty result set

I am still very new to MySQL or SQL in general. So, this is kind of new.

I need to run a select statement and if that select statement return nothing, I would like to run another different select statement :

IF EXISTS (select something from TABLE_A) THEN                                         select something from TABLE_A;                                                 
ELSE
    select something from TABLE_B;
END IF;
More from StackOverflow :

IF NOT EXISTS (SELECT ...)
BEGIN
  INSERT ...
END
And with more control :

IF EXISTS (SELECT ...)
BEGIN
  PRINT 'Do nothing.';
END
ELSE
BEGIN
  INSERT ...
END

August 31, 2017

The APR based Apache Tomcat Native library was not found on the java.library.path

Getting this message when running JasperServer :

The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path

...and SO says :

Tomcat can use the Apache Portable Runtime to provide superior scalability, performance, and better integration with native server technologies. The Apache Portable Runtime is a highly portable library that is at the heart of Apache HTTP Server 2.x. APR has many uses, including access to advanced IO functionality (such as sendfile, epoll and OpenSSL), OS level functionality (random number generation, system status, etc), and native process handling (shared memory, NT pipes and Unix sockets).

The library is bundled into an OS specific dll (tcnative-1.dll) loaded via Java Native Interface (JNI). It allows tomcat to use OS functionalities not provided in the Java Runtime (such as sendfile, epoll, OpenSSL, system status, etc.). Tomcat will run just fine without it, but for some use cases, it will be faster with the native libraries.
If you really want it, download the tcnative-1.dll (or libtcnative.so for Linux) and put it in the bin folder, and add a system property to the launch configuration of the tomcat server in eclipse.
 -Djava.library.path=c:\dev\tomcat\bin

February 16, 2017

User's crontab

Per crontab man page:

Each user can have their own crontab, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly.

Files under /var/spool are considered temporary/working and it's always a good practice to back up your cron entries or keep them in a file in your home directory.

I assume you're using crontab -e to create crontab files on the fly. If so, you can get a "copy" of your crontab file by doing crontab -l. Pipe that to a file to get a "backup":

crontab -l > my-crontab

Then you can edit that my-crontab file to add or modify entries, and then "install" it by giving it to crontab:

crontab my-crontab

This does the same syntax checking as crontab -e.

To list all cron jobs from all users in your system:

for user in $(cut -f1 -d: /etc/passwd)
do
  echo $user
  crontab -u $user -l
done

or

for user in $(cut -f1 -d: /etc/passwd)
do
  echo $user
  crontab -l $user 

done

February 15, 2017

Why am I still getting a password prompt with ssh with public key authentication

Following this tutorial : http://www.linuxproblem.org/art_9.html

Why am I still getting a password prompt with ssh with public key authentication?

Make sure the permissions on the ~/.ssh directory and its contents are proper. Create .ssh folder if it's not there yet. Same goes to authorized_keys file.

Your home directory ~, your ~/.ssh directory and the ~/.ssh/authorized_keys file on the remote machine must be writable only by you:
rwx------ and rwxr-xr-x are fine, but rwxrwx--- is no good.
700 or 755, not 775

If ~/.ssh or authorized_keys is a symbolic link, the canonical path (with symbolic links expanded) is checked.

Your ~/.ssh/authorized_keys file (on the remote machine) must be readable (at least 400) but you probably need it to be also writable (600) if you will add any more keys to it.

Your private key file (on the local machine) must be readable and writable only by you: rw-------, i.e. 600.

Also, if SELinux is set to enforcing, you may need to run restorecon -R -v ~/.ssh (see e.g. Ubuntu bug 965663 and Debian bug report #658675; this is patched in CentOS 6).

January 25, 2017

svn diff with meld

How to use svn diff with meld

1. First, download meld from here and set the path to /bin folder of meld to $PATH variables.

2. Create a script of below content with name meld-diff.sh

#!/bin/bash
meld "$6" "$7" 

exit 0

3. Copy the location, lets say its /home/you/meld-diff.sh

4. Edit subversion configuration file. Found in /home/you/.subversion/config
Search for "diff-cmd", uncomment it and set it to path of our script we created just now. Save it.

For example : diff-cmd = /home/you/meld-diff.sh

5. Now if you run "svn diff", it should redirect to meld automatically.

January 19, 2017

Restart, Start and Stop Tomcat using Bash

$CATALINA_PID is the Environment Variable that has path of the file which should contains the pid of the catalina/tomcat startup java process.

Typically, this will be under /bin folder in your Tomcat installation.

For example, you could have this in your ~/.bashrc
CATALINA_BASE="/opt/tomcat8"
export CATALINA_BASE
CATALINA_PID="$CATALINA_BASE/bin/catalina.pid"
export CATALINA_PID

By having this $CATALINA_PID environment variable set up, we can easily get the pid of the Tomcat process and control it as we want.

I created two scripts
(1) check-tomcat : To check if Tomcat is running, by reading the Tomcat PID file
(2) restart-tomcat : To kill the tomcat, clear the Tomcat's work folder and restart Tomcat


check-tomcat
============
#!/bin/bash
#title           : check-tomcat
#description     : This script will check if Tomcat is running and return the pid
#author : Raf
#note : Requires $CATALINA_BASE/bin/catalina.pid to be created and $CATALINA_PID to be set.

clear 

if [ -f "$CATALINA_PID" ]; then
read kpid < "$CATALINA_PID"
    
    if ps --pid $kpid 2>&1 1>/dev/null; then
        echo "Tomcat is already running at ${kpid}"
    else
        echo "$CATALINA_PID found, but Tomcat is not running"
    fi
fi


restart-tomcat
==============
#!/bin/bash
#title           : restart-tomcat
#description     : This script will first clear the work folder and then check if Tomcat is running, kill it, and restart the tomcat again
#author : Raf
#note : Requires $CATALINA_BASE/bin/catalina.pid to be created and $CATALINA_PID to be set.


echo "Clear Tomcat log...."
rm -fv /opt/apache-tomcat-8.0.39/logs/catalina.out 
touch /opt/apache-tomcat-8.0.39/logs/catalina.out

echo "Clear Tomcat work folder...."
rm -rfv /opt/apache-tomcat-8.0.39/work/Catalina/

if [ -f "$CATALINA_PID" ]; then
read kpid < "$CATALINA_PID"
    
if ps --pid $kpid 2>&1 1
/opt/apache-tomcat-8.0.39/bin/catalina.sh start;

read kpid < "$CATALINA_PID"
echo "Tomcat is started at ${kpid}"
else
    echo "Killing Tomcat...."
kill -kill $kpid

/opt/apache-tomcat-8.0.39/bin/catalina.sh start;

read kpid < "$CATALINA_PID"
echo "Tomcat is started at ${kpid}"
fi
fi

Intro to Tomcat's management in Linux

1. In Linux, Tomcat can be typically installed at a few possible location such as
/usr/share/tomcat8
/var/lib/tomcat8/
/opt/tomcat8

Note : At KWT server, Tomcat is installed at /var/lib/tomcat8/


2. Tomcat has a few Environment Variables

$CATALINA_HOME : where Tomcat is installed
$CATALINA_BASE : same as CATALINA_HOME
$CATALINA_PID : location of pid file for Tomcat running process (will discuss later)


3. To start Tomcat, usually we can run : "$CATALINA_HOME/bin/catalina.sh start"


4. Once Tomcat is running, the pid of the Tomcat process is writen into a file which is declared in $CATALINA_PID.
Typically value for $CATALINA_PID is $CATALINA_BASE\bin\catalina.pid

When you open this file, (run "less $CATALINA_BASE\bin\catalina.pid"), you will see the pid of the Tomcat process


5. You stop Tomcat process by running : "kill -kill 1239" (replace 1239 with the pid of the process)
This is a generic Linux's way of killing any process.


6. Typically, to stop Tomcat, you run "$CATALINA_HOME/bin/shutdown.sh start"


NOTE : To add more : Logging, folder structure, manager, host manager, multiple instance, users, server setting

January 18, 2017

Habits of highly productive people.

The best daily routines involve taking care of yourself physically, emotionally and mentally before the day starts.

Aristotle said, "We are what we do repeatedly, therefore, excellence is not an act, but a habit."

What are those habits?

The habits that affect productivity the most are directly related to the reasons you ordinarily complain about as justifications for being lazy or unproductive. "I'm tired, I'm cold, I can't stop thinking about my ex, I didn't sleep enough last night, I didn't eat enough, my stomach hurts, I just don't feel right today," etc.

The habits themselves are the habits related to your welfare, your physical, mental and emotional well-being. If physically you're feeling good and have the requisite energy for productivity, emotionally you're stable and able to withstand the stress of having a lot to do, and mentally you aren't bogged down by identifying with every passing thought, then you have set yourself up for maximum productivity.

The habits that ensure your physical, mental and emotional welfare are:

1. Get enough sleep

Being tired not only slows down your mental and physical capacity. The first step in eliminating that excuse is making sure you get enough sleep.

How to do it:

(a) Have a set, no negotiation lights-out time.

(b) Have a set time to put stop messing around with the t.v., your phone, computer, etc. Social media and t.v. can easily distract you and keep you up for an extra hour or two.

(c) Read before you sleep, not only do you grow through reading but it helps lull you just enough to get you ready for bed.


2. Wake up early

You don't have to immediately start waking up at 4:00 a.m but you can gradually start pushing back the time you wake up (like 15 minutes a week), and over time you can be a really early riser. The reason waking up early is so integral to success is that there are no distractions whatsoever in the morning time and you can 8-10 hours of work done in 4-5.

How to implement:

(a) Sleep Early

(b) Have the following items done before going to bed : All your bags packed and ready to go for the next day. The clothes you want to wear the next day should be decided. What you want to eat for breakfast.

(c) Have a reason to wake up - you need to know exactly what your plan is. An idea of the first few tasks you want to knock out in the morning or rather, what the first few hours of your morning is going to consist of.

(3) Exercise: Looking good and feeling more confident about your physical attraction is just one incidental benefit to exercising. Your body rewards you with happiness, LITERALLY, in the form of endorphins.


3. Eating healthy  

Looking good and feeling more confident about your physical attraction is just one incidental benefit to eating healthier. To operate at maximum productive capacity, you need to have maximum energy levels. Pay attention to how your body responds to the different foods you eat and you'll quickly see the importance of healthy eating and drinking water. Your body PAYS YOU with the currency of energy for treating it the right way!

How to implement:

(a) Cut junk food - chips, soda, fast-food, etc. Needs to just go and not be an option.

(b) Have bread and other carbs earlier during the day for energy, and try to stay awake from carbs after 3 p.m

(c) WATER, LOTS and LOTS of water - as much as you can have.

(d) Force yourself to eat vegetables and fruits

4. Meditating or pray 

All day long your mind is going at 100 miles an hour and we're not even aware of it. We don't think about our thoughts as events that require energy, but every thought you have requires a certain amount of energy, and the thoughts that actually stick and force you to actively think, DRAIN YOU. Meditation helps patch up the leaking of energy caused the neuroticism of the mind.

How to implement:

(a) Full body stretch for 2-3 minutes

(b) Sit comfortably in a chair, or cross-legged on a pillow on the floor. If you're on the floor try and sit on the edge of pillow so you're leaning forward towards your knees as opposed to back, well, on your lower back.

(c) Close your eyes, try to follow your breath.


5. Stay organized 

When we know we have a million things to do with no plan of attack, we get overwhelmed and we become negative about the fact that we have so much to do, so little time, not knowing where to start. You need to have a plan of attack that breaks your big goals into smaller steps, and be strict on yourself about completing small steps. Progress adds up and you feel accomplished with every step you take.

How to implement:

(a) Have a journal/piece of paper, something to plan on.

(b) Break down the big goals/assignments/projects that you're doing into 5-10 little steps that are necessary in the process.

(c) Plan those steps into feasible deadlines, should be flexible enough that you don't have to stress AT ALL about getting them done.

The software engineer most likely to be sought after

The software engineer most likely to be sought after is ;


  • Utterly reliable. They get things done, even if it means working hard. They notify ahead of time if they're going to miss a deliverable.
  • Thorough. Things they do only need to be done once, which is to say, it's unlikely anyone else could have done it better.
  • Productive. They get things done quickly and competently.
  • Innovative. They can think of new ways to solve problems old and new.
  • Adaptable. They learn new technologies easily, and can integrate it with their existing knowledge to leverage both for gain. Also known as 'fungible.'
  • Optimistic. When things are looking crappy, they're the ones who convince everyone that this too will pass, and probably in a good way.
  • Personable. People like working with them. Despite being good, they don't make other people feel bad.

It's not about your primary programming language or technology. It's not about being smarter than everyone else. It's not about being the world's best programmer. It's about being good, being reliable, and being someone who other people want to work with.

Managers want reliable people in bad times, and people who don't cause trouble. Peers want someone who buoys them and makes the team stronger, and helps their team mates and the company succeed.

If by some miracle a position is open, hiring managers want to hire people like this because they're good for business, they're good for team morale, and they're good for the company. And they find them by remembering who THEY like working with, or asking their team for recommendations of people they like working with

January 17, 2017

Intro to Java Server Pages (JSP)

What is JSP?

JavaServer Pages (JSP) is a technology for developing web pages by inserting java code in HTML pages using special JSP tags, most of which start with <% and end with %>.

JSP can be used as part of the front end (client side) as well as the back end (server side). That means:

  • JSP is written inside HTML file (as a client side code) to perform some logic 
  • JSP can be used just like Java Servlet (as a server side code) - create connection to database, perform query, perform validation logic and so on.

Why JSP? Why not just use Java Servlet?

JSP vs. Pure Servlets :

It is more convenient to write (and to modify!) regular HTML file than to have plenty of println statements in Java Servlets that generate the HTML response.

However, JSP pages can also be used in combination with Java Servlets that handle the business logic and this is usually the code. Java Servlet is usually required to perform more powerful business logic.


JSP Processing:
  • Like normal page, your browser sends an HTTP request to the web server.
  • The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine. This is done by using the file page which ends with .jsp instead of .html i.e. index.jsp instead of index.html
  • The JSP engine converts the JSP page into a Java Servlet. All JSP elements are converted to Java code inside this generated Java Servlet.
  • The generated Java Servlet is then compiled into an executable class.
  • Servlet container loads the servlet class and executes it.
  • During execution, the servlet produces an output in HTML format inside an HTTP Response.
  • The web server forwards the HTTP response (in HTML) to your browser.
  • Finally web browser generates the HTML page inside the HTTP response.
All the above mentioned steps can be shown below in the following diagram


So in a way, a JSP page is really just another way to write a Java Servlet without having to be a Java programming wizard. Except for the translation phase, a JSP page is handled exactly like a regular Java Servlet

Learn JSP Syntax. Proceed to https://www.tutorialspoint.com/jsp/jsp_syntax.htm

Try doing this simple project :
http://www.javaknowledge.info/login-and-registration-example-in-jsp-with-session/

SSH & Linux Services management


* Connect to remote server via ssh
ssh username@server-ip:directory-location-on-server
i.e. ssh ifa@144.83.29.255:/var/lib/tomcat8/webapps 

(or in Windows, use BitviseSSH client - graphical tool)

* List all service
service --status-all

* List specified service
service --status-all | grep tomcat

* Start a service
service {service-name} start 
i.e. service tomcat8 start   

* Stop a service
service {service-name} stop
i.e. service tomcat8 stop

* Check service status
service {service-name} status
i.e. service tomcat8 status

January 13, 2017

Designing the database. First, focus on User Experience.

Don't stress on the design of the database too much.

Sometimes this may be hard. With internal Line-Of-Business (LOB) applications, the prevailing view of the business is often times that the DATA is the primary asset, where as the software is somewhat expendable. That is, for businesses, data is everything.

In my opinion, this is wrong.

In reality the asset is the company's ability to INTERACT with data. To view it, to manipulate it, and to make decisions based on it.

This means that even though they may place a high value on the data, what they are actually valuing is the software that you are writing. This means I would focus most of your effort on building an effective user experience, rather than on "designing the perfect database". The database is really just a tool that enables you to deliver on a user experience.

The key feature of relational data models is data and access path independance. You can add columns, change keys, introduce or remove indexes, etc, while having zero impact (or close to zero) on the applications that use it.

This makes the database structure extremely pliable. Easy to change.

Trying to design the database to "be flexible for the future" or to "optimize performance" is mostly a wasted effort. Changing the structure of the database will have a relatively small impact on your system.

You also can't predict how the database will scale until you run into the scenarios where you need it to scale. Your best bet is to wait until you hit performance issues. and then address them specifically.

Making changes to the user experience of your app, however, is usually more expensive. UI work is time consuming, and usually takes a while to get right.

So, my recommendation is :

  • Just quickly produce a crappy DB design 
  • React to the actual performance scenarios you encounter 
  • Focus your efforts on user experience, not on the database

January 11, 2017

Simple Java Servlet implementation (no database)

1. Creating your project directory in Tomcat's webapps folder.

In Debian Server, Tomcat can be installed at a few possible location such as

  • /usr/share/tomcat8
  • /var/lib/tomcat8/ (Note : this is where Tomcat is installed in KWT server)
  • /opt/tomcat8
However, our web application are to be placed inside the "webapps" folder. For example, for KWT server, your web application should be placed in :
  • /var/lib/tomcat8/webapps/
First, use ssh client to access your server. Then, go to this directory : cd /var/lib/tomcat8/webapps/

Create a directory with your project name : mkdir projectRaf

Create a simple index.html page to test our project page : touch index.html 

Insert something (anything) to that file : echo "this is my project page" > index.html

Go to your project page. For KWT, this will be http://{your-server-IP-here}:8080/projectRaf and the content of the index.html should be displayed. Note that 8080 is the port used by Tomcat. For comparison, Apache2 web server uses port 80, which is the default port for all browser (you don't have to specify the port for Apache2 web server). For Tomcat, you need to specify the port 8080.


2. Revise on the Java Servlet's operation.

If you haven't, you should go through this note again : http://nextslides.blogspot.my/2017/01/beginning-to-web-app-developement-web.html

Make sure you understand the use of HTTP POST and HTTP GET as well as Servlet Mapping.


Handling form data represented in HTML page is a very common task in web development. A typical scenario is the user fills in fields of a form and submits it. 

The server will process the request based on the submitted data, and send response back to the client - this is what our Java Servlet does. 


3. Create simple form (Front end)

Since our index.html file is in the server, it is quite inconvinience to edit the file directly on the server.

Luckily, Bitvise SSH Client comes with SFTP Window browser. This allow us to graphically transfer file from our local to our server, specifically to Tomcat's webapps folder







Edit our index.html and put in the following :

Form attributes :
  • method=”post” : to send the form data as an HTTP POST request to the server.
    Remember the difference between HTTP POST and HTTP GET?
  • action=”servlet--URL” : specifies the URL of the servlet.
    Remember what is Servlet-mapping? This is the usage of that mapping.

Once done, copy and overwrite the index.html on the server. Then go to the project URL, you should see something like below :

Note that this is a simple form. We will use this similar index.html file in our Eclipse project below.


4. Create Servlet using Eclipse (Back End)

For simplicity, we use the following tutorial : http://www.studytonight.com/servlet/creating-servlet-in-eclipse.php


5. Change the Servlet code

On the server side, we need to create a Java servlet which want to be mapped to loginServlet, as specified in the form’s action attribute (see our index.html above). Following is code snippet of the servlet, using the annotation method, specifically on the doPost( ) method

You may want to change the package name to yours.


6. Understand the Servlet 
Notice that the servlet’s URL is specified by the @WebServlet annotation before the servlet class. 

When the user submits the login form above, the servlet’s doPost() method will be invoked by the servlet container. 

Typically we will do the following tasks inside doPost() method:
  • Read values of the fields posted from the form via the request object (implementation of javax.servlet.http.HttpServletRequest interface).
  • Do some processing, e.g. connecting to database to validate the username and password.
  • Return response back to the user via the respone object (implementation of javax.servlet.http.HttpServletResponse interface).


To read values of form’s fields, the HttpServletRequest interface provides the following methods:
  • String getParameter(String name) : gets value of a field which is specified by the given name, as a String. The method returns null if there is no form field exists with the given name.
  • String[] getParameterValues(String name) : gets values of a group of fields which have same name, in an array of String objects. The method returns null if there is no field exists with the given name.
Usage :
String username = request.getParameter("username");
String password = request.getParameter("password");

To send response back to the client, we need to obtain a writer from the response object by calling the method getWriter() of the HttpServletResponse interface:

PrintWriter writer = response.getWriter();

Then use the print() or println() method to deliver the response (in form of HTML code). 

For example :

String htmlRespone = "{create-html-out-here}";
writer.println(htmlRespone);


6. Include index.html 


According to this strucure, we can include our index.html direcly on our project's WebContent root on Eclipse. You can either copy the index.html and drag onto WebContent folder on Eclipse or create a new index.html file and just copy paste the content we have written previously.


7. Create a war file of our project and export to server 

On Eclipse, right-click on the project name > Export > WAR file

Use the Bitvise SSH client SFTP window tool to transfer our war file to server Tomcat's webapps folder.

Note that when the war file are complely transferred to Tomcat's webapps folder, it will be automatically uncompressed into our project folder.

Access our project URL to see our deployed web application (index.html + simple Servlet).


January 10, 2017

Windows hard link and soft link (somewhat similar to Linux symlink)

Hard Links

A hard link is the file system representation of a file. Any changes to that file are instantly visible to applications that access it through the hard links that reference it.

Junctions

A junction (also called a soft link) differs from a hard link in that it references the target directories, as opposed to a single file.

Command :

mklink /H destination-file-address target-file-address
mklink /J destination-folder-address target-folder-address

January 9, 2017

MySQL Error 1215: Cannot add foreign key constraint

As a newbie, I was confused on this error until I search through StackOverflow. Phew!

Reasons you may get a foreign key constraint error:
  • You are not using InnoDB as the engine on all tables.
  • You are trying to reference a nonexistent key on the target table. Make sure it is a key on the other table (it can be a primary or unique key)
  • The types of the columns are not the same (exception is the column on the referencing table can be nullable).
  • One of the reasons may also be that the column you are using for ON DELETE SET NULL is not defined to be null. So make sure that the column is set default null
  • You are not using the same Character Set for both tables.

Installing Joomla in Debian Linux server

Many websites today use Content Management Systems (CMS) that allow you to make changes to a website without needing to touch a single line of code.

The most well know CMS are WordPress, Joomla and Drupal. All of them are free to install on your server.


How to install Joomla on Debian Linux.

1. Prequisite softwares. Make sure your server have these software installed
- MySQL (or any database)
- PHP (check using : php -version)
- Apache Web Server (or any other web server)

2. Create database for Joomla. The database will store data such as articles, menus, categories, and users.
- login to MySQL
- CREATE DATABASE DB_JOOMLA

3. Download Joomla.
- First, create a temporary directory called temp anywhere : mkdir temp
- go to into the directory : cd temp
- download Joomla to that directory : wget https://downloads.joomla.org/cms/joomla3/3-6-5/joomla_3-6-5-stable-full_package-zip?format=zip

4. Copy Joomla files into Apache Web Server and set permission
- first, create a directory under Apache Web Server : mkdir -p /var/www/html/joomla
- unzip downloaded .zip file to our folder in Apache Web Server : unzip -q Joomla_3.6.5-Stable-Full_Package.zip -d /var/www/html/joomla
- set permission for that folder appropiately :
chown -R www-data.www-data /var/www/html/joomla
chmod -R 755 /var/www/html/joomla

5. Now, we're ready to proceed with the installation of Joomla. Go to http://{your-server-ip}/joomla.
Installation page should appear. Provide the neccesary values and click next
Site Name = kwt
Description = kwt
Admin Email = mrafsyam@gmail.com
Admin Username = admin
Admin password = admin
Confirm Admin Password = admin


You should write down the admin username and password for you to login later (currently uses admin/admin)


6. In the next page, provide the database connection
Database Type = MySQLi
hostname = localhost
username = {provide a db username}
password = {password}
Database Name = db_joomla (created previoysly)
Table Prefix = jml_ (you can provide any value here)



7. Next, skip the FTP configuration step.

8. At Overview tab, select Blog English at Install Sample Data and press Install.
The installation will begin.



9. Once installation is completed, remove the installation folder.



10. Joomla is successfully installed. Go to http://{your-server-ip}/joomla/administrator/ and login as admin (check step 5). This is Joomla's Admin Panel.



11. Bravo, Joomla is installed. Go ahead and play around.
You can view your site at http://{your-server-ip}/joomla

January 8, 2017

DAO and DTO

DTO is an abbreviation for Data "Transfer" Object.

  • It is used to transfer the data between classes and modules of your application, typically with data/values from database. DTO should only contain private fields for your data, getters, setters and constructors. It is not recommended to add any logic methods to such classes.


DAO is an abbreviation for Data "Access" Object.

  • It should have the logic for retrieving, saving and updating data in your database.

Business Object is the class that uses DAO and DTO. Consider this is our servlet. It also has all logics (methods to do stuff etc).





Here is an example how the DAO and DTO interfaces would look like:









Version Control with Git

What is Version Control?

Version Control is a program that tracks changes of files over time by virtue of version or revision which is a running numbering system.

Consider this scenario :

When you started you project and save it the first time, that is version #1.
When you worked on the project again and save it for the second time, that is version #2. And, so on.

Version Control allow you to see the differences between each revision, when it was made and who did it. It also allow you to have a backup (you normally save your project to a remote server). Also, when you mess up, you can also easily get back to a previous working version.



Learn the Lingo!

Most version control systems have similar concepts. Lets go with Git.

Terms

1. Remote Repository (repo) : The remote server which we stored our files.

2. Local repo/Working Copy : Our local directory of files, where you make changes.

3. Head: The latest revision in the repo.

4. Origin : The "main" location of the files. Typically, on remote repo. But can also be on your computer.


Commands

1. git init : Make the current directory as a local repo. (local repo is the origin. no remote repo linked at this point)

2. git add : Put files into local repo for the first time, i.e. begin tracking it with Version Control.
This is called "staging" or put the file on "stage".

3. git clone : Clone a remote repo to a local directory with origin pointing to the remote repo.
Doesn't require git init. Also called checkout code from remote repo.

4. git status : Show files that are tracked in local repo.
Typically, will show files that are newly added, modified or untracked files.

5. git log : Show log of changes with their revision.

6. git revert : Revert local repo to previous revision state.

7. git commit : Commit all changes (or all files in staging) to local repo.

8. git push : Push your last commit to origin (typically, remote repo).
Also called checkin your code to remote repo.

9. git pull : Keep your local repo up to date with remote repo aka get updates from remote repo.
Obviously, also called update or sync.

10. git diff : view the differences between current codes (if changes are made) and the previous revision.

11. git merge : apply the changes from one file to another.
Typically, when you try to push a file, another developer has also pushed for some changes on the file after your last update with remote repo (so you haven't got his changes yet)

12. git rm : remove the file from tracking.
 After commited, the file then becomes "untracked" if you use "git status".





January 6, 2017

Beginning to Java Web App Developement

1. Website/Web Application

Website is a collection of web pages that may contain text, images, audio and video.
Web Application is a website which focuses more on providing some useful functions to users.

Static Website : Static website displays static/fixed information
Dynamic Website : Dynamic website displays information that keep changing.


Static WebsiteDynamic Website
Prebuilt content is same every time the page is loaded.Content is generated quickly and changes regularly.
It uses the HTML code for developing a website.It uses the server side languages such as PHP,SERVLET, JSP, and ASP.NET etc. for developing a website.
It sends exactly the same response for every request.It may generate different HTML for each of the request.
The content is only changes when someone publishes and updates the file (sends it to the web server).The page contains "server-side" code it allows the server to generate the unique content when the page is loaded.
Flexibility is the main advantage of static website.Content Management System (CMS) is the main advantage of dynamic website.


2. Hyper Text Transfer Protocol (HTTP)


HTTP
Data communication protocol used to establish communication between client (internet browser) and web server.

HTTP Request
The information send by the client (browser) to a web server that 'requesting' for some response (data, file, info etc) from the web server using HTTP protocol

GET vs POST (Both are type HTTP Request)

GETPOST
is used to retrieve data from web serveris used to insert/update data into web server.
is not secured because data is exposed in URL bar.is secured because data is NOT exposed in URL bar.
is used typically for viewing something, without changing itis used for changing something
data is sent through the header (can be seen in URL bar)
www.ask.com/register.jsp?name1=value
data is sent in body (can NOT be seen in URL bar)
www.ask.com/registerDao.jsp


3. Front End (client-side) vs Back End (server-side)

Front EndBack End
Refers to the stuff that you actually see on the browser aka the UI Interface of the website.Refers to the "brain" of the application which live on the server 
Use HTML, CSS, and JavaScript that are supported by the browser (client)Use other languages such as Java and PHP that are supported by the server 



4. Java Servlet 

Servlet is a java program that run at the back end (server-side). Servlet is the program that listen and response to HTTP Request. Servlet is the one who retrieve data from the database by performing queries.

Basically, Servlet is a java class (that extends HttpServlet class from Java API).
For example, MyServlet.java (which is then compiled to MyServlet.class)



5. Web Server & Servlet Container

Web Server is a program that processes request via HTTP..
Web Server is responsible to store, process and deliver web pages to clients.

Example : Apache HTTP Server (Apache2, httpd)

A Servlet Container is a type of Web Server that host and process Java program called Servlet. Servlet Container is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet etc. Servlet Container also works as Web Server for non-java programs.

Example : Tomcat

NOTE : All Servlet Containers are Web Server. However, not all Web Server are Servlet Container. Apache HTTP Server is only a Web Server and it cannot run Java Servlet at all.

Servlet Container can host many Java Servlets.

For normal Java Servlet, one Java Servlets are "mapped" to one specific URL/page.
For example,
When user open www.ask.com/register.jsp, regServlet  is called.
When user open www.ask.com/logout.jsp, logoutServlet  is called.

This is called Servlet Mapping and is done via Web Deployment Descriptor or using WebServlet annotation (see next)


6. Web Deployment Descriptor (web.xml)

This is a XML file where Servlet Container finds out which Servlet are mapped to which URL/page.



Example : web.xml



7. Servlet Annotation vs web.xml

'@WebServlet' annotation can be used to declare a servlet instead of web.xml (especially if you have many Servlets). This annotation is processed by the Servlet Container at deployment time, and the corresponding servlet made available at the specified URL patterns (value=URL Pattern).



8. Web Application Structure