JSP Interview Questions
1. What are the
implicit objects in JSP?
JSP has following implicit objects:
1. Request
2. Response
3. Application
4. Exception
5. Page
6. Config
7. Session
2. How will you extend
JSP code?
We can extend JSP code by using Tag
libraries and Custom actions.
3. How will you handle
runtime exceptions in JSP?
We use Errorpage attribute in JSP to
catch runtime exceptions. This attribute forwards user request to the error
page automatically.
4. How will you prevent
multiple submits of a page that come by clicking refresh button multiple times?
We can use Post Redirect Get (PRG)
pattern to solve the issue of multiple submission of same data. It works as
follows:
First time when a user submits a form
to server by POST or GET method, then we update the state in application
database. Then we send a redirect response to send reply to client. Then we
load a view by using GET command. There is no data is sent in this. Since this
a new JSP page, it is safe from multiple submits. The code that processes the
request is idempotent. So it does not do same action twice for same request.
5. How will you
implement a thread safe JSP page?
We can use SingleThreadModel
Interface to implement a thread safe JSP page. We can also add <%@page
isThreadSafe=”false” %> directive in JSP page to make it thread safe.
6. How will you include
a static file in a JSP page?
We can use include directive of JSP
to include a Static page in JSP. In this approach, we use translation phase to
include a static page. We have to specify the URL of the resource to be
included as file attribute in this directive.
E.g. <%@ include file="footer.html" %>
7. What are the
lifecycle methods of a JSP?
A JSP has following lifecycle
methods:
1. jspInit(): This method is invoked when the JSP is called for the
first time. We can do initial setup for servicing a request in this method.
2. _jspService(): This method is used to serve every request of the
JSP.
3. jspDestroy(): Once we remove a JSP from the container, we call this
method. It is used for cleanup of resources like Database connections etc.
8. What are the
advantages of using JSP in web architecture?
We get following advantages by using
JSP in web architecture:
1. Performance: JSP provides very good performance due to their design
of using same code to service multiple requests.
2. Fast: Since JSP is pre-compiled, server can serve the pages very
fast.
3. Extendable: JSP is based on Java Servlets. This helps in extending
JSP architecture with other Java technologies like JDBC, JMS, JNDI etc.
4. Design: It is easier to design user interface with JSP, since it is
very close to HTML. UI designers can create a JSP with mock data and developers
can later provide implementation of dynamic data.
9. What is the advantage of JSP over Javascript?
In
JSP we can write Java code seamlessly. It allows for writing code that can
interact with the rest of the application. Javascript code is mostly executed
at client side. This limits the tasks that can be done in Javascript code. We
cannot connect to database server from Javascript at the client side.
10. What is the Lifecycle of JSP?
JSP
has following lifecycle stages:
Compilation:
When a request is made for a JSP, the corresponding JSP is converted into
Servlet and compiled. If there is already a compiled form of JSP and there is
not change in JSP page since last compilation, this stage does not do anything.
Initialization:
In this stage, jspInit() method is called to initialize any data or code that
will be later used multiple times in _jspService() method.
Service: In
this stage, with each request to JSP, _jspService() method is called to service
the request. This is the core logic of JSP that generates response for request.
Destroy: In
this stage, JSP is removed from the container/server. Just before removal, this
stage performs the cleanup of any resources held by JSP.
11.What is a JSP expression?
A
JSP expression is an element of a JSP page that is used to evaluate a Java
expression and convert into a String. This String is replaced into the
locations wherever the expression occurs in JSP page.
E.g. <%= expression =%>
12. What are the different types of directive tags in JSP?
JSP
has following directive tags:
1. Page: This directive is used for page related attributes. It can be
put anywhere in the JSP page. But by convention we put it on the top of the
page.
E.g. <%@
page attribute="value" %>
2. Taglib: We can create custom tags in JSP and use these by taglib
directive in a JSP page.
E.g. <%@
taglib uri=“abc.html” prefix=“tag_prefix” >
3. Include: We use include directive to read a file and merge its
content with the JSP page. This is done during compilation stage.
E.g. <%@
include file="relative url" >
13. What is session attribute in JSP?
Session
attribute in JSP is used for HTTP session mechanism. If we do not want to use
HTTP session in JSP, then we set this attribute to false. If it is set to true,
we can use built in session object in JSP.
14. What are the different scopes of a JSP object?
A
JSP object, implicit or explicit, can have one of the following scopes:
Page: In this
scope, the object is accessible from the page where it was created. Important
point here is that when a user refreshes the page, the objects of this scope
also get created again.
Request: In
request scope, the object is accessible to the HTTP request that created this
object.
Session: In
this scope, the object is available throughout the same HTTP session.
Application:
This is the widest scope. The object is available throughout the application in
which JSP was created.
15. What is pageContext in JSP?
In JSP, pageContext is an implicit object . This is used for
storing and accessing all the page scope objects of JSP. It is an instance of
the PageContext class from javax.servlet.jsp package.
16. What is the use of jsp:useBean in JSP?
We use jsp:useBean to invoke the methods of a Java Bean class. The Java Bean class has some data and setter/getters to access the data. With this tag, container will try to locate the bean. If bean is not already loaded then it will create an instance of a bean and load it. Later this bean can be used in expressions or JSP code.
17. What is difference between include Directive and include Action of JSP?
Some of the main differences between include Directive and include Action are as follows:
1. Include directive is called at translation phase to include content in JSP. Include Action is executed during runtime of JSP.
2. It is not possible to pass parameters to include directive. Include action can accept parameters by jsp:param tag.
3. Include directive is just copying of content from another file to JSP code and then it goes through compilation. Include action will dynamically process the resource being called and then include it in the JSP page.
18. How will you use other Java files of your application in JSP code?
We can use import tag to import a Java file in JSP code. Once a file is imported, it can be used by JSP code. It is a very convenient method to use Java classes in JSP code. For better organization of Java code, we should create a package of classes that we are planning to use in JSP code.
19. How will you use an existing class and extend it to use in the JSP?
We can use extends attribute in include tag to use an existing class and extend it in the current JSP.
E.g.<%@ include page extends=“parent_class” %>
20. Why _jspService method starts with _ symbol in JSP?
All the code that we write in a JSP goes into _jspService method during translation phase. We cannot override this method. Where as other lifecycle methods jspInit() and jspDestroy() can be overridden. It appears that container uses _ symbol to distinguish the method that cannot be overridden by client code.
21. Why do we use tag library in JSP?
At times we want to create a UI framework with custom tags. In such a scenario, taglib is a very good feature of JSP. With taglib we can create tags that can provide custom features. Taglib is also a nice way to communicate with UI designers who can use custom tags in the html without going into the details of how the code is implemented. Another benefit of taglib is reusability of the code. This promotes writing code only once and using is multiple times.
22. What is the different type of tag library groups in JSTL?
JSTL stands for JavaServer Pages Standard Tag Library. In JSTL, we have a collection of JSP tags that can be used in different scenarios. There are following main groups of tags in JSTL:
Core tags
SQL tags
Formatting tags
XML tags
JSTL Functions
23. How will you pass information from one JSP to another JSP?
We can pass information from one JSP to another by using implicit objects. If different JSP are called in same session, we can use session object to pass information from one JSP to another. If we want to pass information from one JSP to another JSP included in the main JSP, then we can use jsp:param to pass this information.
24. How will you call a stored procedure from JSP?
JSP allows running Java code from a .jsp file. We can call a stored procedure by using JDBC code. We can call a CallableStatement from JSP code to invoke a stored procedure. If we are using Spring framework, then we can use JdbcTemplate class to invoke stored procedure from a JSP.
25. Can we override _jspService() method in JSP?
No, JSP specification does not allow overriding of _jspService method in JSP. We can override other methods like jspInit() and jspDestroy().
26. What is a directive in JSP?
JSP directive is a mechanism to pass message to JSP container. JSP directive does not produce an output to the page. But it communicates with JSP container.
E.g. <%@include ..%> directive is used for telling JSP container to include the content of another file during translation of JSP. There can be zero or more attributes in a directive to pass additional information to JSP container. Some of the important directives in JSP are: page, include and taglib.
27. How will you implement Session tracking in JSP?
We can use different mechanisms to implement Session tracking JSP. Some these mechanisms are as follows:
Cookies: We can use cookie to set session information and pass it to web client. In subsequent requests we can use the information in cookie to track session.
Hidden Form Field: We can send session id in a hidden field in HTML form. By using this we can track session.
Session object: We can use the built in session object to track session in JSP.
URL Rewriting: We can also add session id at the end of a URL.
Like- www.abcserver.com?sessionid=1234
28. How do you debug code in JSP?
In simplest form we can write logger statements or System.out.println() statements to write messages to log files. When we call a JSP, the log messages get written to logs. With useful information getting logged we can easily debug the code. Another option in debugging is to link JSP container with an IDE. Once we link IDE debugger to JSP Engine, we can use standard operations of debugging like breakpoint, step through etc.
Comments