Top 50 JSP Interview Questions | JSP Interview Questions

Below are the Top 50 JSP Interview Questions based on the candidate's experiences and the company's interview pattern.

1. What is JSP?
JSP stands for Java Server Pages. It is a presentation layer technology independent of the platform. It comes with SUN’s J2EE platforms. They are like HTML pages but with Java code pieces embedded in them. They are saved with a .jsp extension. They are compiled using the JSP compiler in the background and generate a Servlet from the page.
2. What is the 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 databases or other services. Some popular libraries are the String tag library and Apache display tag library.
3. Explain Implicit objects in JSP.
Objects created by the web container and contain information regarding a particular request, application or page are called Implicit Objects. They are :
1) response
2) exception
3) application
4) request
5) session
6) page
7) out
8) config
9) pageContext


4.  Is JSP technology extensible?
Yes, JSP is easily extensible by the use and modification of tags, or custom actions, encapsulated in tag libraries.
5. Can a subsequent request be accessed with one’s servlet code, if a request attribute is already sent in his JSP?
The request goes out of scope, thus, it cannot be accessed. However, if a request attribute is set in one’s servlet, then it can be accessed in his JSP.
A JSP is a server-side component and the page is translated to a Java servlet, and then executed. Only HTML code is given as output.


6.  How to include 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 the file attribute. Although static resources may be included, it is not preferred as each request requires inclusion.
7. Why is it that JComponent have add() and remove() methods but Component doesn’t?
JComponent is a subclass of Container. It contains other Components and JComponents.
8. How can a thread-safe JSP page be implemented?
It can be done by having them implemented by the SingleThreadModel Interface. Add <%@page isThreadSafe=”false” %> directive in the JSP page.
9. How can the output of the JSP or servlet page be prevented from being cached by the browser?
Using appropriate HTTP header attributes to prevent the dynamic content output by a JSP page from being cached by the browser.
10.  How to restrict page errors display in a JSP page?
By setting up an “ErrorPage” attribute of PAGE directory to the name of the error page in the JSP page, and then in the error JSP page set “isErrorpage=”TRUE”, Errors can be stopped from getting displayed.


11. Explain JSP lifecycle methods. 
1) jsplnit(): The container calls this to initialize servlet instance. It is called only once for the servlet instance and preceded every other method. 2)
2) jspService(): The container calls this for each request and passes it on to the objects. 3)
3) Destroy(): It is called by the container just before destruction of the instance.
12. Define Expression tag?
Expression tag is used to insert Java values directly in the output. Its syntax is
13. How can information from one JSP be passed to another JSP?
The tag <Jsp:param> allows us to pass information between multiple Jsp’s.
14. Explain the uses of <jsp:usebean> tag.
scope= "page | application"
class="A.class"  type="A.class"
This tag creates an instance of java bean. It first tries to find if bean instances already exist and assign stores a reference in the variable. Type is also specified; otherwise, it instantiates from the specified class storing a reference in the new variable.
15.  Explain the various scope values for <jsp:useBean> tag.
<jsp:useBean> tag is used to use any java object in the jsp page. Some scope values are :
1) application
2) request
3) page
4) session


16.  Explain JSP directives.
JSP directives are messages to JSP Engine. They serve as a message from page to container and control the processing of the entire page. They can set global values like class declaration. They do not produce output and are enclosed in <%@....%>
17. Explain page Directives.
Page Directives inform the JSP Engine about headers and facilities that the page receives from the environment. It is found at the top of all JSP pages. Its syntax is <%@ page attribute=”value”>
18. What is Include directive?
The include directive statically inserts the contents of a resource into the current JSP. It helps in the reuse of code without duplication. and includes contents of the file at translation time. Its syntax is as follows <%@ include file=”Filename”%>.
19. What are standard actions in JSP?
They affect the overall runtime behavior of a page and the response sent to the client. They are used to include a file at request time, to instantiate a JavaBean or find one. They are also used to generate a browser-specific code or forward a request to a new page.
20. What is Translation Phase?
JSP engine translates and compiles a JSP file to a servlet. This servlet moves to the execution phase where requests and responses are handled. They are compiled for the first time they are accessed unless manually compiled ahead of time. The manual or explicit compilation is useful for long and convoluted programs.
21. How to forward a request to another source in JSP?
<jsp:forward page="/New.jsp" />
22. How can the Automatic creation of session be prevented in a JSP page?
JSP page automatically create sessions for requests. By typing the following, it can be avoided.
<%@ page session=”false”  %>
23. How can you avoid scriptlet code in JSP?
JavaBeans or Custom Tags can be used instead of scriptlet code.
24. What are the Literals used in JSP?
The Literals used in JSP are as follows:
1) Null
2) Boolean
3) String
4) Integer
5) Float
25. Why are the request.getRequestDispatcher() and context.getRequestDispatcher() used?
The RequestDispatcher() and the context.getRequestDispatcher() are used for the following purposes.

request.getRequestDispatcher() is used to create request. We need to give the relative path of the resource.

context.getRequestDispatcher() is used to create context. We need to give the absolute path of the resource.


26. List down the major differences between the JSP Custom Tags and Java Beans.

Sr. No

Custom tags

Java Beans


Custom Tags can manipulate JSP content

Java Beans cannot manipulate JSP content


Executing complex operations is simple

Executing complex operations is difficult


Custom Tags are hard to set up

Java Beans are simple to set up


Custom Tags are available only in JSP 1.1

Java Beans are used in all JSP 1.x versions

27. Mention the three important tags used in the development of JSP Bean.
The Three tags used in the JSP Bean development are as follows:
1) jsp:useBean
2) jsp:setProperty
3) jsp:getProperty


28. Differentiate between JSP Scriptlet tag and Declaration tag.

Sr. No

Scriptlet Tag

Declaration Tag


JSP Scriptlet Tag can only declare Variables

Declaration Tag declares Methods and Variables


Scriptlet Tag is placed within jspService()

Declaration Tag is placed outside jspService()

29. What do you mean by isScriptingEnabled Attribute?
isScriptingEnabled attribute determines if scripting elements are allowed for use or not. The default value is true and it enables scriptlets, expressions, and declarations. If the attribute’s value is set to false, a translation-time error will be raised if the JSP uses any scriptlets, expressions/declarations.
30. What are the steps involved in reading data from a form using JSP?
The data parsing is a JSP is Automatic. It is done through the following steps depending on the situation.
1) getParameter(): request.getParameter() method is called to get the value of the form parameter.
2) getParameterValues(): This method is called if the parameter appears more than once and returns multiple values.
3) getParameterNames(): This method is called if the user wants a complete list of all parameters in the current request.
4) getInputStream(): This method is used to read binary data stream coming from the client.


31. How are cookies set in JSP?
Setting cookies with JSP involves the following steps:
1) Creating a Cookie object: Cookie constructor is called with a cookie name and a cookie value, both are strings.
2) Setting the maximum age: setMaxAge is used to specify the length of the cookie(in seconds) should be valid.
3) Sending the cookie into the HTTP response headers: response.addCookie is used to add cookies in the HTTP response header.
32. Explain the difference between forward and sendRedirect?
When a forward request is called, the request is sent to a different resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completely within the web container.
When a sendRedirtect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Since the browser issues a completely new request any objects that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward.
33. Why are JSP pages preferred for creating web-based client program?
JSP is preferred for creating web-based client programs. Because no plug-ins/security policy files are needed on the client systems whereas applet does. Also, JSP pages enable cleaner and more module application design because they provide a way to separate applications programming from web page design. This means personnel involved in web page design do not need to understand Java programming language syntax to do their jobs.
34. Explain some JSP Life-Cycle methods that can be overridden.
You can override the jspInit() and jspDestroy() methods within a JSP page. It is good programming practice to free any allocated resources within jspDestroy(). The jspInit() and jspDestroy() methods are each executed just once during the lifecycle of a JSP page and are typically declared as JSP declarations
35. How can I declare methods within my JSP page?
Ans: Methods can be declared for use within a JSP page. The methods are invoked within any other methods you declare, or within JSP scriptlets and expressions.
36. Can you disable JSP Scripting?
Yes, Scripting is disabled by setting the scripting-invalid element of the deployment descriptor to true. It is a sub-element of the JSP-property group. Its valid values are true and false.
The syntax for disabling scripting is as follows:


37. How can we handle the exceptions in JSP?
There are two ways to perform exception handling, one is by the errorPage element of the page directive, and the second is by the error page element of the web.xml file.
38. How is JSP used in the MVC model?
JSP is usually used for presentation in the MVC pattern (Model View Controller ), i.e., it plays the role of the view. The controller deals with calling the model and the business classes which in turn get the data, and this data is then presented to the JSP for rendering on to the client.
39. What is the difference between ServletContext and PageContext?
ServletContext gives the information about the container whereas PageContext gives the information about the Request.
40. What is EL in JSP?
The Expression Language(EL) is used in JSP to simplify the accessibility of objects. It provides many objects that can be used directly like param, requestScope, sessionScope, applicationScope, request, session, etc.


41. Can an interface be implemented in the JSP file?
42. What is JSTL?
JSP Standard Tag Library is a library of predefined tags that ease the development of JSP.
43. How to disable session in JSP?
<%@ page session="false" %>
44. What is the difference between hide comment and output comment?
The JSP comment is called hide comment whereas HTML comment is called output comment. If a user views the source of the page, the JSP comment will not be shown whereas HTML comment will be displayed.
45. What are context initialization parameters?
Context initialization parameters are specified by the <context-param> in the web.xml file, and these are initialization parameters for the whole application and not specific to any servlet or JSP.


46. What is the purpose of <jsp:setProperty >?
This action sets the properties of a bean.
47. What is the purpose of <jsp:getProperty >?
This action retrieves the properties of a bean.
48. How do you delete the Session Data?
Deleting the Session Data involves the following steps.
1) Remove a particular attribute: public void removeAttribute(String name) method is called to delete the value associated with the particular key.
2) Delete the whole session: public void invalidate() method is called to discard an entire session.
3) Setting the Session timeout: public void setMaxInactiveInterval(int interval) method is called to set the timeout for a session individually.
4) Log the user out: The logout is called to log the client out of the Web server and invalidate all sessions belonging to all the users.
5) web.xml Configuration: In Tomcat, using the above-mentioned methods, one can configure session time out in web.xml file as follows.


49. Explain the jspDestroy() method.
Whenever a JSP page is about to be destroyed, the container invokes the jspDestroy() method from the javax.servlet.jsp.JspPage interface. Servlets destroy methods are similar to it. It can be easily overridden to perform cleanup, like when closing a database connection.
50. How is JSP better than Servlet technology?
JSP is a technology on the server’s side to make content generation simple. They are document-centric, whereas servlets are programs. A Java server page can contain fragments of Java program, which execute and instantiate Java classes. However, they occur inside HTML template files. It provides the framework for the development of a Web Application.