JSP interview questions [ Part 1 ]



1. What is JSP and why do we need it?
Java Server Pages technology (JSP) is used to create dynamic web page. It is an extension to the servlet technology. A JSP page is internally converted into servlet.
JSP is actually developed to
1. lower the burden on the developer as the Servlet coding developement will be taken care by jsp compiler and can make use of some implicit variables.
2. allow a non-java programmer can make use of JSP to develop a web application by knowing only a few tags and their usage.
3. to resemble the MVC arch.


2. Explain JSP Compilation Model?
When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page. If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP engine compiles the page.
The compilation process involves three steps:
Parsing the JSP.
Turning the JSP into a servlet.
Compiling the servlet.

3. Difference between JSP and Servlet?
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
   
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.

4. Explain JSP Life cycle?
A JSP page is converted into Servlet in order to service requests. The translation of a JSP page to a Servlet is called Lifecycle of JSP. JSP Lifecycle consists of following steps.
Translation of JSP to Servlet code.
Compilation of Servlet to bytecode.
Loading Servlet class.
Creating servlet instance.
Initialization by calling jspInit() method
Request Processing by calling _jspService() method
Destroying by calling jspDestroy() method

Web Container translates JSP code into a servlet class source(.java) file, then compiles that into a java servlet class. In the third step, the servlet class bytecode is loaded using classloader. The Container then creates an instance of that servlet class.
The initialized servlet can now service request. For each request the Web Container call the _jspService() method. When the Container removes the servlet instance from service, it calls the jspDestroy() method to perform any required clean up.

5. Explain the types of directives in JSP

-The page directive
-The include directive
-The taglib directive
 
6. How can we avoid direct access of JSP pages from client browser?

 
7. Difference between HTML and JSP Comment?
JSP Comments are removed by the JSP Engine during the translation phase (JSP Comments are not even part of the compilation unit) whereas the HTML Comments are treated like any other HTML tage and hence they are maintained throughout. This is reason why we can easily see the HTML comments in the browse by viewing the source of the page.
 
8. Is it possible to use java comment in JSP?
yes
 
9. What is Scriptlet, Expression and Declaration in JSP?

10. What are JSP implicit objects?
Implicit Objects and their corresponding classes:

out javax.servlet.jsp.JspWriter 
request javax.servlet.http.HttpServletRequest
response javax.servlet.http.HttpServletResponse
session javax.servlet.http.HttpSession
application javax.servlet.ServletContext
exception javax.servlet.jsp.JspException
page java.lang.Object pageContext javax.servlet.jsp.
PageContext config javax.servlet.ServletConfig

Out: This is used for writing content to the client (browser). It has several methods which can be used for properly formatting output message to the browser and for dealing with the buffer.

Request: The main purpose of request implicit object is to get the data on a JSP page which has been entered by user on the previous JSP page. While dealing with login and signup forms in JSP we often prompts user to fill in those details, this object is then used to get those entered details on an another JSP page (action page) for validation and other purposes.

Response: It is basically used for modfying or delaing with the response which is being sent to the client(browser) after processing the request.

Session: It is most frequently used implicit object, which is used for storing the user?s data to make it available on other JSP pages till the user session is active.

Application: This is used for getting application-wide initialization parameters and to maintain useful data across whole JSP application.

Exception: Exception implicit object is used in exception handling for displaying the error messages. This object is only available to the JSP pages, which has isErrorPage set to true.

Page: Page implicit object is a reference to the current Servlet instance (Converted Servlet, generated during translation phase from a JSP page). We can simply use this in place of it. I?m not covering it in detail as it is rarely used and not a useful implicit object while building a JSP application.

pageContext: It is used for accessing page, request, application and session attributes.

Config: This is a Servlet configuration object and mainly used for accessing getting configuration information such as servlet context, servlet name, configuration parameters etc.

11. Use of page implicit object?

The page implicit object is of type Object and it is assigned a reference to the servlet that executing the _jspService() method. Page is the instance of the JSP page's servlet processing the current request. Not typically used by JSP page authors. Thus in the Servlet generated by tomcat the page object is created as
Object page = this;
Since page is a variable of type Object, it cannot be used to directly call the servlet methods. To access any of the methods of the servlet through page it must be first cast to type Servlet.
<%= this.getServletInfo(); %>
<%= ((Servlet)page).getServletInfo(); %>
But the following code will generate error, because can not use page directly without casting:
<%= page.getServletInfo(); %>
Note that page is not the same as the pageContext object

12. Use of application implicit object?
In JSP, application is an implicit object of type ServletContext.
The instance of ServletContext is created only once by the web container when application or project is deployed on the server.
This object can be used to get initialization parameter from configuaration file (web.xml). It can also be used to get, set or remove attribute from the application scope.
This initialization parameter can be used by all jsp pages.
 
13. Can we use JSP implicit objects in JSP Declaration?
 
14. Which implicit object is not available in normal JSP pages?
JSP exception implicit object is not available in normal JSP pages and it?s used in JSP error pages only to catch the exception thrown by the JSP pages and provide useful message to the client.

15. What are the benefits of PageContext implicit object?

16. How do we configure init params for JSP?
 
17. What is difference between include directive and jsp:include action?

18. What is JSP Expression Language and what are it?s benefits?
The Expression Language (EL) simplifies the accessibility of data stored in the Java Bean component, and other objects like request, session, application etc.
There are many implicit objects, operators and reserve words in EL.
 
19. What are JSP EL implicit objects and how it?s different from JSP implicit Objects?

20. How do we print ?<br> creates a new line in HTML? in JSP?
We can use c:outescapeXml attribute to escape the HTML elements so that it get?s shown as text in the browser, for this scenario we will write code like below.
<c:out value="<br> creates a new line in HTML" escapeXml="true"></c:out>

21. What is jsp­config in deployment descriptor?

22. How to ignore the EL expression evaluation in a JSP?
We can deactivate EL evaluation in a single JSP page by specifying the attribute isELIgnored=?true? in the page directive as follows:
<%@ page isELIgnored="true" %>



Related Post

Comments


©candidjava.com