Ad

Friday 9 October 2009

To create and write data to CSV files in Java(using BufferedWriter)

CSV stands of comma separated values.csv files will have the data separated by the delimiter ','.we don't need to use the third party jars for writing data to the CSV files.A Simple straightforward approach is to use the BufferedWriter for writing data to csv files.Lets say we need to create a csv file which has employee details like employee id and name.
We can use the BufferdWriter for this as shown below

BufferedWriter br =new BufferedWriter(new FileWriter("D://test.CSV"));                                              
br.write("EMPID,EMPNAME");
br.write('\n');
br.write("E01,John");
br.write('\n');
br.write("E02,George");
br.close();


As shown in the above code,we used ',' delimiter to separate the data and '\n' operator to create the new line.when we run the above code,csv files will be generated in the specified directory and it will be as shown below

Saturday 3 October 2009

Enhanced for loop in Java 5(To iterate the Collection objects)

Prior to version java 5 ,we can use only the Iterator interface to iterate the collections .The below code sample show us the typical use of iterator interface.

List strlist = new ArrayList();
strlist.add("one");
strlist.add("two");
strlist.add("three");
Iterator it=strlist.iterator();
while(it.hasNext())
{
System.out.println("String in Collections---"+it.next());                                                                          
}


Output:
String in Collections---one
String in Collections---two
String in Collections---three

But from version java 5,we can use the Enhanced for loops to iterate the collections instead of using iterator interface.Enhanced for loops will make the code looks easier and simpler as shown below


List<String> strlist = new ArrayList<String>();                                                                                        
strlist.add("one");
strlist.add("two");
strlist.add("three");
for(String str:strlist)
{
System.out.println("String in Collections---"+str);
}


Output:
String in Collections---one
String in Collections---two
String in Collections---three

Wednesday 2 September 2009

java.util.ConcurrentModificationException in java


java.util.ConcurrentModificationException is thrown in collections,when one thread is iterating a collection and at the same time another thread is modifying it.This is also applicable to collection accessed by a single thread as well when it is being iterated and modified at the same time.Consider the below code where the arraylist is iterated by the for loop and being modified at the same time.


public class Test {  
                                                                                                                           
public static void main(String[] args) {
List<String> list= new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
for(String var:list) //Iterate the ArrayList
{
list.add(var.toUpperCase); //Add the new String to ArrayList
}

System.out.println("Elements:"+list);
}


}                                                                                                                                                             


when we try to run the above code,we will get the below exception

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at COM.Test.main(Test.java:17)


To avoid this exception,iterate the collection using the ListIterator and  for adding the object to the list use the add method of the ListIterator.Sample code is shown below:


public class Test {

public static void main(String[] args) {
List<String> list= new ArrayList<String>();                                                                                          
list.add("one");
list.add("two");
list.add("three");

ListIterator<String> itr=list.listIterator();
while(itr.hasNext())
{
   itr.add(itr.next().toUpperCase());
}
System.out.println("Elements:"+list);
}

}

When we run the above program,we will get the below output

Elements:[one, ONE, two, TWO, three, THREE]

Sunday 30 August 2009

overriding hashcode and equals method

We need to override the hashcode and equals method for the object that has to be added in Collections which uses hash technique.Hence for collection like hashset,hashmap,linkedhashset and hashtable we need to override both the equals and hashcode method .

what does it really means overriding hashcode and equals method?why we need to do it?
    Map stores the object as Key-value pair.we will retrieve the value from Map,by passing key to it.when we add an key/value pair to the map,the hashcode of the Key(object) will be used to allocate a slot in the collection.Its something like an seat no where an object in the collection sits.To retrieve the value from Map,the same hashcode will be used to locate the object in Map.

Lets take a below sample class

Class Department
{
String name;
public Department(String name)
{
this.name=name;
}
public static void main (String [] args) {
HashMap map = new HashMap(); //Initialize the HashMap
Department d1= new Department("CHEMICAL"); //construct a Department Object
Department d2= new Department("PHYSICS"); //construct a Department Object
map.put(d1,"CHEMICALLAB"); //Add the String "CHEMICALLAB" to Map with key as d1
map.put(d2,"PHYSICSLAB"); //Add the String "PHYSICSLAB" to Map with key as d2
retrieveMap(map);
}
public void retrieveMap(HashMap map) //To retrive the value from Map
{
Department dep1= new Department("CHEMICAL");
Department dep2= new Department("PHYSICS");
System.out.prinntln("LABNAME1:"+map.get(dep1));//Retrieve the value by passing the Key
System.out.prinntln("LABNAME2:"+map.get(dep2));//Retrieve the value by passing the Key

}
}

when we run the above program,we will get the below output

LABNAME1:null
LABNAME2:null


But the expected output was
LABNAME1:CHEMICALLAB
LABNAME2:PHYSICSLAB


when we add the object d1 as key and Value as String "CHEMICALLAB" to HashMap,object(Superclass of all Classes)hashcode will be used to allocate a seat to key d1 in hashmap.The object hashcode method always return distinct integers for distinct objects.when we pass the key to retrieve the value associated ,it will locate the Key in the collection whose
(1) hashcode is same as the object hashcode which we passed
(2) key object in collection is same as passed key object according to the equals method.
Once the Key is found,the value associated with the key will be returned.

But we could not able to retrieve the value from haspmap,since we didn't override both the method.while we try to retrieve the value from hashmap in the above example
(1)object hashcode will be executed.Since the object d1(Key in collection) and dep1(key we passed) are distict objects,the hashcode method will return distinct values
(2) object equals method will be executed and it will return false. The object equals method will return true only if the the two references refer the same object ie if d1==dep1

Add the below methods to Department Class to override the hashcode and equals method

public boolean equals(Object obj) {                                                                                                          
Department key = (Department)obj;
return name.equals(key.name()) ;
}

public int hashCode() {
return name.hashCode();
}


and now the output will be as expected

LABNAME1:CHEMICALLAB
LABNAME2:PHYSICSLAB

Monday 24 August 2009

Struts - Cannot find bean in any scope

When we develop application using struts ,the most common error we encounters are:
(1)Cannot find bean 'myformbeanname' in any scope
(2)Cannot find bean 'mylistname' in any scope
(3)Cannot find bean error in any scope
(4)Cannot find bean org.apache.struts.taglib.html.BEAN in any scope

(1)Cannot find bean 'myformbeanname' in any scope
we will encounter this error in Jsp when we incorrectly use formbean in the Struts bean and logic tags.The cause for this error may be due to one of the below:
(a) If the formbean Name is misspelled in the Jsp.So make sure the correct formbean name is used by comparing it in struts-config.xml
(b)If we set the scope of the formbean as request and if use the forward tag as shown below

<action path="/actionurl" type="com.Sample" name="SampleFormbean" scope="request">
<forward name="view" path="sample.jsp" redirect="true"/>
</action>


The above forward tag will make the struts to use 'sendRidrect' instead of
requestdispatcher (forward method) to forward the request to jsp from the action(Sample.java).since the scope of the formbean is request,the formbean wont be available in JSP and hence we will get this error.To rectify this ,do either one of the below:
(i)set the scope of the formbean as session as shown below,so that the formabean will be available in jsp when using sendredirect by struts .

<action path="/actionurl" type="com.Sample" name="SampleFormbean" scope="session">
<forward name="view" path="sample.jsp" redirect="true"/>
</action>


(OR)
(ii)Remove the redirect attribute from the forward tag,so that the struts will use requestdispatcher to display the jsp and the formbean which is in request scope will be available to them.

<action path="/actionurl" type="com.Sample" name="SampleFormbean" scope="request">
<forward name="view" path="sample.jsp"/>
</action>


(2)Cannot find bean 'mylistname' in any scope
We get this error when we try to access the list(which is set by the servlet/action in some scope) in jsp.A typical example will be the optionCollection tag which will dynamically populate the dropdown box with list. The cause of this error may due to one of the below
(a) if the list name is misspelled in the struts tag in Jsp.Check the list attribute name in Servlet/action in which the list is set in some scope.
(b) if we set the scope of the list as request in the Action and in the forward tag if we use the attribute redirect="true".

(3)Cannot find bean error in any scope
we get this error when we do not properly implement the validation framework.The cause of this error may due to one of the below
(a) In the Formbean validate method, we add the error key for the error message to be displayed as shown below

errors.add("fieldblank",new ActionMessage("errkey1"));


If the above error key("errKey1") is not configured in the message resource properties file, then the <html:error/> used in Jsp will throw the error "Cannot find bean error in any scope".
(b)Make sure the message resource properties file in the correct classpath.If you use the properties file name other than "ApplicationResources.properties",then make an entry in the struts config like below

<message-resources parameter="com.ErrorFile" />


(4)Cannot find bean org.apache.struts.taglib.html.BEAN in any scope
we get this error,if we put our Struts tag like <html:hidden>,<html:text> outside the <html:form>tag in JSP.So make sure you put all the struts html tags inside <html:form></html:form> block.

Saturday 22 August 2009

To dynamically populate the dropdrownBox with the values in the List by using <html:optionsCollection> tag in Struts

Most of the time in wepApplications, we need to populate the dropdown box with the values available in the database.Struts provide a <html:optionsCollection> tag for populating the dropdown box with the list.There are two flavours of <html:optionsCollection> tags comes with struts.

Lets says we need to populate the dropdownbox with the city name available in the backendtable.The Value object for the table will look like

public Class Citydetails
{
private String cityName;//To hold the cityname available in the table
private String cityId; //To hold the cityid available in the table
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getCityId() {
return cityId;
}
public void setCityId(String cityId) {
this.cityId = cityId;
}
}

In the action we will construct the list of above bean object from the data available in the table.

//Ge the data from the city table and construct the list
List<Citydetails> cityList =new ArrayList<Citydetails>();
while(//there is a city row in the table)
{
//construct the Citydetails bean object and set cityid and city name from the table
cityList.add(citydetails);//add the bean to the list
}
request.setAttribute("cityList",cityList)//set the list in the request scope
return mapping.findForward("jsp");//forward to Jsp page


we can populate the dropdownbox with the list which is available in any scope by using the <html:optionsCollection> in Jsp as shown below

<html:select property="formbeancitycode">
<html:optionsCollection name="citylist" value="cityID" label="cityName" />
</html:select>

Name - The attribute name which we have set the list in request,session or application scope
value - The bean property value which will be posted to the server on form submit
label - The bean property value which should get populated in the dropdownbox.

The dropdownbox will have all the cityName which are fetched from the table.
once we select the dropdown and submit the form,the cityID of the selected cityName will be posted to the server.

Instead of setting the list in any scope,we can also set the list to a formbean property in the action.Then we can populate the list from the formbean property in the dropdownbox as shown below

<html:select property="formbeancitycode">
<html:optionsCollection property="citylist" value="cityID" label="cityName" />
</html:select>

property - The formbean property name which has the list.


Note: The list which is set in the scope or in the formbean property for populating the dropdown should be the list of Java bean objects.

Friday 21 August 2009

log4j:WARN No appenders could be found for logger

Nowadays log4j is used as a logging service in almost every J2EE application.
Sometimes we may get the below error when we try to configure log4j to our application

log4j:WARN No appenders could be found for logger (somePackageName.someClassName)
log4j:WARN Please initialize the log4j system properly


The above error will be thrown,when classes in log4j.jar could not able to find the configuration file(log4j.properties or log4j.xml) in the application Classpath.

So make sure you put the file(log4j.properties or log4j.xml)
(1)in WEB-INF/classes directory
or
(2)in the shared application classpath which may depends on the server(Incase of weblogic it is APP-INF/classes)

How to iterate the HashMap?

Map has a Keyset method,which return the set of Keys in the Map.Then by iterating this Set,we can iterate the hashMap values as shown below

Map strMap= new HashMap();
strMap.put("1","one");
strMap.put("2","two");
strMap.put("3","three"); //Construct a HahMap
for(String key:strMap.keySet()) //keySet will give the set of keys                                                               
{
System.out.println("Key - "+key+" Value - "+strMap.get(key));
}

Output will be:
Key - 3 Value - three
Key - 2 Value - two
Key - 1 Value - one

we can iterate the hash map values alone by the Map "values()" method as shown below.
Map "Values()" method will return the collection view of the values present in the hashmap.

for(String value:strMap.values())
{
System.out.println(" Value - "+value);                                                                                                        
}

Output will be
Value - three
Value - two
Value - one

Thursday 20 August 2009

Can we make an object ineligigle for Garbage collection by using finalize method

An object will become eligible for garbage collection,when its not referred by any reference variable.so when the object is garbage collected,the object memory will be claimed by the JVM.The object finalize method will be called before an object is garbage collected.

Lets come to our discussion.can we use this finalize method to make this object ineligible for garbage collection by passing this object to some reference.The answer is Yes,but only once.

Let says we have a object 'A' whose finalize method is overridden to pass the object reference to some other reference Variable.At some point during program execution,when an object 'A' doesn't not have any reference variable, it becomes eligible for garbage collection.when this object is about to garbage collected,object's finalize method is called.since in finalize method we have passed the this object to some reference variable,it become ineligible for garbage collection(As this object has been referred).At some later point when the same object does not have any reference ,it will again become eligible for garbage collection.But this time finalize method wont be called at the time of garbage collection.so the object will be garbage collected and the object memory will be claimed.

Hence finalize method will be called only once for any object before the garbage collection.Thus we can't make any object permanently ineligible for garbage collection not more than once by using finalize method.

Wednesday 19 August 2009

Why String is called as immutable object?

As everyone knows String is called as immutable object.what does immutable object means? An immutable object is the one, whose state can't be changed or modified after its initial construction.

Lets take the below code

String s1=new String ("immutable"); // A new String object will be created("immutable") and s1 refer to it.
System.out.println("String-"+s1);
s1=s1.toUpperCase(); //A new string object will be created("IMMUTABLE") and s1 refer to it.
System.out.println("String-"+s1);

Output will be
String-immutable
String-IMMUTABLE

So when we try to modify the string object("immutable") by calling the toUppercase method on reference s1,a new string object is created instead of applying the changes to the existing object. Thus s1 will refer to the new object("IMMUTABLE") created and the old object("immutable") will lose the reference.
Hence there will be two objects created in the memory:

  1. A string object "immutable" which has no reference(become eligible for garbage collection) and
  2. A new String object "IMMUTABLE" which is  referred by S1.

Hence if we try to alter/modify a string object,it creates a new string object instead of altering the existing object and the existing object will become eligible for garbage collection.

StringBuilder and StringBuffer classes are not immutable object.Hence if we try to make changes to these object,it will apply the changes to the same object instead of creating a new one.

Note:Wrapper classes(like Integer,Float etc) too have this immutable behaviour

Tuesday 18 August 2009

Securing JSP using security-constraint in web.xml


Normally in any web application, we will restrict the JSP not to be accessible by directly hitting the JSP URL in the client browser.It’s the servlet (or the controller) Job to forward the request to the desired JSP Pages and to render the JSP in the browser.

We can restrict the jsp pages from the direct access by the user, by adding the below entry in web.xml

<security-constraint>
<web-resource-collection>
<web-resource-name>Access to JSP</web-resource-name>
<url-pattern>/pages/*</url-pattern>--Security constraint will apply to the URL mentioned in this tag
<http-method>GET</http-method> --The http method for which this security constraint need to be applied
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint/>
</security-constraint>

Thus by the above entry, the JSP pages (also the other resources) in the directory "$applicationcontext/pages" will be accessible only through the servlet and not via the direct hit by the user in the browser.

Note: we can also achieve this security constraint for JSP, by placing the JSP files inside the web-inf directory.

Monday 17 August 2009

Difference between StringTokenizer and String Split(StringTokenizer VS Split)

We all know that the StringTokenizer class and split method in String class will break the string into multiple tokens for the given delimiter .we will be thinking which one to use as both functionality are same.But there is a feature which makes split more advantageous than StringTokenizer.

Lets take a scenario where we have a string("This,is,,Test") and we need to break this string into tokens separated by a delimiter comma(,).

String Tokenizer:

First we will use the StringTokenizer to split this string as shown below

StringTokenizer st = new StringTokenizer("This,is,,Test",",");                                                                       while (st.hasMoreTokens()) {
System.out.println("Token-"+st.nextToken());
}

The output for this will be

Token-This
Token-is
Token-Test


As we can see in the above output, StringTokenizer discarded the tokens which are empty string between the delimiter and hence the number of tokens printed is only 3.

Split:

Now we will split the string using  split method as shown below

String[] strarray = ("This,is,,Test",").split(",");                                                                                            
for (String test:strarray)
System.out.println("Token-"+test);

The output for this will be

Token-This
Token-is
Token-
Token-Test


As we can see in the above output, split takes care of tokens which are empty string between the delimiter and hence the number of tokens printed is 4.

Thus Split method is more useful in the scenarios where the string to be split'ted can have empty spaces between the delimiter.

To Rollback the Transaction in EJB when an exception occurs

The most common usage of EJB is to handle transactions.When an EJB bean is invoked the transaction will be started by the EJB Container.Whenever there is a problem or exceptions occurs in between the transaction,we need to rollback the transaction completely.There are two approaches EJB provides us to handle this rollback.

(1)Annotating the user defined exception as Application Exception:
       we need to identify the business exceptions that can be thrown by the business logic in our application.Then we have to annotate this exception as javax.ejb.ApplicationException,so that the container will rollback the transaction whenever this business exception is thrown in between the EJB transaction.
The below example shows us how to annotate an user defined exception as Application Exception:

@ApplicationException(rollback=true)                                                                                                    
public class UploadException extends java.lang.Exception {
}

whenever UploadException is thrown in between the EJB transaction,the EJB container will automatically rollback the transaction.
Note:All the Run time exception and the Remote Exception will be rollback automatically by the container

(2) using setRollbackonly() method in the EJB bean:
       Another approach is to use the setRollbackOnly() method of the SessionContext Class.
 The below EJB bean example will show how to use this method.

@Stateless                                                                                                                                            
public class SessionBeanSample implements sampleinterface
{
@Resource
SessionContext ctx;//Get the sessioncontext
public void uploadFile()
{
try
{
//Business Logic
}
catch(Exception e)
{
ctx.setRollbackonly();//By calling the setRollbackOnly on the sessioncontext
} //makes the container to rollback the transcation when
} //any exception occurs in the method uploadFile().

Friday 14 August 2009

Redirecting to a Jsp page after the user session gets expired


In any web application ,if the user session gets expired we will be redirecting to a jsp page indicating the user that the session has been time out.There are two scenerios for this.

(1)Redirecting to a JSP page after the session gets expired and only if the user performs any action :
we normally set the session activation time by adding a below entry in the web.xml file as shown below.

<session-config>
<session-timeout>60</session-timeout>
</session-config>

So by the above entry in web.xml,the the application session will be active for the maximum of 1 hr without user doing any operation or actions.If the user click or do any operation which submits the form to the sever after this 60 min of no activity by the user,we need to redirect to a JSP page indicating the user that the session has been time out.We can do this by adding the below code in the servlet

HttpSession session = request.getSession(false);//Always return only the active sessin object ie if the session already exists
if (session == null)
{
//redirect to login page
}

But the above approach is tedious as we need to include the above code in all our servlets.The Best approach will be to use Fileters like below

public class SessionFilter implements Filter
{
private FilterConfig filterConfig;
public void doFilter (ServletRequest request,ServletResponse response,
FilterChain chain)
{
HttpSession session =null;
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
try
{
session=req.getSession(false);
if(session==null)
{
res.sendRedirect("/timeout.jsp"); //If the Active session is null ,we redirect to the timeout.jsp
}
chain.doFilter(request, response);
}
catch (IOException io) {
System.out.println ("IOException raised in SimpleFilter");
} catch (ServletException se) {
System.out.println ("ServletException raised in SimpleFilter");
}
}
public FilterConfig getFilterConfig()
{
return this.filterConfig;
}
public void setFilterConfig (FilterConfig filterConfig)
{
this.filterConfig = filterConfig;
}
public void init(FilterConfig arg0) throws ServletException {
}

}

Also map the Fiter in web.xml as shown below

<filter>
<filter-name>sessionfilter</filter-name>
<filter-class> com.filter.SessionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sessionfilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

So all the request of URL pattern *.do will call the session filter prior to the servlets mapped to it

Note:Make sure that the URL pattern for the login page doesnt call the above filter as the session will be always new while the user hits the login page.

(2)Redirecting to a JSP Page automatically after the session gets expired:
There will be some cases where we need to automatically redirect to the JSP if the user session gets expired without waiting for the user doing any action or operation.
One may think to use HttpSessionListener or HttpSessionActivationListener.But it doesn't solve the problem.The below HTML meta tag helps us to acheive this

<meta http-equiv="refresh" content="<%=session.getMaxInactiveInterval%>;url=/timeout.jsp" />

session.getMaxInactiveInterval - will get the maximum inactive interval for the session which we set in the web.xml
Thus the current page will be refreshed to the jsp file(mentioned in URL), if the page is kept open for time specified in Content.

Note:Please use this Meta tag approach only if you have this requirement of redirecting to the JSP page automatically after the session the gets expired.Otherwise always use filters for the session check.

Related post:

Thursday 13 August 2009

Default JNDI Name for the EJB Bean

In the EJB version prior to 3.0 we need to specify the JNDI name for the EJB to be deployed in the server specific xml file(For weblogicit is weblogic-ejb-jar.xml).But for EJB 3.0 ,server will provide a default JNDI name for the EJB which gets deployed in the server.Hence we need to know the defaut JNDI name in which the server will bind the ejb object in theJNDI tree,so that client application can able to look up the ejb object by using this JNDI Name.

Default JNDI Name for the EJB Bean in Weblogic:
    For weblogic ,the default jndi name will be "java:comp/env/ejb/[Business interface name]"
For instance if the name of the business interface which the EJB has implemented is sampleInterface,then the default jndi name will be "java:comp/env/ejb/sampleInterface".

Default JNDI Name for the EJB Bean in JBOSS:
     For JBOSS,the default jndi Name will be "[Name of the EAR]/[EJB Bean logical Name]/[Business Interface Type]"

  • [Name of the EAR] --The Name of the EAR file which contains the EJB
  • [EJB Bean logical Name]-The logical name of the EJB bean (We normally give the logical name for the EJB bean by using annotation in the bean like @Stateless(name = "SampleBean")
  • [Business Interface Type]-The Business interface can be local or remote which depends if the Client is in the same JVM or in a deifferent JVM.We normaly give this information in the EJB bean by using annotaton like @Local(businterface.class) or @Remote(businterface.class)

For Intstance if the name of the EAR file is SamApp.ear and the logical name of the Ejb bean is sampleBean and if the busines interface is local,then the default JNDI Name will be "SamApp/sampleBean/local "

NOTE:If we want to bind the EJB with the new JNDI Name other than default one,we can do the same by overriding it in Server specific xml File.

Java-J2EE

This blog is for Java-J2EE Aspirants