Pages

Wednesday 6 June 2012

JSP (Java Server Pages) Tutorial 
In this JSP tutorial, we will have in-depth learning of
JSP technology with simplified examples.
 
JSP 
JSP is another technology to create web application. It is an extension to the servlet. A JSP page contains
HTML code and JSP tags. The jsp pages are easier to maintain than servlet because we can separate designing and development. It provides some additional features such as Expression Language,Custom Tag etc.Problem in Servlet

Problem in Servlet 
  1. The development of servlet is a cumbersome and time consuming process, especially when you have to send a long HTML page.
  2. The servlet code needs to be updated and recompiled if we have to change the look and feel of the application.
How JSP overcomes the problem of servlet ? 
The JSP technology overcomes these problems. JSP provides many tags and implicit objects so it saves our
time. If you have a HTML file but you want some java code, JSP is far better than servlet. IF you change
the JSP page, you don't need to do anything. Thus JSP is easy to maintain. The JSP is internally converted
into servlet. In JSP pages, custom tags and JSP Expression Language provide code reusability and
separation of concerns.
 
Life cycle of a JSP Page 
The JSP pages follows these phases:
 
  • Translation of JSP Page
  • Compilation of JSP Page
  • Classloading (class file is loaded by the classloader)
  • Instantiation (Object of the Generated Servlet is created).
  • Initialization ( jspInit() method is invoked by the container).
  • Reqeust processing ( _jspService() method is invoked by the container).
  • Destroy ( jspDestroy() method is invoked by the container).

 

As depicted in the above diagram, JSP page is translated into servlet by the help of JSP translator. The JSP
translator is a part of webserver that is responsible to translate the JSP page into servlet. Afterthat Servlet
page is compiled by the compiler and gets converted into the class file. Moreover, all the processes that
happens in servlet is performed on JSP later like initialization, commiting response to the browser and
destroy.
 
Creating a simple JSP Page 
To create the first jsp page, write some html code as given below, and save it by .jsp extention. We have
save this file as index.jsp. Put it in a folder and paste the folder in the web-apps directory in apache tomcat
to run the jsp page.
 
index.jsp

 

How to run a simple JSP Page ? 
Follow the following steps to execute this JSP page:
Start the server
put the jsp file in a folder and deploy on the server
visit the browser by the url http://localhost:portno/contextRoot/jspfile e.g. http://localhost:8888
/myapplication/index.jsp
 
Do I need to follow directory structure to run a simple JSP ?No, there is no need of directory structure if you don't have class files or tld files. For example, put jsp files
in a folder directly and deploy that folder.It will be running fine.But if you are using bean class, Servlet or
tld file then directory structure is required.
 
Directory structure of JSP 

The directory structure of JSP page is same as servlet. We contains the jsp page outside the WEB-INF

folder or in any directory.

 

7 comments: