About Jsp Interview Preparation
Based on Java Servlet technology, JSP (JavaServer Pages) allows the platform-independent development of dynamic web applications
For better preparation, read each question carefully, understand the concept, prepare one real example,
and connect your answer with your project or work experience.
Jsp Questions List
Quick list of important Jsp interview questions. Detailed answer previews are available below.
- Explain JSP and tell its uses?
- What is the requirement of a tag library?
- Explain JSP Technology.
- Explain Implicit objects in JSP.
- How can multiple submits due to refresh button clicks be prevented?
- Is JSP technology extensible?
- Differentiate between response.sendRedirect(url) and .
- Can a subsequent request be accessed with one's servlet code, if a request attribute is already sent in his JSP?
- How to include static files in a JSP page?
- Why is it that JComponent have add() and remove() methods but Component doesn't?
- How can a thread safe JSP page be implemented?
- How can the output of JSP or servlet page be prevented from being cached by the browser?
- How to restrict page errors display in a JSP page?
- What are JSP Actions?
- Differentiate between and .?
- Can constructor be used instead of init(), to initialize servlet?
- Explain lifecycle methods.
- Explain JSP Output comments?
- Define Expression
- Define Composition.
- Define JSP Scriptlet.
- How can information from one JSP be passed to another JSP?
- Explain handling of runtime exceptions.
- Why does _jspService() start with an '_' but other lifecycle methods do not?
- Explain the various scope values for tag.
- Show the 2 types of comments in JSP.
- Can Static method be Override?
- Explain JSP directives.
- Explain pge Directives.
- Show attributes of page directives.
Question 1: Explain JSP and tell its uses?
Answer: JSP stands for Java Server Pages. It is a presentation layer technology independent of 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 JSP compiler in the background and generate a Servlet from the page.
View detailed answer →
Question 2: What is the requirement of a tag library?
Answer: 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 database or other services. Some popular libraries are String tag library and Apache display tag library.
View detailed answer →
Question 3: Explain JSP Technology.
Answer: JSP is a standard extension of Java and is defined on top of Servlet extensions. Its goal is to simplify management and creation of dynamic web pages. It is platform-independent, secure, and it makes use of Java as a server side scripting language.
View detailed answer →
Question 4: Explain Implicit objects in JSP.
Answer: Objects created by 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
View detailed answer →
Question 5: How can multiple submits due to refresh button clicks be prevented?
Answer: Using a Post/Redirect/Get or a PRG pattern, this problem can be solved. 1. A form filled by the user is submitted to the server using POST or GET method. The state in the database and business model are updated. 2. A redirect response is used to reply by the servlet for a view page. 3. A view is loaded by the browser using the GET command and no user data is sent. This is safe from multiple submits as it is a separate JSP page.
View detailed answer →
Question 6: Is JSP technology extensible?
Answer: Yes, JSP is easily extensible by use and modification of tags, or custom actions, encapsulated in tag libraries.
View detailed answer →
Question 7: Differentiate between response.sendRedirect(url) and .
Answer: <jsp.forward> element forwards the request object from 1 JSP file to another. Target file can be HTML, servlet or another JSP file, but it should be in the same application context as forwarding JSP file. sendRedirect send HTTP temporary redirect response to the browser. The browser then creates a new request for the redirected page. It kills the session variables.
View detailed answer →
Question 8: Can a subsequent request be accessed with one's servlet code, if a request attribute is already sent in his JSP?
Answer: 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 in translated to a Java servlet, and then executed. Only HTML code is given as output.
View detailed answer →
Question 9: How to include static files in a JSP page?
Answer: 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 file attribute. Although static resources may be included, it is not preferred as each request requires inclusion.
View detailed answer →
Question 10: Why is it that JComponent have add() and remove() methods but Component doesn't?
Answer: JComponent is a subclass of Container. It contains other Components and JComponents.
View detailed answer →
Question 11: How can a thread safe JSP page be implemented?
Answer: It can be done by having them implemented by the SingleThreadModel Interface. Add <%@page isThreadSafe=”false” %> directive in the JSP page.
View detailed answer →
Question 12: How can the output of JSP or servlet page be prevented from being cached by the browser?
Answer: Using appropriate HTTP header attributes to prevent the dynamic content output by a JSP page from being cached by the browser.
View detailed answer →
Question 13: How to restrict page errors display in a JSP page?
Answer: 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.
View detailed answer →
Question 14: What are JSP Actions?
Answer: They are XML tags, which direct the server to using existing components or control behavior of JSP Engine. They consist of a typical prefix of “jsp:” and action name.
View detailed answer →
Question 15: Differentiate between and .?
Answer: Both these tags include information from 1 page to another. The first tag acts as a function call between two Jsp’s. It is executed each time client page is accessed by the client. It is useful to modularize the web application. New content is included in the output. The second tag content of file is textually embedded having similar directive. The changed content is not included in the output. It is helpful when code from one jsp is required by several jsp’s.
View detailed answer →
Question 16: Can constructor be used instead of init(), to initialize servlet?
Answer: Yes, it is possible. But it is not preferred because init() was developed because earlier Java versions could not invoke constructors with arguments dynamically. So they could not assign a servletConfig. Today, however, servlet containers still call only no-arg constructor. So there is no access to servletContext or servletConfig.
View detailed answer →
Question 17: Explain lifecycle methods.
Answer: 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) _jspService(): The container calls this for each request and passes it on to the objects. 3) jspDestroy(): It is called by the container just before destruction of the instance.
View detailed answer →
Question 18: Explain JSP Output comments?
Question 19: Define Expression
Answer: Expression tag is used to insert Java values directly in the output. Its syntax is <%=expression%> It contains a scripting language expression that is evaluated, then converted to a string, and then inserted where the expression comes in JSP file.
View detailed answer →
Question 20: Define Composition.
Question 21: Define JSP Scriptlet.
Answer: It a JSP tag that encloses Java code in JSP pages. Their syntax is <% %>. Code written in scriptlet executes every time the program is run.
View detailed answer →
Question 22: How can information from one JSP be passed to another JSP?
Answer: The tag <Jsp:param> allows us to pass information between multiple Jsp’s.
View detailed answer →
Question 23: Explain handling of runtime exceptions.
Answer: Errorpage attribute is used to uncatch the run-time exceptions forwarded automatically to an error processing page. It redirects the browser to JSP page error.jsp if any uncaught exception is faces during request handling. It is an error processing page.
View detailed answer →
Question 24: Why does _jspService() start with an '_' but other lifecycle methods do not?
Answer: Whatever content made in a jsp page goes inside the _jspService() method by the container. If it is override, the compiler gives an error, but the other 2 lifecycles can be easily override. So ‘_’ shows that we cannot override this method.
View detailed answer →
Question 25: Explain the various scope values for tag.
Answer: <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
View detailed answer →
Question 26: Show the 2 types of comments in JSP.
Answer: The 2 types are: <%–JSP Comment–%><!–HTML comment–>
View detailed answer →
Question 27: Can Static method be Override?
Answer: We can declare static methods with same signature in subclass, but it is not considered overriding as there won’t be any run-time polymorphism. Hence the answer is ‘No’.
View detailed answer →
Question 28: Explain JSP directives.
Answer: 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 <%@….%>
View detailed answer →
Question 29: Explain pge Directives.
Answer: 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”>
View detailed answer →
Question 30: Show attributes of page directives.
Answer: 1)Session : It shows if a session data is available to the page. 2)Import : it shows packages that are imported. 3)isELIgnored : It shows whether EL expressions are ignored when JSP translates into a servlet. 4)contentType : it allows the user to specify the content type of page.
View detailed answer →
Question 31: What is Include directive?
Answer: 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”%>.
View detailed answer →
Question 32: Explain the jsp:setProperty action.
Answer: It is used to give values to properties of beans that have been referenced beforehand. <jsp:useBean id=”ABC”…/> … <jsp:setProperty name=”ABC” property=”myProperty”… jsp:setproperty is executed even if a new bean is instantiated or existing bean is found. By adding </jsp.useBean> at the end of the code, the condition for execution is inverted i.e. It is not executed if existing object was found and only if a new object was instantiated.
View detailed answer →
Question 33: Define Static Block.
Answer: It is used to start the static data member. It is executed before classloading.
View detailed answer →
Question 34: Explain jsp:plugin action.
Answer: This action helps in insertion of a specific object in the browser or embed the element needed to specify the running of applet using Java plugin.
View detailed answer →
Question 35: Explain client and server side validation.
Answer: Javascript is used for the client-side validation. It takes place within the browser. Javascript is used to submit the form data if validation is successful. Validation errors require no extra network trip because form cannot be submitted. Validation is also carried out in the server after submission. If validation fails, extra network trip is required to resend the form to the client.
View detailed answer →
Question 36: What is Translation Phase?
Answer: 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.
View detailed answer →
Question 37: Perform a Browser Redirection from a JSP Page.
Question 38: Give uses of Object Cloning.?
Answer: Object cloning is used to create an exact copy of an object by typing the same code or using various other techniques.
View detailed answer →
Question 39: How to forward a request to another source.
Question 40: How can Automatic creation of session be prevented in a JSP page?
Answer: JSP page automatically create sessions for requests. By typing the following, it can be avoided. <%@pagesession=”false” %>
View detailed answer →
Question 41: How can you avoid scriptlet code in JSP?
Question 42: Explain the jspDestroy() method.
Answer: 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.
View detailed answer →
Question 43: Explain the action.
Answer: It is an action used with include or forward standard actions. It helps in passing the parameter names and values to a resource
View detailed answer →
Question 44: Explain static method.?
Answer: A static method is of the class and not the object of a class. It can be invoked without instance of a class. Static members can also access the static data and change its value.
View detailed answer →
Question 45: How to disable scripting?
Answer: Scripting can be easily disabled by setting scripting-invalid element of the deployment descriptor to true. It is a sub-element of property group. Its can be false as well.
View detailed answer →
Question 46: Define JSP Declaration.?
Answer: JSP Declaration are tags used in declaring variables. They are enclosed in <%!%> tag. They are used in declaring functions and variables. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <%@pagecontentType=”text/html”%> <html> <body> <%! inta=0; privateintgetCount(){ a++; returna; } %> <p>Values of a are:</p> <p><%=getCount()%></p> </body> </html>
View detailed answer →
Question 47: How can HTML Output be prevented from being cached?
Answer: <% response.setHeader(“Cache-Control”,“no=store”); response.setDateHeader(“Expires”,0); %>
View detailed answer →
Question 48: How is JSP better than Servlet technology?
Answer: 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 file. It provides the framework for development of a Web Application
View detailed answer →