RSS

Monthly Archives: July 2010

What are Servlets?

As seen from the top then Servlets are no different as compared to a Java class. They provide a component based way to design a web application. Using servlets web developers can create fast and efficient server side application which can run on any servlet enabled web server. Servlets run entirely inside the Java Virtual Machine and so have access to the whole set of Java API. Also an application written using Servlets can be deployed in any container and it will work. Servlets are popular choice for building interactive Web applications. Third-party servlet containers are available for example Apache Web Server, Microsoft IIS, and others. Servlet containers are usually a component of Web and application servers, such as BEA WebLogic Application Server, IBM WebSphere, Sun Java System Web Server, Sun Java System Application Server, and others.

Servlets are not bound to any specific protocol and can be extended to design one of its own choice but commonly servlets designed to work with HTTP(Hyper Text Transfer Protocol) are used heavily. These Servlets are controlled by the container from their creation to collection by Garbage Collector.

 
Leave a comment

Posted by on July 7, 2010 in J2EE, Java

 

Tags: ,

Why Server Side Programming?

Server side programming is done to run the application on server from where all possible clients can access it whereas client side programming is done to run the application on client computers. Using Server Side Programming it is possible to make dynamic websites. Today we see a number of web-applications which are made using server side programming. This includes Google, Yahoo, Facebook and Wikipedia etc. Though there are hundreds of advantages of server side programming over client side, we will discuss here the most important of them.

Security: Server Side Applications are far better secure as compared to Client side applications. For eg: if a user is sending some sensitive information then the data is encrypted during transmission and decrypted at server side. Moreover all the code is on the server which is not accessible to any user. But in client side applications a user can decompile the application code to see the logic and security measures to use them for his benefit.

Maintainability: As all the code is on single machine it is easier to update or make any changes to the application whereas in Client Side Applications we have to update all the applications on each client machine. This saves a lot of cost and effort.

Browser Compatibility: In client side applications you have to make a different version for each type of OS. There are some platforms in market which provides the OS independence but then they also need an Interpreter which is OS specific. As the Server Side applications runs on server and browser is used for the data interchange there are no efforts applied to make it compatible. Moreover Ajax is a better technology that uses both client-side and server-side scripting. It can call a database without even refreshing the page at browser. The request made is transparent to the user since a new call to the server is done in the running window.

The most common scripting languages used to create server side applications are ASP.NET, Java Servlets, JSP, PHP and Ruby on Rails. There are various factors on which the choice of language used to develop application depends. With the advent of Cloud Computing the server side applications have grown faster.

 
Leave a comment

Posted by on July 5, 2010 in Programming

 

Spring Dependency Injection

Spring DI is the most fundamental feature that it provides. This feature has also been referred as Inversion of Control but after many discussions it was decided that DI is a better term.

Any application which is of practical use is composed of two or more Objects which communicate or use each other.  Traditionally each of those objects is responsible to get the reference of objects it uses but in DI objects are given references to the other objects when needed. All the bean creation and their injection are handled by Spring which acts as a container of beans. This provides loose coupling and also promotes programming to an Interface as the bean knows only about the type of bean but nothing about its implementation.

For Example: If the bean Store has declared Product property as an interface then the Product implementation becomes irrelevant.   The Product bean can be web-service implementation, an EJB or a mock test object.

Some other benefits of using DI:
• Reduction of boiler plate coding.
• As all metadata is in xml file and is easy to configure. This gives flexibility in testing and development.

 
Leave a comment

Posted by on July 5, 2010 in Java, Spring

 

Tags: ,

Java Example of Spring Dependency Injection

This tutorial uses the following:

    • Eclipse IDE
    • Spring Jars
    • commons-logging-api-1.1.jar
  1. Make a new Java Project in Eclipse.
  2. Add the following jars to its classpath:
    • commons-logging-api-1.1.jar
    • org.springframework.core-3.0.2.jar
    • org.springframework.beans-3.0.2.jar

The Spring jars can be taken from the dist folder of the expanded zip file. Here you have to include the two jars one for core and other for beans. Commons-logging is used for logging purpose.

  • Make a new class – Person.java. It is a 100% Java bean and has the proper getters and setters.
package com.spring.ioc.beans;

public class Person {

	private String name;

	private String address;

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * @param address the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}

}

  • Make an XML file and name it context.xml and save it in the src folder.  This file contains all the meta information needed for bean creation. Here the bean id is person and class has the full qualified name of the class. The “name” property of the class Person is populated with the value Mac.

<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="person" class="com.spring.ioc.beans.Person">
	<property name="name" value="Mac"></property>
	</bean>
</beans>

  • Make another class named Main.java.  Here an object of XmlBeanFactory loads the configurations from contect.xml file. Also its constructor takes an argument of ClassPathResource.
  • To get a bean from XmlBeanFactory object use its reference type to invoke the method getBean(<bean id>) and it will return the bean as defined in the spring context file.  The Object it returns is of type Object so you do have to cast it to the correct type.
package com.spring.ioc.main;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import com.spring.ioc.beans.Person;

/**
 * @author hgupta
 *
 */
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
				"context.xml"));
		Person person = (Person) beanFactory.getBean("person");
		System.out.println(person.getName());
	}

}

  • Run the Main class as Java Application and you will see the following output: The output is the name of the person which you configured in the xml file.

Output:

May 27, 2010 6:54:04 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [context.xml]

Mac

 
Leave a comment

Posted by on July 5, 2010 in Java, Spring

 

Tags: ,

Spring Framework Architecture

Spring framework is layered in architecture which consist several modules. All its modules are built on the top of its core container. These modules provide everything that a developer may use in the enterprise application development. He is always free to choose what all features he needs and eliminate the modules which are of no use. Its modular architecture enables integration with other frameworks without much hassle.


The Core Module: It provides the Dependency Injection (DI) feature which is the basic concept of spring framework. This module contains the BeanFactory an implementation of Factory Pattern which creates the bean as per the configurations provided by the developer in an XML file.

AOP Module: Its Aspect Oriented Programming module allows developer to define method-interceptors and point cuts to keep the concerns apart. It is configured at run time so the compilation step is skipped. It aims at declarative transaction management which are easier to maintain.

DAO Module: This provides an abstraction layer to the low level task of creating a connection, releasing it etc. It also maintains a hierarchy of meaningful exception rather than throwing complicated error codes of specific database vendors. It uses AOP to manage transactions. Transactions can also be managed programmatically.

ORM Module: Spring doesn’t provides its own ORM implementation but offers integrations with popular Object Relational mapping tools like Hibernate, iBATIS SQL Maps, Oracle TopLink and JPA etc.

JEE Module: It also provides support for JMX, JCA, EJB and JMS etc. In lots of cases, JCA (Java EE Connection API) is much like JDBC, except where JDBC is focused on database JCA focus on connecting to legacy systems.

Web Module: Spring comes with MVC framework which eases the task of developing web applications. It also integrates well with the most popular MVC frameworks like Struts, Tapestry, JSF, Wicket etc.

 
Leave a comment

Posted by on July 5, 2010 in Java, Spring

 

Tags: , ,