More on JSP by R4R Team

 Life-cycle of JSP:

When a request is mapped to a JSP page for the first time, it translates the JSP page into a servlet class and compiles the class. It is this servlet that services the client requests.

A JSP page has seven phases in its lifecycle, as listed below in the sequence of occurrence:

c)Loading the class
d)Instantiating the class
e)jspInit() invocation
f)jspService() invocation
g)jspDestroy() invocation

Difference between jsp and servlet.
Some important difference between jsp and servlet are listed below...

                        JSP :
JSP is a webpage scripting language that can generate dynamic content.
JSP run slower compared to Servlet as it takes compilation time to convert into Java Servlets.

It’s easier to code in JSP than in Java Servlets.

In MVC, jsp act as a view.

JSP are generally preferred when there is not much processing of data required.

The advantage of JSP programming over servlets is that we can build custom tags which can directly call Java beans

We can achieve functionality of JSP at client side by running JavaScript at client side.


Servlets are Java programs that are already compiled which also creates dynamic web content.
Servlets run faster compared to JSP.

Its little much code to write here.

In MVC, servlet act as a controller.

servlets are best for use when there is more processing and manipulation involved.

There is no such custom tag facility in servlets.

There are no such methods for servlets.

Advantages of JSP over Servlet are given bellow:

1)JSP is a serverside technology to make content generation a simple appear.
2)The advantage of JSP is that they are document-centric. Servlets, on the other hand, look and act like programs.
3)A Java Server Page can contain Java program fragments that instantiate and execute Java classes, but these occur inside an HTML template file and are primarily used to generate dynamic content.
4) Some of the JSP functionality can be achieved on the client, using JavaScript.
5)The power of JSP is that it is server-based and provides a framework for Web application development.

Requirement of a tag library:

A collection of custom tags is called a Tag Library. Recurring tasks are handled more easily and reused across multiple applications to increase productivity. They are used by Web Application designers who focus on presentation rather than accessing database or other services. Some popular libraries are String tag library and Apache display tag library.
Explain JSP URL mapping? What is URL hiding or protecting the JSP page?
The JSP resources usually reside directly or under subdirectories (e.g. myPath) of the document root, which are directly accessible to the user through the URL. If you want to protect your Web resources then hiding the JSP files behind the WEB-INF directory can protect the JSP files, css (cascading style sheets) files, Java Script files, pdf files, image files, html files etc from direct access. The request should be made to a servlet who is responsible for authenticating and authorising the user before returning the protected JSP page or its resources.

JSP expression:
A JSP expression is used to write an output without using the out.print statement. It can be said as a shorthand representation for scriptlets. An expression is written between the <%= and %> tags. It is not required to end the expression with a semicolon, as it implicitly adds a semicolon to all the expressions within the expression tags.

Static files in a JSP page:
Static pages are always included using JSP include directive. This way the inclusion is performed in the translation phase once. Note that a relative URL must be supplied for file attribute. Although static resources may be included, it is not preferred as each request requires inclusion.
what usage of Declaration Tag explain with example?.
Declaration tag is used to define functions, methods and variables that will be used in Java Server Pages(JSP).

Notation of the Declaration tag is given below:

<%! %>

At the start of Declaration tag one must place <%! Inside Declaration tag one can declare variables or methods. Declaration tag ends with the notation %>. Also care must be taken to place a semicolon that is ; at the end of each code placed inside Declaration tag.

General syntax of Declaration Tag:

<%! //start of declaration tag


statement2; //variables or methods declaration



%> //end of declaration tag

For example:


private int example = 0 ;

private int test = 5 ; %>

Types of JSTL tags:
Based on the JSTL functions, they are categorized into five types.

1)Core Tags – Core tags provide support for iteration, conditional logic, catch exception, url, forward or redirect response etc.
2)Formatting and Localization Tags – These tags are provided for formatting of Numbers, Dates and i18n support through locales and resource bundles.
3)SQL Tags – JSTL SQL Tags provide support for interaction with relational databases such as Oracle, MySql etc.
4)XML Tags – XML tags are used to work with XML documents such as parsing XML, transforming XML data and XPath expressions evaluation.
5)JSTL Functions Tags – JSTL tags provide a number of functions that we can use to perform common operation, most of them are for String manipulation such as String Concatenation, Split String etc.

Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!