Search This Blog

Friday, January 06, 2006

Spring is HOT - An Introduction

This time I come up with an interesting framework and it's popularity is increasing in the Java development community. The so called framework is Spring. I insisted to learn spring becuase many of the requirements i have seen in the companies mention Spring framework. So, i decided to learn Spring.

I have decided to write series of blogs on spring. In this "Spring is HOT - An Introduction" , i will give an introduction to the spring frmework. Future blogs i will explain how to use Spring in the web developement. In this blog i am not going to teach you how to program in spring rather i will explain you what is spring and how it is richer than EJB.

If you think Spring is an framework in the place of Struts, you have wrong conception about the Spring . Spring is not only a framework, also it is an container to develope high end enterprise applications. Unlike EJB, it doesn't come up with more complex architecture rather it is a simple framewok to use in our application with all other services(Transaction,Security,etc.) wrapped up your code. All the services are bundled in the different jar files and you just add the required files into your project.

Spring uses Plain Old Java Object(POJO) paradigm to implement the services needed for your application. This facilitate the use of Spring framework out of the container. Easy to Unit Test the Spring enabled applications. Another salient feature of Spring is, it allows us to easily integrate with other frameworks like Struts, WebWorks, Hibernate, etc.

Spring is built using two programming techniques : Inversion of Control(IoC) and Aspect Oriented Programming(AOP). In my next blog i will write about IoC in detail. Spring is extensively implemented in the Banking solutions. I can see the prosperous year ahead for Spring in 2006.

Spring HomePage

Spring Downloads

List of Web frameworks in Java

We have lot of opensource frameworks available for Java developement. If we are closely looking at the each one, they have specific feature to highlight from the other frameworks. In this blog i just write the list of frameworks available and give the brief on it. I am not going to write about all the frameworks in the market. I will look into few, which is more familiar with us. Also i may miss some frameworks. I will write those frameworks in my future blogs.

Struts

Struts is the best implementation for Model View Controller(MVC) pattern and widely accepted by the developers. This framework is originated by the
Apache Foundation, a famous open source commuinty in Java. Recent days the Struts framework lossing its popularity to other frameworks in the market Java Server Faces(JSF),Spring,etc. But still its mostly used in all the web applications. Currently this project developement is stopped and started a sub project called Shale. Which is more like Java Server Faces(JSF)
.

Spring

Spring is a lightweight container and framework to develope and deploy the enterprise application. It is an alternative for Enterprise Java Beans(EJB). It reduce the complexity behind developing EJB's, and it uses only Plain Old Java Objects(POJOs) as the componenets. It uses two types of programming techniques : Inversion of control (also called as Dependency Injection) and Aspect Oriented Programming(AOP).

This framework become more popular amoung the J2EE developers becuse of its simplicity and its more flexible to add the persistance layer which ever you want for your application(ie,Hibernate, DAO,etc). Unlike EJBs it need not force you to add all the service to the container, you have the control of adding services to your application. The main pitfall of EJB is for an simple EJB also you have to bare the cost of container servies. But in spring this overhead is reduced. Every service has been modulrized and you have the option to add into your application. For your convenience, spring framework gives all the services in the different jar files. You can download it from here.

Also Spring gives you the MVC pattern to build your web application. This feature makes spring stand out from the other frameworks. Spring contains both container and framework. It gives the better way to integrate with EJBs and Struts framework. Those who feel EJB is more complicated, Spring will be the answer.

This framework is developer by Interface21 and home page is SpringFramework.


Java Server Faces(JSF)

Java Server Faces(JSF) is a java based web framework to develope web applications. Its is used inside an Java Server Pages(JSP) and gives more flexibility to design User Interfaces. This framework is designed and maintained by Sun Micro Systems. Craig McClanahan is the co-specification lead for this framework. Also he is the creator of Struts framework. You can read his blog to find more information about him. From his blog:

"It should come as no surprise that the most frequent questions I get asked center around the issue of which of these two web tier technologies an organization or individual developer should consider using. It makes sense to ask me, because I was the original creator of the Struts Framework, and was the co-specification lead for JavaServer Faces 1.0 (JSF).


Web Work

WebWork is a Java web-application development framework. It is built specifically with developer productivity and code simplicity in mind. WebWork is built on top of XWork, which provides a generic command pattern framework as well as an
Inversion of Control container. In addition to these features, WebWork provides robust support for building reusable UI templates, such as form controls, UI themes, internationalization, dynamic form parameter mapping to JavaBeans, robust client and server side validation, and much more.

WebWork's homepage is http://www.opensymphony.com/webwork.

Tapestry

Tapestry is an open-source framework for creating dynamic, robust, highly scalable web applications in Java. Tapestry complements and builds upon the standard Java Servlet API, and so it works in any servlet container or application server.

Tapestry divides a web application into a set of pages, each constructed from components. This provides a consistent structure, allowing the Tapestry framework to assume responsibility for key concerns such as URL construction and dispatch, persistent state storage on the client or on the server, user input validation, localization/internationalization, and exception reporting. Developing Tapestry applications involves creating HTML templates using plain HTML, and combining the templates with small amounts of Java code using (optional) XML descriptor files. In Tapestry, you create your application in terms of objects, and the methods and properties of those objects -- and specifically not in terms of URLs and query parameters. Tapestry brings true object oriented development to Java web applications.

This framework is developed by Apache foundation. You can access the documentation at the following URL Tapestry .

Wednesday, January 04, 2006

Adavance Java Interview Questions On Servlets Part 2

What is a servlet?
Servlets are modules that extend request/response-oriented servers,such as Java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company’s order database. Servlets are to servers what applets are to browsers. Unlike applets, however, servlets have no graphical user interface.

Whats the advantages using servlets over using CGI?
Servlets provide a way to generate dynamic documents that is both easier to write and faster to run. Servlets also address the problem of doing server-side programming with platform-specific APIs: they are developed with the Java Servlet API, a standard Java extension.

What are the general advantages and selling points of Servlets?
A servlet can handle multiple requests concurrently, and synchronize requests. This allows servlets to support systems such as online
real-time conferencing. Servlets can forward requests to other servers and servlets. Thus servlets can be used to balance load among several servers that mirror the same content, and to partition a single logical service over several servers, according to task type or organizational boundaries.

Which package provides interfaces and classes for writing servlets? javax

What’s the Servlet Interface?
The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or, more
commonly, by extending a class that implements it such as HttpServlet.Servlets > Generic Servlet > HttpServlet > MyServlet.
The Servlet interface declares, but does not implement, methods that manage the servlet and its communications with clients. Servlet writers provide some or all of these methods when developing a servlet.

When a servlet accepts a call from a client, it receives two objects. What are they?
ServletRequest (which encapsulates the communication from the client to the server) and ServletResponse (which encapsulates the communication from the servlet back to the client). ServletRequest and ServletResponse are interfaces defined inside javax.servlet package.

What information does ServletRequest allow access to?
Information such as the names of the parameters passed in by the client, the protocol (scheme) being used by the client, and the names
of the remote host that made the request and the server that received it. Also the input stream, as ServletInputStream.Servlets use the input stream to get data from clients that use application protocols such as the HTTP POST and GET methods.

What type of constraints can ServletResponse interface set on the client?
It can set the content length and MIME type of the reply. It also provides an output stream, ServletOutputStream and a Writer through
which the servlet can send the reply data.

Explain servlet lifecycle?
Each servlet has the same life cycle: first, the server loads and initializes the servlet (init()), then the servlet handles zero or more client requests (service()), after that the server removes the servlet (destroy()). Worth noting that the last step on some servers is done when they shut down.

How does HTTP Servlet handle client requests?
An HTTP Servlet handles client requests through its service method. The service method supports standard HTTP client requests by dispatching each request to a method designed to handle that request.

Adavance Java Interview Questions On Servlets Part1

What is servlet?
Servlets are modules that extend request/response-oriented servers, such as java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company's order database.

What are the classes and interfaces for servlets?
There are two packages in servlets and they are javax.servlet and javax.servlet.http.
Javax.servlet contains:

Interfaces Classes
Servlet Generic Servlet
ServletRequest ServletInputStream
ServletResponse ServletOutputStream
ServletConfig ServletException
ServletContext UnavailableException
SingleThreadModel

Javax.servlet.http contains:

Interfaces Classes
HttpServletRequest Cookie
HttpServletResponse HttpServlet
HttpSession HttpSessionBindingEvent
HttpSessionContext HttpUtils
HttpSeesionBindingListener

What is the difference between an applet and a servlet?
a) Servlets are to servers what applets are to browsers.
b) Applets must have graphical user interfaces whereas servlets have no graphical user interfaces.

what is the lifecycle of a servlet.
Each Servlet has the same life cycle:
a) A server loads and initializes the servlet by init () method.
b) The servlet handles zero or more client's requests through service( ) method.
c) The server removes the servlet through destroy() method.

What is the ServletConfig() and why are using ServletConfig ?
This interface is implemented by services in order to pass configuration information to a servlet when it is first loaded.A service writer implementing this interface must write methods
for the servlet to use to get its initialization parameters and the context in which it is running.

public interface ServletConfig
What is meant by the ServletContext() and use of the method ?
Ans: public interface ServletContext

The ServletContext interface gives servlets access to information about their environment ,and allows them to log significant events. Servlet writers decide what data to log. The interface is implemented by services, and used by servlets. Different virtual hosts should have different servlet
contexts.

What is use of parseQueryString ?
Parses a query string and builds a hashtable of key-value pairs, where the values are arrays
of strings. The query string should have the form of a string packaged by the GET or POST method.
(For example, it should have its key-value pairs delimited by ampersands (&) and its keys
separated from its values by equal signs (=).)
Note:
public static Hashtable parseQueryString(String s)

what are the types of servlets?
Genereic Servlets,HttpServlets.

what are the different methods in HttpServlet?
doGet(),doPost(),doHead,doDelete(),deTrace()

What is the difference between GET and POST?
a) doGet() method is used to get information, while doPost( ) method is used for posting information.
b) doGet() requests can't send large amount of information and is limited to 240-255 characters. However,
doPost( )requests passes all of its data, of unlimited length.
c) A doGet( ) request is appended to the request URL in a query string and this allows the exchange is visible to the client, whereas a doPost() request passes directly over the socket connection as part of its HTTP request body and the exchange are invisible to the client.