Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Wednesday, December 1, 2010

Java Design Pattern

Design Patterns are recurring solutions to design problems.

The 23 design patterns included in Design Patterns all had several known applications and were on a middle level of generality, where they could easily cross application areas and encompass several objects. These patterns are divided into 3 types.


  1. Creational Patterns: create objects for you, rather than your having to instantiate objects directly. Your programs gains more flexibility in deciding which objects need to be created for a given case.

  2. Structural Patterns: help you to compose group of objects to larger structures, such as complex user interfaces and accounting data.

  3. Behavioural Patterns: help you to define the communication between objects in your system and how the flow is controlled in a complex program.


Tuesday, November 30, 2010

Multiple Message Resource Bundle in Struts 1

Target Audience: Web Developer, Web Component Developer

What you should know already: Struts Web Framework, MVC Pattern, Knowledge of struts-config.xml file

Introduction


The message resource class in Struts allows the developer internationalizing web application easy and fast. The user can put labels of fields or description text in a central file and can access them later in the JSP File. The advantage is that you can reuse labels like error messages, titles in multiple JSP files and you can provide the ressource files in multiple languages.

message-resource Element


You might have already seen the message-resource element used in the file struts-config.xml

<message-resources parameter="ApplicationResource" null="false" />


However, it is not often recognized that you can use more than one of these elements in struts-config.xml file. For example, this is a perfectly legal configuration:
<message-resources parameter="ApplicationResrc_English" null="false" />
<message-resources parameter="ApplicationResrc_French" key="fr" />
<message-resources parameter="ApplicationResrc_German" key="gr" />


The first of these - without a key attribute - becomes the default resource set. So, all your JSPs and servlets have access to that set of resources through the servlet context.

Content of ApplicationResrc_English.properties is:
label.register=register
Content of ApplicationResrc_German.properties is:
label.register=registrieren

How to Access?


Your JSP page must indicate a resource if they want to use the non-default set (in this case ApplicationResrc_German). To do this, you will need to use the message tag, part of the bean tag library. So, assume that,

  • you have associated the bean tag library with bean prefix

  • you have got a property in the ApplicationResrc_German bundle with the key label.register, then


<bean:message bundle="gr" key="label.register" />
This will print registrieren

If you use without the bundle attribute, it will pick from the default resource set which from ApplicationResrc_English.properties file.

<bean:message key="label.register" />
This will print register

Thus, by configuring multiple message resource bundles in Struts 1, website can easily be internationalized.

Sunday, November 28, 2010

Make use of toString() in Java

Every Java beginners must know about toString() method of java.lang.Object class. As you know java.lang.Object is the super most class in Java. Every pre-defined and user-defined class can override the methods available in the class java.lang.Object.

Of these, toString() method plays a vital role. See the following example:

int x = 100;
System.out.println(x);


This will print 100. Discuss the below example too:

class Student{
private String name;
public Student(String studentName){ name=studentName; }
private void setName(String name){ this.name=name; }
private String getName(){ return name; }
}


class ABC{
public static void main(String [] a){
Student s=new Student("Guru");
System.out.println(s);
}
}


Can you guess the output of the above program? Dirtily, it prints the object reference, not some meaningful information (such as student name, in this example). Now see this example:
class Student{
private String name;
public Student(String studentName){ name=studentName; }
private void setName(String name){ this.name=name; }
private String getName(){ return name; }
/** method overridden **/
public String toString(){ return name; }
}


class ABC{
public static void main(String [] a){
Student s=new Student("Guru");
System.out.println(s);
}
}


This prints Guru. So, overriding toString() method improves your object quality and very very helpful when you directly pass or print object.

Saturday, November 27, 2010

Configuring JNDI data source in Tomcat

By using Java Naming and Directory Interface (JNDI) service, one can connect resources of different technologies.

A typical web application in Tomcat has a file called as context.xml in which the context path (root) of the web application is defined. Usually this file is located in META-INF directory of you web application (see figure 1). You can configure a JNDI data source in Tomcat by adding a declaration for your resource to this file. This is the application's context container, which enables you to specify application meta-data necessary for the server in order to deploy and run the application. There are various locations where you can specify context elements, such as your server's global i.e. tomcat-install-dir/conf/context.xml

[caption id="attachment_109" align="alignnone" width="264" caption="Figure 1: META-INF/context.xml"]context.xml[/caption]

Add the following Resource tag as a declaration for the JNDI resource. The code below is an example of how you can declare JNDI resource for MySQL database.

<?xml version="1.0"?>
<Context path="/abc">
<Resource name="jdbc/jndi-name"
auth="container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="yourusername"
password="yourpassword"
driverClass="com.mysql.jdbc.Driver"
url="jdbc:mysql://hostname:3306/databasename" />
</Context>