JSP Interview Questions
by Bharathkumar, on Sep 10, 2022 12:36:51 PM
1. WHAT IS JSP?
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.
2. EXPLAIN JSP AND TELL ITS USES.
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.
3. WHAT IS DIFFERENCE BETWEEN HIDE COMMENT AND OUTPUT COMMENT?
The jsp comment is called hide comment whereas html comment is called output comment. If user views the source of the page, the jsp comment will not be shown whereas html comment will be shown.
4. 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 database or other services. Some popular libraries are String tag library and Apache display tag library.
5. IS JSP TECHNOLOGY EXTENSIBLE?
Yes. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.
6. EXPLAIN JSP TECHNOLOGY.
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.
7. HOW CAN I IMPLEMENT A THREAD-SAFE JSP PAGE? WHAT ARE THE ADVANTAGES AND DISADVANTAGES OF USING IT?
You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive <%@ page isThreadSafe=”false” %> within your JSP page.
8. HOW CAN MULTIPLE SUBMITS DUE TO REFRESH BUTTON CLICKS BE PREVENTED?
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.
9. HOW CAN WE HANDLE THE EXCEPTIONS IN JSP ?
There are two ways to perform exception handling, one is by the errorPage element of page directive, and second is by the error-page element of web.xml file.
10. IS JSP TECHNOLOGY EXTENSIBLE?
Yes, JSP is easily extensible by use and modification of tags, or custom actions, encapsulated in tag libraries.
11. DIFFERENTIATE BETWEEN RESPONSE.SENDREDIRECT(URL) AND .
- <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.
12. CAN WE USE THE EXCEPTION IMPLICIT OBJECT IN ANY JSP PAGE ?
No. The exception implicit object can only be used in the error page which defines it with the isErrorPage attribute of page directive.
13. 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 in translated to a Java servlet, and then executed. Only HTML code is given as output.
14. 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, this data is then presented to the JSP for rendering on to the client.
15. 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 file attribute. Although static resources may be included, it is not preferred as each request requires inclusion.
16. WHAT ARE CONTEXT INITIALIZATION PARAMETERS?
Context initialization parameters are specified by the <context-param> in the web.xml file, these are initialization parameter for the whole application and not specific to any servlet or JSP.
17. 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.
18. WHY IS IT THAT JCOMPONENT HAVE ADD() AND REMOVE() METHODS BUT COMPONENT DOESN’T?
ServletContext gives the information about the container whereas PageContext gives the information about the Request.
19. HOW CAN A THREAD SAFE JSP PAGE BE IMPLEMENTED?
It can be done by having them implemented by the SingleThreadModel Interface. Add <%@page