Friday, May 10, 2013

What is a JSP..? components of a JSP


JSP:

A Jsp refers to Java Server Pages, a dynamic web component runs inside server. A Jsp is also a servelt converted by jsp compiler.

According to Jsp specification every server vendor must have their own jsp compiler as part of server and their own servelt that extends HttpServlet.

As part of Tomcat we can observe HttpJspBase extending HttpServlet which is Tomcat's proprietary class to handle jsp's.

Jsp Compiler:

A Jsp compiler is a tool (program) as part of every server supporting jsp's which converts the jsp into corresponding servlet following jsp specification.

Jsp Life Cycle:

Similar to servlet, a jsp (which will be converted into servelt) can also be illustrated with the help of  life cycle methods.

jspInit(), _jspService() and jspDestroy() are the life cycle methods of a jsp.

jspInit() will be called immediately when the jsp(generated servlet) object is created and will be called only once .

For every client request to the jsp _jspService() will be called by the servlet container.

When the project got undeployed from the server, jspDestroy() will be called.



A jsp is a program which can contain different components as listed below.

--> Template text

--> Scriptlet

--> Jsp declarations

--> Jsp Expressions

--> Jsp Directives

--> Jsp action tags

--> Jsp custom tags

--> EL expressions

A Jsp file must have an extension of ' .jsp ' and all the jsp's as part of the web application will be placed outside WEB-INF folder. (only server can access files inside WEB-INF )

Template text:

Normal static text written in a jsp is called 'Template text'. What ever we write the template text as part of jsp, will be converted by jsp compiler into corresponding servlet code as shown.


That means, if we want to display normal text to user , we will use template text. Here, text which written in jsp is the template text. 


Scriptlet:

A scriptlet is used to write java code as part of jsp. Java code as part of jsp must be enclosed in a scriptlet (<% %>).

Syntax: 


Once we run the application, jsp compiler will convert the above code into corresponding servlet code and is placed inside _jspService().

It can be observed as part of the generated servlet by the jsp compiler,


Jsp Declarations:

A Jsp declaration is used to write instance / static variables and methods as part of Jsp.

The variable or(and) method declarations have to be written inside <%!  %>

Syntax:


Once jsp compiler converts this jsp into corresponding servlet, the above code will be placed outside the _jspService() and inside the generated servlet class.

The generated code by the jsp compiler will look like,


Jsp Expressions:

A Jsp expression is used to display the content available in a variable to the client.

The Java code has to be write inside Jsp Expression ( <%=  %> ), in order to display the content available in that variable.

Syntax:


Once Jsp compiler has converted the jsp into corresponding servlet, the above code will be placed as part of the generated servlet as shown.



Jsp Directives:

Jsp Directives are used to give instructions to the jsp compiler. <%@  %> represents a jsp directive.

Syntax:



Genearated Servlet by the jsp compiler:

Let's see the generated servlet by the jsp compiler corresponding to our jsp 'sample.jsp'



How and when the jsp is converted into servlet:

A jsp can be configured as part of web.xml as shown below.


If we didn't mention load-on-startup tag for the jsp, when we deploy the application server will read the contents of web.xml, but jsp compiler won't convert jsp into servlet.

When we send the request to the corresponding jsp, jsp compiler will convert the jsp (sample.jsp) into corresponding servlet by following jsp specification.

If we use load-on-startup tag for the jsp in web.xml,


Jsp compiler will convert the jsp into corresponding servlet when we deploy our application in the server. To the generated servlet, container will create object and call the life cycle methods initially jspInit(). It will be calles only once through out the life time of the jsp (converted servlet).

When the client sends the request to the jsp, every time container will call _jspService(). It will be called for every request.

jspDestroy() will be called by the servlet container only once , when the application has been undeployed from the server.


Note:

As per jsp specification, we should not write java code as part of a jsp, which means we should use tags instead of direct java code.

For this reason,components related to java code ( scriptlet, jsp declarations, jsp expressions which ) should be eliminated from jsp.

In place of java code we can use corresponding tags as part of jsp.

Jsp action tags, custom tags and EL expressions( non java code ) will resolve the problem of writing java code as part of jsp and will be discussed in next article



No comments:

Post a Comment