HTML link for bookmarking a page

Hi there,

Ability to simply add your website to someone's bookmarks list can increase your traffic and chances that some one will visit your page again.

This is not because of shortcut purposes - currently bookmarking the page is pretty simply and quick anyway. You can use the Bookmarks menu or some keyboard shortcuts, i.e. CTRL+D in Firefox.

But this is more because if someone actually see the link/text saying 'Bookmark This Page' or 'Add it to your favorite' then there is a better chance that your website will be added.

OK so here is the simple HTML code for adding bookmark link:
<a href="javascript:if(document.all)window.external.AddFavorite(location.href,document.title);else%20if(window.sidebar)window.sidebar.addPanel(document.title,location.href,'');">BOOKMARK THIS PAGE</a>

In result you will have the link: BOOKMARK THIS PAGE

This it it. Hope it will be helpful.

JAVA - getting server date from remote host

Date objects in Java are quite common. Usually if you want to get the current date and time you just need to create new instance of java.util.Date:

new Date();

It is simply and straight forward in most cases. It returns whatever current date is on the client machine. 

But there might be a case when you want to have the actual date from the remote server. It is useful for example if you have trial version of your software and would like to check the real date or in general if the time is very important thing in your application.

Remote host date and time can be get from connection header. Example code below:
01. Date remoteDate = null;
03. URLConnection urlConn = url.openConnection();
04. HttpURLConnection conn = (HttpURLConnection) urlConn;
05. conn.setConnectTimeout(10000);
06. conn.setReadTimeout(10000);
07. conn.setInstanceFollowRedirects( true );
08. conn.setRequestProperty( "User-agent", "spider" );
09. conn.connect();
20. Map<String,List<String>> header = conn.getHeaderFields();
21. for (String key : header.keySet()) {
22.     if (key != null && "Date".equals(key)) {
23.         List<String> data = header.get(key);
24.         String dateString = data.get(0);
25.         SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
26.         remoteDate = sdf.parse(dateString);
27.         break;

28.     }
29. }

Some notes for this code example:

Line 22. Some of the key from header map fields may be null and we are interested only in 'Date' field.

Line 25. This one depends on the date format returned by the server. In this example it is valid for example for:
Sat, 13 Oct 2012 17:57:50

And this it - you got a date :) 

Permission problems for FAT filesystems (Ubuntu)


Here is quick post about resolving problems with mounting FAT32 paritions on Ubuntu.

There are several ways to mount a partition, for example:
1. Manual by clicking on the device from the UI ('Places').
2. Configure FSTAB to do the job for you automatically.
3. Use GUI tools such as pysdm.
4. Manual from command line.

There might be a problem with permissions of writing or even accessing mounted fat partition. You can get message that you do not have permission to see the disk content or something similar.

Basically partition type of the fat file system is called VFAT in Unix based system. By default when you mount it the ownership of the place belongs to the root user. So you have to be a root to write files to the partition. Even more - you may also not be allowed to change ownership.

So to resolve this problems you can create an entry in /etc/fstab containing some special "settings" which will mount the partition for you with all required rights:

# device mount_point  type properties X Y
/dev/sda1 /media/directory  vfat umask=0000,rw,user 0 0

The key elements are the flags:
  • rw - mount as read and write (but it is not enough for fat devices)
  • user - permits any user to mount the filesystem
  • umask - gives actual permissions or rather resets it - this actually seems to resolve an issue with lack of permission for user due to root ownership
This topic, setting and flags could be discussed in more details but umask=0000 should do the trick.

VPN Nortel via vpnc on Ubuntu

You may want to use VPN Nortel connection via vpnc client on Ubuntu. The default version does not support it - I mean it does not allow you to use PIN-token authentication mode.

However there is a workaround!
First you will need to checkout the vpnc-nortel sources directly from the SVN repo. It would be good to have subversion and vpnc dependencies before so please run:
sudo apt-get install build-essential subversion

sudo apt-get build-dep vpnc

Then to checkout from SVN:

svn co -r 517

This will get the revision 517 which is the latest at the time of writing this post.

One minor step before building the sources is to edit the Makefile and set the PREFIX var to be /usr

Now you can build the sources:
sudo make install

From now on you should be able to use it to connect via PIN-token authentication. Settings are stored in the /etc/vpnc where you can set default configuration by editing default.conf file. For example:

IPSec gateway <gateway_address>
IPSec ID <group_username_or_id>
IPSec secret <user_password>
Vendor nortel
Nortel Client ID V05_01
IKE Authmode PIN-token
Xauth username <auth_username>
Xauth PIN <pin>

To connect just run: sudo vpnc

To disconnect: sudo vpnc-disconnect

This is it. Cheers!

Microsoft Lync for Ubuntu / Linux

If you are Linux user and require to use Microsoft Lync communication you may have small problem. Currently it works only for Windows based systems or eventually for Macs.

So what can Linux user do?
Actually there is quite easy workaround. You can use Pidgin as general IM tool and install additional plugin which will get you connected with Lync users.

Follow the steps below:
  • sudo apt-get install pidgin
  • sudo apt-get install pidgin-sipe (this has specified plugin)
From now on you can add "Office Communicator" account to the Pidgin.

Provide your details (user name, psswd, etc.) and try to connect. It can offer to accept the certificate which is fine to do.

There might be 'READ ERROR' on connection. If so, run command below before starting Pidgin:



However it may be annoying to type this extra bit every time you run the IM. In that case you may consider of writing small script to do that for you.

This solution has been tested under Ubuntu distribution but should also work for other cores.


Resolving 'The terminal server has exceeded the maximum number of allowed connections'

When you are working on the remote server using remote desktop connection you may see the error when trying to connect:

"The terminal server has exceeded the maximum number of allowed connections"

The reason behind that is that Windows Server only allows limited number of RDP connections (usually just 2). There are couple of scenarios when this error can occur:
  1. There are already two people connected to the same server.
  2. There are disconnected sessions which from the server view are still active.

But in most cases the first scenario causes the error.

How to get rid of it?
Basically you will need to have an access to that machine from the other machine.
Then you can run command line and view users connected to the specified server. Use the query session command as follows:

query session /server:SERVER_IP_ADDR

It should display information such as session name, user name, id, state, type, etc. The most important is an identifier (ID) - you can reset a session of the given ID:

reset session [ID] /server:SERVER_IP_ADDR

This is it. You should be able to connect to that server.

How to make a column header immovable in JTable

I am not fulltime Swing developer but I have come to this little problem quite lately.  JTable columns headers are movable by default which sometimes can be annoying. Even if you do not want to move them you can do it by accident when for example trying to sort the column data.

Actually solution is pretty simple. Just use a custom table header renderer :)

It can be something like this one:
public class UnmovableTableHeader extends JTableHeader {

    public UnmovableTableHeader(TableColumnModel model) {

    public void setDraggedColumn(TableColumn aColumn) {
        super.setDraggedColumn(null); // it does the trick :)


Then it can be set directly on JTable object. Assuming there is somewhere an instance of JTable called table:

TableColumnModel cm = table.getColumnModel();
UnmovableTableHeader th = new UnmovableTableHeader(cm)

That's all - columns cannot be dragged but you can still use them (i.e. sorting).

Java: StringBuilder versus StringBuffer

When it comes to building / joining the strings there are some options. The most popular way is to use '+' operator. However it is not always a good idea because in some circumstances it can cause out of memory related errors. The reason behind that is that whenever you using '+' operator the new string object is created. If this is done in a long loop you may get into troubles ;)

The other options are to use existing Java classes: StringBuilder or StringBuffer.

StringBuilder was introduced in Java 5 so there is a good chance that some old geeks are get used to StringBuffer instead.

In most cases StringBuilder should be used. It is more efficient than his older brother but it does not support multi-threaded environment. In other hand StringBuffer is synchronized and should be used multithreads.

But please do not give up on using '+' operator - it is still good way when there are no loops and when it comes to quickly join some lines intro a string.

Tomcat as a Windows Service

When an application is ready to be deployed/installed it is often useful to run it as a service rather than directly from console. Tomcat provides some utilz which make it easier.

First step is to install new service. Existing service.bat script can be used:
service.bat install service_name

In order to configure existing service use Tomcat7w GUI application:
tomcat7w.exe //ES//service_name

Apart for service name and description settings you can also setup additional variables, configure memory settings and set Java VM parameters such as for example size of the permanent generation:

To monitor a service run:
tomcat7w.exe //MS//service_name
which will put an icon in the system tray

Another useful tomcat application is Tomcat7 itself. Some most popular usage below:
tomcat7.exe //RS//service_name - run the service
tomcat7.exe //SS//service_name - stop the service
tomcat7.exe //DS//service_name - delete service

You can also install Windows Administration Tools which contains "Services". Additional options can be set such as startup method (manual/ automatic) or recovery options (for example restart at first failure).

Secure copying via SCP

SCP allows to securely transfer files between a local host and a remote host or between two remote hosts. It is based on the SSH protocol.

Copying file to host:
scp file user@host:directory/

Copying file from host to current directory:
scp user@host:directory/file .

To copy entire folders use -r parameter:
scp -r user@host:directory/folder .

SCP by default uses port 22. Sometimes remote host may have it blocked or just uses other port. You can specify the different port using -P parameter.

To copy all files from remote host using port 23: 
scp -P 23 user@host:/directory/* .

To copy all files from current directory to remote host using port 23:
scp -P 23 * user@host:/directory

How to set up SSH keys

To use SSH you will need the keys. To generate them run the command:


It will prompt you for a password or just press <ENTER> to use an empty one.

Two keys have been generated under hidden directory /home/your_username/.ssh
  • id_rsa (private)
  • (public)

The last and very important step is to restric access to your keys for the others. Run the commands:

chmod 600 id_rsa
chmod 600

That's all for initial setup. Done!

To log in using your keys use the command:
ssh username@address

If you want to bind some address/port use the command:
ssh -L port:host:hostport username@address

for example: ssh -L 8888:localhost:8888 username@address