JSP Tutorial

♠ Posted by Java Tutorials at 9:13 AM


What is JSP and why do we need JSP?

JSP (JavaServer Pages) is server side technology to create dynamic java web application. JSP can be thought as an extension to servlet technology because it provides features to easily create user views.
JSP Page consists of HTML code and provide option to include java code for dynamic content. Since web applications contain a lot of user screens, JSPs are used a lot in web applications. To bridge the gap between java code and HTML in JSP, it provides additional features such as JSP Tags, Expression Language, Custom tags. This makes it easy to understand and helps a web developer to quickly develop JSP pages.

Advantages of JSP over Servlets?

·         We can generate HTML response from servlets also but the process is cumbersome and error prone, when it comes to writing a complex HTML response, writing in a servlet will be a nightmare. JSP helps in this situation and provide us flexibility to write normal HTML page and include our java code only where it’s required.
·         JSP provides additional features such as tag libraries, expression language, custom tags that helps in faster development of user views.
·         JSP pages are easy to deploy, we just need to replace the modified page in the server and container takes care of the deployment. For servlets, we need to recompile and deploy whole project again.
Actually Servlet and JSPs compliment each other. We should use Servlet as server side controller and to communicate with model classes whereas JSPs should be used for presentation layer.

Life cycle of JSP Page

JSP life cycle is also managed by container. Usually every web container that contains servlet container also contains JSP container for managing JSP pages.
JSP pages life cycle phases are:
·         Translation – JSP pages doesn’t look like normal java classes, actually JSP container parse the JSP pages and translate them to generate corresponding servlet source code. If JSP file name is home.jsp, usually its named as home_jsp.java.
·         Compilation – If the translation is successful, then container compiles the generated servlet source file to generate class file.
·         Class Loading – Once JSP is compiled as servlet class, its lifecycle is similar to servlet and it gets loaded into memory.
·         Instance Creation – After JSP class is loaded into memory, its object is instantiated by the container.
·         Initialization – The JSP class is then initialized and it transforms from a normal class to servlet. After initialization, ServletConfig and ServletContext objects become accessible to JSP class.
·         Request Processing – For every client request, a new thread is spawned with ServletRequest and ServletResponse to process and generate the HTML response.
·         Destroy – Last phase of JSP life cycle where it’s unloaded into memory.

Life cycle methods of JSP

JSP lifecycle methods are:
K.     jspInit() declared in JspPage interface. This method is called only once in JSP lifecycle to initialize config params.
L.      _jspService(HttpServletRequest request, HttpServletResponse response) declared in HttpJspPage interface and response for handling client requests.
M.    jspDestroy() declared in JspPage interface to unload the JSP from memory.



=========================================================================



0 comments :

Post a Comment