Friday, September 17, 2010

Load the xml file from the class which is in the same Jar file

Lets assume you have a class called samle and xml file called sample.xml file. Both class and xml files are in the jar file.

If you want to load the sample.xml from the sample.class file, you need to use the following code.

this.getClass().getClassLoader().getResource("sample.xml")

Sample examples:

public class Sample {
public void callXmlMapping() {
URL url = this.getClass().getClassLoader().getResource("sample.xml");
}
}


You can create jar file with these both files and add this jar file in class path of your webapplication.

Log4j setup and configuration specific to module

Lets assume you would like to enable logging for the modules which starts with the package called com.sample.*.*.*. You need to create a log4j.properties file in \web-inf\classes folder. And place foloowing entries in the log4j.properties file. And make sure that log4j.jar file needs to be there in your class path (Ex: web-inf\lib folder)

log4j.logger.com.sample=DEBUG, sample
log4j.appender.sample.File=${user.dir}/sample/logs/sample.log
log4j.appender.sample.MaxFileSize=10MB
log4j.appender.sample.MaxBackupIndex=10
log4j.appender.sample.Append=false
log4j.appender.sample=org.apache.log4j.RollingFileAppender
log4j.appender.sample.layout=org.apache.log4j.PatternLayout
log4j.appender.sample.layout.ConversionPattern=%d{ABSOLUTE} %5p [%t] %c - %m%n


Now, the log messages will redirect to the location ${user.dir}/sample/logs/sample.log.

Wednesday, September 8, 2010

Repeat HashMap using JSTL tags

Lets assume You have a HashMap like this:

Map map = new HashMap();
map.put("fruits", "Apples");
map.put("Vegetables", "Tomota");
map.put("Vehicles", "Hero Honda");
request.setAttribute("itemsMap", map);


We need the following code to repeat the HashMap Using JSTL tags

c:forEach var="item" items="${itemsMap}">

Key is : ${item.key} and value is: ${item.value}



/c:forEach>

Please note that "<" is missing before c:forEach tag

Thursday, June 3, 2010

Have you aware of the problems using toUpperCase and toLowerCase method's in Java String API?

The toUpperCase() and toLowerCase() method's in String API's internally use the Locales. And hence these functions won't work with some of the locales (like turkish e.t.c).

For example consider the following toUpperCase() method code, it won't return the uppercase of the specified string value. To avoid that, you need to explicitly set English locale to the toUpperCase() and toLowerCase() method's. Here turkish language has a problems with charecter "i", so it won't return the expected value.

public class TestAgain

{

public static void main (String[] args) {
Locale locale = new Locale("tr" , "TR");

Locale.setDefault(locale);
String val = "DeleteAllVersions";
val = val.toUpperCase(Locale.getDefault());

System.out.println("val:" + val);

}
}

Iterator Vs Enumerator

1. By using iterator we can remove the value but in ennumerator we can't.

2. Enumeration is twice as fast as Iterator and uses very less memory.

3. Iterator is much safer as compared to Enumeration b’ coz it always denies other threads to modify the collection object which is being iterated by it. Whenever a second thread tries for that Iterator will throw a ConcurrentModificationException. Iterators that do this are known as fail-fast iterators as they fail quickly and cleanly

Iterator Vs ListIterator

Iterator : Enables you to cycle through a collection,obtaining or removing elements

ListIterator :It extends Iterator allow bidirectional traversal of list & the modification of element

Websphere - java.io.IOException: Async IO operation failed

1. The problem only happens when finishBufferResponse request is processed asynhronously and it is possible to prevent the problem by forcing all such requests to be handled synchronously. This can be done by setting the webcontainer custom property:

com.ibm.ws.webcontainer.channelwritetype=sync


2. The above errors can cause the Http connections to be terminated and can cause session timeouts. To prevent this, you may need to set this custom property