Search This Blog

Showing posts with label Useful Tip. Show all posts
Showing posts with label Useful Tip. Show all posts

Saturday, 7 November 2015

Be careful with Sets and equals behavior

I was recently assigned to fix a bug in the code. For the scope of this post I re-framed the problem as below:
Context: There is a set of objects of each type (lets say fruits). Over a series of operations different type of fruits are added to the set. In certain stages certain fruits may be replaced by others in the family. For example during stage 4, we may need to replace a green apple by a red apple. On the final page we display all the fruits selected.
Problem: While the various fruits show up, the changes are not seen. So if we had updated the apple to be a red one, the final result still displays a green apple.

Thursday, 14 August 2014

Tinkering with blogger - Adding a scrolling headline

OK first and foremost - This is not related to Java. And second - This was all done with the purpose of getting an internship (yes selfish and self serving reasons for this post people).

Thursday, 24 April 2014

Creating a Database in Oracle and in PostgreSQL

As a part of my college project, I needed to create a database in oracle. It went fine. A I had a PostgreSQL set up at home, I decided to run the scripts on the PostgreSQL setup. Some interesting observations...

Monday, 20 January 2014

Mapping a Filter directly to a Servlet

I guess we have all used filters in our web projects. The common approach that I have followed is specifying the URL patterns to be intercepted by the filter.
<filter>
    <filter-name>someFilter</filter-name>
    <filter-class>com.filter.SomeFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>someFilter</filter-name>
    <url-pattern>*.do</url-pattern>
</filter-mapping>
All urls having the .do extension will now pass through the filter. There is also one more way to specify the filter pattern.

Monday, 13 January 2014

Which Log message belongs to which user ?

I created a very simple web application. In it any calls to URLs ending in ".do" reach the below servlet :

Wednesday, 14 August 2013

Accesing System Properties in Java

In an earlier post we saw The ExceptionUtils class provided by the Commons Lang library. The library also provides us with a SystemUtils class - it provides us with information about the JVM in use and the underlying platform. Consider the below code:

Sunday, 28 July 2013

Table names - does the case matter ?

In one of my previous projects I encountered a strange issue. The SQL queries that worked in our development code always failed on the server. The logs indicated that the queried tables did not exist.
This did not make any sense to us. The code ran perfectly on local MySQL database installed on our developer machines. But the minute we did it on the server we got table does not exist error. After a lot of digging we discovered that there was no problem in the code. Our table name in the ddl queries was in lower case but in our code it was in title case. But why did it work on Windows then. Some searching on the net explained the actual cause of the issue.
From the MySql docs:
In MySQL, databases correspond to directories within the data directory. Each table within a database corresponds to at least one file within the database directory (and possibly more, depending on the storage engine). Consequently, the case sensitivity of the underlying operating system plays a part in the case sensitivity of database and table names. This means database and table names are not case sensitive in Windows, and case sensitive in most varieties of Unix
Our development machines were using Windows operating systems. In Windows you cannot have two files in same directory with same names but different cases. So the case difference did not cause any problems on Windows. MySql was able to find the table correctly on the file system and all things worked fine. But our test server was a linux environment. The table in the SQL query was not same as the table in the database. Result - requested table not found.
Our solution was to create constants in the code to hold our table names in small case and to ensure that all references in our queries used these values only.
An alternative action to doing drop/rename is also available in MySql docs.
How table and database names are stored on disk and used in MySQL is affected by the value of lower_case_table_names. Changing this value on linux can make MySQL use Windows style treatment for the tables.

Sunday, 30 June 2013

ExceptionUtils

One of the most commonly used features in Java is Exceptions. I have always used it- throwing, catching and sometimes crashing code with them. I recently had a requirement where I needed to convert a stack trace to a String.

Friday, 10 May 2013

instanceof operator and equals method

I always based my equals implementation on the code written in the String class.

Wednesday, 1 May 2013

Sorting enums in java

Consider the below code:
I have defined a simple enum:

Thursday, 7 March 2013

ROOT.war

Deploying a web application in Tomcat involves placing the war file or the extracted folder in Tomcat's webapps folder. I created a very basic web application named simple and placed it in the webapps folder.