Jsp interview questions

What do you understand of the JSP technology and why is it used?

Java Server Pages (JSP) is a cross-platform technology residing on the presentation layer and is the part of the SUN’s J2EE platform. JSPs are like standard HTML web pages with Java code embedded inside. JSP files has the *.jsp file-extension. J2EE platform packages a JSP compiler to produce a Servlet class from the JSP page.

JSP technology enables the web developers to add Java code and some predefined actions into the static content. Java server pages get converted into Java servlet by the JSP compiler.

 What are the advantages of JSP over Servlet?

JSP is a server-side construct to simplify the dynamic generation of the HTML content. One of its advantages is that JSP is document-centric. While the Servlets purely behave like the programs. A JSP Page can consist of Java code fragments that can instantiate and invokes the methods of Java classes. All of this occurs inside an HTML template file which is mainly used to produce dynamic content. However, some of the JSP functionality can be achieved at the client-end using JavaScript. The power of JSP is that it is server-based and provides a framework for the Web application.

Advantages of JSP.

JSP represents an HTML page embed with Java code.
JSP is cross-platform technology.
JSP can create database-driven Web applications.
JSP enables Server-side programming abilities.

What are JSP lifecycle methods?

The JSP container executes the jspInit() method the first time it initializes the JSP.
Whenever the container receives a request, it invokes the _jspService() method, processes the request and generates a response.
The container calls the jspDestroy() method for cleaning up the memory allocated for the JSP.

Why is it not mandatory to configure the JSP standard tags in the web.xml?

There is no need to add the JSP standard tags to the web.xml file because the META-INF directory corresponding to the JSTL jar files contains the TLD files.

While loading the web application the container detects the TLD files present in the META-INF directory of the JAR file. It then automatically configures them and makes available for the JSP pages. You just have to add them to the JSP page using <taglib> directive.

What are the different JSP scripting elements?

There are three types of scripting language elements:

Declarations,
Scriptlets, and
Expressions.


What is the logic behind a scriptlet in JSP?

A scriptlet holds the executable Java code which runs whenever the JSP gets loaded. The scriptlet passes its code to the service() method while the JSP is getting compiled to a servlet. So all the scriptlet variables and methods become local to the service() method. A scriptlet is coded between the <% and %> tags and the container call it while processing the request.

What is a JSP declaration?

JSP Declarations help to declare the class variables and methods in a JSP page. They get initialized along with the initialization of the class. Everything in a <declaration> is accessible to the whole JSP page. You can encircle a declaration block within the <%! And %> tags.

What is a JSP expression?

A JSP expression helps to write an output without using the <out.print statement>. You can see it as a shorthand description for the scriptlets. And as usual, you delimit an expression using the <%= and %>tags.

Ending the expression with a semicolon is not mandatory, as it gets automatically added to the <expressions> within the expression tags.

Source:

JSP interview questions and answers