Thursday, December 18, 2014

Expression language (EL) and jstl tags in JSP

Expression language is very useful and powerful tool available for developers. With JSTL tags and expression language, we can avoid Scriptlets in JSP to make JSP code more clear and maintainable.

Here are few examples to use EL with jstl tags to perform logical operations required in JSP:

1. Condition to check value is not null or blank:
If you want to check value of "abc"  variable is not blank and not null
<c:if test="${not empty abc}">
Your HTML/JSP code
</c:if>
To print value you can also use
${not empty abc ?abc:xyz}

2. Comparision     
<c:if test="${abc eq '1'}">
Your HTML/JSP code
</c:if>
3. Multiple condition

 You can use multiple if-else condition
<c:choose>
                                    <c:when test="${not empty abc}">
                                        Code when abc not empty
                                    </c:when>
                                    <c:when test="${not empty xyz}">
                                        Code when xyz not empty
                                    </c:when>                                   
                                    <c:otherwise>
                                        Code when both abc and xyz empty
            </c:otherwise>
</c:choose>

Tuesday, July 1, 2014

How to increase SOAP request timeout value in Websphere

How to increase SOAP request timeout value in Websphere?
Change "com.ibm.SOAP.requestTimeout" configuration setting.

Path : {WebSphere-Profile-Root-Folder}\properties
File name: soap.client.props
Parameter:
com.ibm.SOAP.requestTimeout : Default value is 180, 0 implies no timeout

So when is 180 seconds timeout not enough?
Answer:  Build automation tools like Jenkins use SOAP connectivity port to deploy applications and there might be "Connection reset" error if deployment step takes more than 180 seconds.

 Exception stack trace :
exception information: com.ibm.websphere.management.exception.ConnectorException
[wsadmin] org.apache.soap.SOAPException: [SOAPException: 
faultCode=SOAP-ENV:Client; msg=Connection reset; 
targetException=java.net.SocketException: Connection reset]

Tuesday, March 4, 2014

Java 8: Release date 18th March 2014

Finally, Java 8 is releasing in few days on 18th March. Every java developer is excited to know what new features are coming.
Lambda expressions, default and static methods in Interface are few new features.
Lambda expressions will be much talked subject after people start using it.
Reality is, industry is always 5 years behind in using latest version of java.
Lets see and explore Java 8 after 18th March.

Thursday, February 20, 2014

Java : Few basic things to know about "finally" block in Exception handling

finally block is used to as cleanup facility in case of program abruptly interrupts due to exception. e.g. Closing I/O resources, closing connection etc.

Lets find out more details about finally block and answers to few basic questions.
Question 1: Can we write try block without catch?
Answer: Yes. we can write try - finally block as shown in below code example.

Question 2 : Whether finally block is executed if exception is thrown from try block?
Answer : Yes. Code in finally block will be executed even if exception is thrown from try block.

Question 3: Exception A is thrown from try block and while executing finally block, exception B is thrown. Which exception will be thrown to calling method?
Answer : Exception B which is thrown from finally block. Exception thrown from try block is discarded

Code to verify above scenarios:


/**
* Program to show how exception in try block is discarded if there is exception in finally block
*/
public class TestException {

public static void main(String[] args) {
try {
testException();
} catch (Exception e) {
System.out.println(e);
}

}

public static void testException(){
Integer j = null;
Integer k;
String [] strArray = new String []{"aa"};
try {
System.out.println("In try");
//ArrayIndexOutOfBoundsException will be thrown from below code
String testStr = strArray[1];
}
finally{
System.out.println("in finally");
//NullPointerException will be thrown from below code as j is null.
k = j *10;
}
}

}

Output after program execution:

In try
in finally
java.lang.NullPointerException


Monday, January 13, 2014

Java: Generics and Inheritance- Important things

Generics helps to write code in easy way.
-Generics makes code robust by adding strong type check at compile time check.
- Casting is not required as you define type while creating objects.


There is very important point to note related to Generics and Inheritance concept of OOPS.

Consider following java class:


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestGenericList {

/**
* @param args
*/
public static void main(String[] args) {
List< String > strList  = new ArrayList < String >();
 strList .add("a");
 strList .add("b");
 strList .add("c");
TestGenericList t = new TestGenericList();
// Can we use below code to call printList?
// t.printList(strList );

}

public void printList(List < Object > list  ){
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
System.out.println(object);

}
}
}


We know that String extends Object so you assume that you can pass strList object to printList function.
WRONG
You can not pass strList to printList function as function accept list of Object.

Even though String extends Object but List does not extend List< Object >



You can refer java tutorials for more details at:
http://docs.oracle.com/javase/tutorial/java/generics/inheritance.html