• Entries (RSS)
  • Comments (RSS)

Cannot find the tag library descriptor for “http://java.sun.com/jsp/jstl/core”

Posted by | Posted in JSP | Posted on 09-12-2008

Tagged Under : , ,

Cannot find the tag library descriptor for “http://java.sun.com/jsp/jstl/core”

‘Cannot find the tag library descriptor for “http://java.sun.com/jsp/jstl/core”‘ is an error message that you might have seen while executing your JSTL code. So if you are getting this error message, the first thing you need to do is to make sure that you have all the JSTL jar files are in your class path. If you have the JSTL libraries in your class path and still you are getting the same error, you will have an older version of JSTL in your class path. Now to fix this issue change the declaration to

<%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

Recursively delete a directory

Posted by | Posted in Java | Posted on 02-11-2008

Tagged Under :

Recursively delete a directory

Here is a very simple code for recursively delete directories using java.

	/**
	 * Recursively deletes a directory and all its
	 * sub directories.
	 */
	public static void deleteDir(File dir){
		// If it is a directory get the child
		if(dir.isDirectory()) {
			// List all the contents of the directory
			File fileList[] = dir.listFiles();
 
			// Loop through the list of files/directories
			for(int index = 0; index < fileList.length; index++) {
				// Get the current file object.
				File file = fileList[index];
 
				// Call deleteDir function once again for deleting all the directory contents or
				// sub directories if any present.
				deleteDir(file);
			}
		}
 
		// Delete the current directory or file.
	        dir.delete();
	}

To delete a directory using this method pass the directory name as a File object to deleteDir method. This code is very useful if the directory name is too long. Windows has a restriction in how long a Filename can be. So if you file path is too long, you wouldn’t be able to delete a directory.

Sending an e-mail using WebSphere Mail session settings.

Posted by | Posted in WebSphere | Posted on 27-10-2008

Tagged Under : , , , ,

Sending an e-mail using WebSphere Mail session settings.

Yesterday I had posted about configuring a Mail Session in WebSphere Application Server admin console. Today I will be talking about sending an email from your JSP using the configured mail session.

To access the Mail session for sending your email you need to do a JNDI lookup from your JSP file. The complete code for sending email using the Mail Session settings is given below.

	// Look up mail session
	javax.naming.Context context = new javax.naming.InitialContext();
	javax.mail.Session mailSession = (javax.mail.Session) context.lookup("mail/localmail");
	javax.mail.Message msg = new javax.mail.internet.MimeMessage(mailSession);
	msg.setFrom(new javax.mail.internet.InternetAddress(fromEmail));
	msg.setRecipients(javax.mail.Message.RecipientType.TO, javax.mail.internet.InternetAddress.parse(toEmail));
	msg.setSubject(mailSubject);
	msg.setText(mailBody);
 
	javax.mail.Transport.send(msg);
	System.out.println("Message Sent");

I have used the JNDI name directly for accessing the mail session settings. A better alternative is to use the resource reference in your JSP/Servlet code. The advantage of this approach is that we don’t need to alter the code even if our configuration name changes.

To add a resource reference opens your web.xml. Go to References Tab. Click on Add and select Resource Reference as the reference type and enter the details. For type select javax.mail.Session from the drop down box. If you are using resource reference, your lookup code will change to

	javax.mail.Session mailSession = (javax.mail.Session) context.lookup("java:comp/mail/localmail");

Sorting an ArrayList of objects.

Posted by | Posted in Java | Posted on 16-10-2008

Tagged Under :

Sorting an ArrayList of objects.

How do we sort an ArrayList of objects? The easiest way to sort an ArrayList of Objects is to use the Collections.sort() method. But how does the sort method know based on which field you need to sort? The solution is to implement the Comparator interface and override the compare method. For e.g.: if I have a class named person with the following structure.

class Person {
    private String firstName;
    private String lastName;
 
    public Person(){
    }
    public Person(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }
 
    /**
     * @return Returns the firstName.
     */
    public String getFirstName() {
        return firstName;
    }
    /**
     * @param firstName The firstName to set.
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    /**
     * @return Returns the lastName.
     */
    public String getLastName() {
        return lastName;
    }
    /**
     * @param lastName The lastName to set.
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
 
    public String toString(){
        return "## Firstname : "+this.firstName+", Lastname : "+this.lastName;
    }
}

I have created some objects of Person class and they are added to an ArrayList. Now I need to sort the ArrayList based on Person’s firstName. The complete code listing for sorting the ArrayList is given below.

	Person p = new Person("Bruce", "Willis");
        Person p1  = new Person("Tom", "Hanks");
        Person p2 = new Person("Nicolas","Cage");
        Person p3 = new Person("John","Travolta");
 
        ArrayList list = new ArrayList();
        list.add(p);
        list.add(p1);
        list.add(p2);
        list.add(p3);
 
        Collections.sort(list, new Comparator(){
 
            public int compare(Object o1, Object o2) {
                Person p1 = (Person) o1;
                Person p2 = (Person) o2;
               return p1.getFirstName().compareToIgnoreCase(p2.getFirstName());
            }
 
        });
 
        System.out.println(list);

Here inside my Collections.sort method I am implementing the Comparator interface and overriding the compare method.

LIKE clause with PreparedStatement

Posted by | Posted in Java | Posted on 18-01-2008

Tagged Under : ,

How to use a LIKE clause with PreparedStatement object? I had this requirement and all the methods that I tried were not working. Java was not giving me any compile time nor run time errors. However the result was not coming as expected. I did some search on that and finally I got the solution. When we use PreparedStatement with LIKE clause, give the percentage sign in your setXXX method.

For eg:

	String firstName ="A";
	String query = "SELECT NAME, EMAIL FROM USER WHERE USERID LIKE ?";
	PreparedStatement stmt = con.prepareStatement(query);
	stmt.setString(1, "%"+firstName+"%");
	ResultSet rst = stmt.executeQuery();

The above code is tested in WebSpere Application Server + Oracle and Tomcat + MySQL environments and is working fine.