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 http://www.roseindia.net/jstl/jstlcoretags.shtml

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/')}">
...
</c:if>
<% 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!" />
</c:if>

<%
switch(s)
{
case 1: out.println("Sunday");
case 2: out.println("Monday");
...
}
%>

<c:choose>
<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>

<c:otherwise>
select between 1 & 5
</c:otherwise>
</c:choose>

<%
HttpSession session = request.getSession();
out.println(

(Questions)

session.getAttribute("Questions").getQuestionPaperID());

%>
<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;  
        System.out.println(f);//1997500.4  
  
        float f3 = Float.parseFloat(s);  
        System.out.println(f3);//1997500.4  
  
        float f4 = Double.valueOf(s).floatValue();  
        System.out.println(f4);//1997500.4  
  
        float f5 = new Double(s).floatValue();  
        System.out.println(f5);//1997500.4  
  
        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 : http://www.coderanch.com/t/564234/java/java/Precision-loss-String-Float-double

Best Practices while writing Java code

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

try
{
       // some code here
}
catch(Exception e)
{
    // log exception here
}

Better way to do it is

try
{
       // 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. 

Reason:

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 : http://oolacola.blogspot.com/2009/04/how-to-find-out-which-application-is.html

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.

TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 560

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