Tutorial of Simple JSP Tiles application without Struts

There are very few resources available on internet which explains step by step integration of tiles in simple jsp page.
Apache Tilesâ„¢ is a templating framework built to simplify the development of web application user interfaces.

Tiles allows authors to define page fragments which can be assembled into a complete page at runtime. These fragments, or tiles, can be used as simple includes in order to reduce the duplication of common page elements or embedded within other tiles to develop a series of reusable templates. These templates streamline the development of a consistent look and feel across an entire application.

Ste p1 : Download the Tiles jar file from here.

Published
Categorized as JSP Tagged

Java – J2EE Interview Questions – 1

1. What is Generic Servlet ?
Answer : Defines a generic, protocol-independent servlet. GenericServlet implements the Servlet and ServletConfig interfaces. GenericServlet makes writing servlets easier. It provides simple versions of the lifecycle methods init and destroy. To use Protocol specific servlet like http protocol, we can use HttpServlet. Difference in HttpServlet and GenericServlet is that, HttpServlet provides few more methods like doGet and doPost. Same operations can be done in service method of generic servlet.  Read Servlet Life cycle


2. Can abstract class have Constructor ?
Answer : Yes. abstract class can have a constructor.


3. We cannot instantiate the abstract class, then what is the need of constructor and how it is called.
Answer : You may have not come across this situation. but answer i very easy. If want to initialize few parameters for all the subclasses then we can use the constructor of abstract class. Constructor calling mechanism is same as normal inheritance in java. Default constructor gets called automatically and to call other constructors we have to explicitly call it by using keyword “super”.


Published
Categorized as JAVA Tagged ,

J2EE – Servlet , JSP , JSF and JMS Version Table

Hi Friends,

Below table gives the information about the versions of J2EE (Java Table Version).

 
J2EE 1.2
JEE 5
Servlet2.22.32.42.5 new3 new
JSP1.11.22.0 new2.12.2
JSTL--1.11.21.2
Tomcat4.x5.x5.x6.x7.x
JMS1.0.21.0.21.11.11.1
JSF---1.22.0 new
J2EE Release DateDec 12, 1999Sep 24, 2001Nov 11, 2003May 11 , 2006Dec 2009
EJB1.12.02.13.03.0
Extra Info----JDK 5 and above required

Create Servlet using Annotation – Servlet 3.0

Before reading this article, i will suggest readers to go through the version table of J2EE.

One of the most appraised feature of Servlet 3.0 is the support of the annotations to create the Servlet. Before  this the Deployment Descriptor (web.xml) was used to create the servlet mapping , defining init parameters etc. All the settings can be now achieved without any entry in web.xml.

Prerequisite:

  1. JDK 5.0 and above.
  2. Tomcat 7
  3. Eclipse 3.6 – Helios (as of now, it only supports Tomcat 7)

We will start with creating the Servlet class :

Published
Categorized as Servlet Tagged

Cause and solution of “org.hibernate.LazyInitializationException: could not initialize proxy – no Session” error

In Hibernate, a common error faced by lots of developer is “org.hibernate.LazyInitializationException: could not initialize proxy – no Session“. In lots of forum the answer is no answer and most of forum have answer but there is no explanation for the beginners.

First, Lets reproduce this error in Hibernate.

Assume that Hibernate configuration (hibernate.cfg.xml) and  mapping file is already written.

Published
Categorized as Hibernate Tagged

Difference in SendRedirect() and RequestDispatcher() in Servlet

SendRedirect

  • This is the method of object HttpServlerResponse.
  • Request is redirected to client (Browser), and it will process the new URL.
  • End User can see on which page, url is redirected.
  • In Nutshell, Processing done at client side.

RequestDispatcher

  • This object can be accessed from HttpServletRequest.
  • Servlet will internally forward the request to another servlet or jsp page.
  • End user don’t know that which page is processed internally.
  • In Nutshell, Processing done at server side.
Published
Categorized as Servlet Tagged

Life Cycle of Servlet

Servlet is used in J2EE to create the dynamic web pages under Java Environment.  Like Applets, Servlet also have init() and destroy() methods. In this article i will explain the Life cycle of servlet.

Step 1 : After Compilation of the Servlet, the class file is loaded by the loader.

Step 2 : Then the Container instantiates the Servlet class by calling default Constructor. Read this article to know more that how container works for Servlet.

Servlet Life Cycle
Servlet Life Cycle

Published
Categorized as Servlet Tagged

Example to Override the init() method of the servlet

It is not recommended to override the init() method of the servlet.

Here is an example to override init() method of servlet:

    public class BookDBServlet ... {

        private BookstoreDB books;

        public void init(ServletConfig config) throws ServletException {

            // Store the ServletConfig object and log the initialization
            super.init(config);

            // Load the database to prepare for requests
            books = new BookstoreDB();
        }
        ...
    }

you must write super.init(config) code and after that program specific logic.

Published
Categorized as Servlet Tagged

Why not to write constructor in Servlet

Technically you can define constructors in servlet. But, the declared constructor cannot access the ServletConfig object or throw a ServletException.

Then why is it not customary to declare a constructor in a servlet? Because the init() method is used to perform servlet initialization. In JDK 1.0 (servlet were written in this version), constructors for dynamically loaded Java classes such as servlets cannot accept arguments. Therefore init() was used to initialize by passing the implemented object of ServletConfig interface and other needed parameters.
Also, Java constructors cannot be declared in interfaces. So, javax.servlet.Servlet interface cannot have a constructor that accepts a ServletConfig parameter. To overcome this, init() method is used for initialization instead of declaring a constructor.

Published
Categorized as Servlet Tagged