How to create a custom tag in JSP?

Today one of my colleague approached me and asked he needs to create a custom JSP tag. I did some quick google search and tried it out and it worked. Just wanted to share how it’s done.

It’s simple and straight forward.

Say you want to create a custom tag named and want to use it like this

<ex:hello />

Before creating the TAG we have to create the tag handler like this,

package com.karthik;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;

public class HelloTag extends SimpleTagSupport {

public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
out.println(“Hello Custom Tag!”);

Above code has simple coding where doTag() method takes the current JspContext object using getJspContext() method and uses it to send “Hello Custom Tag!” to the current JspWriter object.

Let us compile above class and copy it in a directory available in environment variable CLASSPATH. Finally create following tag library file: <YOUR_PROJECT>\WebContent\WEB-INF\custom.tld.

<short-name>Example TLD</short-name>

So, Now let’s try to test our newly created JSP custom tag by including it in a jsp page.

<%@ taglib prefix=”ex” uri=”WEB-INF/custom.tld”%>
<title>A sample custom tag</title>

Now deploy your application and try accessing the jsp. You should see this result.

Hello Custom Tag!

Technology always excites me. Gangadhar, Thanks man.
Today I had some learning because of you my friend. This post is for you mate. Cheers 🙂

Happy Coding 🙂


Postmortem of Method Overloading in Java.

Hello Code geeks,
I’m back again to share with you all an interesting overloading concept which I have just learnt.

Java Overlaoding


Please have a look at the following code,

public class OverloadDemo {

	public static void  hello(Integer i)
	public static void  hello(String s)
	public static void main(String[] args) {


What will be the output of the program?
How many of you have noticed that there is a compilation error in the above code. Well, yes there is a compilation error at Line 16 which says “The method hello(Integer) is ambiguous for the type OverloadDemo”

I was playing around with some programs implementing method overloading. I got this error. So, let’s jump in right away and figure out what’s the root cause.

So, here we have method hello which is overloaded with two different parameters.
1.hello(Integer i)
2.hello(String s)
So, Here if we pass a value like hello(1), the Integer version of it will be called and output will be Integer and if we call like this hello(“One”), the String version of it will be called and output will be String.

So, why are we getting a compilation error once we try to pass hello(null). Because, as String and Integer are classes and they both can have null values.When compiler executes it, it will be ambiguous for it, as which method it has to call. Is it hello(Integer)? Which can also take a null argument or Is it hello(String) which as well can take a null argument again.

So, for solving this, changes the Integer to int.Then everything works fine.Now, as we identified the root cause for this issue, this can be fixed by whatever the way it is possible.

Forgot to mention that, I’m preparing for OCJP7 Up gradation exam.So, that’s the reason I’m coming across such things while I’m coding.This might look simple, but in the OCJP we might not actually get it right unless we’re aware or came across of such a scenario.As I always say, OCJP is all about tricky questions.

Have a Good Night.

How to get the size of resultset in Java without iterating though ResultSet object?

Often many of us would come across such a situation where we need to know the size of the resultset returned from a database query without iterating the ResultSet object.

I also encountered a similar situation where, I needed to create an object with all the information of a particular row.

Consider this example.I have a person_info table, where I have the information like person_id,first_name,last_name,middle_name,mobile,city,gender,date_of_birth,age etc.

Now I need to query the database and for every row returned I need to create an JSONObject array like,

JSONObject json[]=new JSONObject[SIZE];

So, we have to initialize the size before iterating through ResultSet object.

After doing a lot of mistakes, found the right way to do this. Here it is.

 Statement st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
 ResultSet rs=st.executeQuery("SELECT * from persons");

 int SIZE= 0;
 if (rs != null)
 size = rs.getRow();

System.out.println("SIZE: "+size);

Happy Coding 🙂