Sunday, January 22, 2012

Removing scriptlets from a JSP

I have read on internet that it is a very bad idea to use scriplets in a JSP.
Even if you need to use scriplets in a JSP, that is most likely because you are doing something in JSP, which should be better handled in a Action class.

So, I am trying to gather few tips, how to convert an existing JSP with scriplet to a JSP without scriplet.

Guys please comment and add more, i'll keep on adding it to the main post :)
There could be multiple ways of doing the same thing, you guys can add your knowledge to this, so that we can come up with a post, that we can re-use.

Most of the help has been taken from

You might like to read a discussion on WHY to avoid scriplets

Scriplet code Without scriplet code
<%=request.getContextPath()%> ${pageContext.request.contextPath}
<%= request.getRequestURI().contains("/events/")%> ${fn:contains(pageContext.request.requestURI, '/events/')}
<%= request.getRequestURI().contains("/events/") ? "class='selected'" : ""%> ${fn:contains(pageContext.request.requestURI, '/events/')? 'class="selected"' : ''}
<% if(request.getRequestURI().contains("/events/")) { %>


<c:if test="${fn:contains(pageContext.request.requestURI, '/events/')}">
<% String s = request.getParameter("text1"); %> <c:set var="s" value="${param.text1}" >
<% out.println(s);%> <c:out value="${s}" />
<% if(s.equals("sam"))
out.println("Good Morning...SAM!");
<c:if test="${s eq 'sam'}">
<c:out value="Good Morning...SAM!" />

case 1: out.println("Sunday");
case 2: out.println("Monday");

<c:when test="${s==1}">Sunday </c:when>
<c:when test="${s==2}">Monday</c:when>
<c:when test="${s==3}">Tuesday</c:when>
<c:when test="${s==4}">Wednesday</c:when>
<c:when test="${s==5}">Thursday</c:when>

select between 1 & 5

HttpSession session = request.getSession();



<c:out value="${sessionScope.Questions.questionPaperID}" />

<%request.getSession().getAttribute("my_var") %>

Can be accessed using JSTL with the sessionScope keyword. To access my_var, call sessionScope.myvar

Remember to use sessionScope

<c:out value="${sessionScope.my_var}"/>

To make it easier, this link would help you understand the JSTL tags better...

Thursday, January 12, 2012

Basic fundas about using float or double

Think again if you are going to use float or double for amount/currency field.

Reason behind this is

Floats have a fixed number of total bits of precision, which must be shared among the integer and fractional parts. If you use more of those bits to store a larger integer portion (123456 vs. just 12), that leaves fewer for the fractional portion (.4 vs. .45678).

Also, you should be aware that since float and double are base-2 formats, rather than base-10, many values that can be represented with a small, finite number of digits in base-10 cannot be stored in a double or float. For instance, it is impossible to store exactly 1/10 (0.1) in a double or float, just as it's impossible to store 1/3 (0.333...) exactly in a finite number of digits in base-10.

Demonstration code:

    public static void main(String args[]) {  
        double d = 1997500.43;  
        String s = "1997500.43";  
        float f = (float) d;  
        float f3 = Float.parseFloat(s);  
        float f4 = Double.valueOf(s).floatValue();  
        float f5 = new Double(s).floatValue();  
        float xFloat;  
        Double x = new Double("63.8644951");  
        xFloat = x.floatValue();  
        System.out.println(xFloat);// 63.864494  
        float xFloat2;  
        Double x2 = new Double("3333263.8644951");  
        xFloat2 = x2.floatValue();  
        System.out.println(xFloat2);// 3333263.8  

Detailed information about floats and doubles is available at :

Best Practices while writing Java code

Usually many of us write the java code in a try-catch block as follows

       // some code here
catch(Exception e)
    // log exception here

Better way to do it is

       // some code here
catch(Exception e)
    // log exception here
catch(Throwable ex)
     // One must add throwable clause always
     // because in case of some error also, you can
     // log it and see, what error has occured.

If you do not add Throwable clause, and in production, some error occurs, Then nothing will be logged in your logs, you may end up breaking your head, why the hell it is not working.

Make it a habit to use try-catch-Throwable in place of try-catch-Exception

Classes not being generated in WEB-INF/classes folder

Posting this on behalf of Vaibhav Garg..

The settings in the Eclipse were perfect to store the output in the output folder as WEB-INF/classes folder. But, still the classes were not being generated in this folder when the code was built. 


There were a couple of dependent projects included in the build path. And, there were errors in those projects. Due to this reason, the project was not getting built. And, the error list was huge around 2500 errors in the entire workspace. So, it was difficult to get the exact reason whether the project has some errors or not.

Once the errors in dependent projects were resolved, it got built successfully and the class files got generated in the WEB-INF/classes folder.

Monday, January 2, 2012

How to check which application is using a particular port?

Source :

Let's say that we are looking for port 80 -- IIS, Apache and other web servers listen in port 80, so when you are having problems starting Apache, this technique will be useful. Here is the command.

C:\>netstat -aon | findstr 80

-a means list all active connections and their ports. -o means include their process IDs. -n means display the port numbers numerically.

The pipe symbol ( | ) means, that instead of the result of netstat being displayed on the screen, feed it's result to the findstr process -- we are looking specifically for the line which has 0.0:80 -- you actually don't need findstr, but I don't want to scroll down and hunt down manually which app is using port 80. You might see something like this.


Aha! now we know that process 560 is using port 80 (that last column right there, is the process ID), you could press CTRL-ALT-DEL now to show the process window, and manually look up which app has the process ID 560, or you could enter ..

C:\>tasklist | findstr 560