JSP Interview Questions

21 - What are the basic differences between Custom tag and Java Bean?
  • Using custom tag presentation logic separates from the business logic completely.
  • Custom tag created by developer and used by web designer whereas web designer cannot use java bean if does not know java.
  • Custom tag can manipulate jsp content whereas java bean cannot.

22 - What is Tag library descriptor (TLD)?
A tag library descriptor is an XML file that contains information about a library as a whole and about each tag contained in the library. TLDs are used by a web container to validate the tags and by JSP page development tools. TLD file name must have .tld extension and must be under WEB-INF folder.
23 - Why and how to disable session in a JSP page.
All JSP session are enabled by default. When jsp send a request it create a HttpSession object to maintain state for each unique client and session object use the server resource and stored on server side. This increase the traffic as session id is sent from server to client and client to send for each request and response. If there is no need to maintain a session on website it's better to disable session.
You need to set the session attribute of the page directive to false.
Syntax : <%@ page session="false" %>
24 - How to implement a thread-safe JSP page?
Jsp pages are not thread-safe by default, to make JSPs page thread-safe, you need to implement the SingleThreadModel that prevents two threads from accessing the service method at the same time.
Add the following directive in your JSP page declaration : <%@ page isThreadSafe="false" %>
25 - What is the difference between JSP and Servlet life cycles?
  • In servlet life cycle servlet object is created first. The init() method is invoked by the servlet container and the servlet is initialized by its arguments. Servlet's service() method is invoked next. And at the end, the destroy() method is invoked.
  • In JSP life cycle the JSP first convert to servlet and then servlet life cycle begins.

26 - Explain how to perform browser redirection from a JSP page?
Use implicit object of response to redirect from a jsp page. Example response.sendRedirect(http://www.tkhts.com); Or use <jsp:forward page="/login"> <jsp:param name="username" value="xyz" /> </jsp:forward> to forward the any output to client.
27 - How JSP Use MVC Model?
MVC stands for Model View Controller. In MVC JSP is use as a view/presentation part, to display the content on client browser, JSP use JSTL and EL to eliminate the java code from the view. Controller is a servlet act as a middleware and model is a POJO class which processes the data from the database.
28 - What is the difference between Servlet Context and Page Context ?
  • The ServletContext hold the reference to the overall context of the web application.
  • The PageContext is a collection of request and response references for a single JSP servlet instance, for handling errors, forwarding requests

29 - Can you implement the interface in the JSP file?
No,you cannot implement the interface in the JSP file.
30 - How to declare methods within JSP page?
you can declare method in JSP page to use within JSP. The methods can invoke any other methods you declare, or within JSP scriptlets and expressions.