JSP Interview Questions

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.

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.

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.

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.

IS JSP TECHNOLOGY EXTENSIBLE?

Yes. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.

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.

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.

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.

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.

IS JSP TECHNOLOGY EXTENSIBLE?

Yes, JSP is easily extensible by use and modification of tags, or custom actions, encapsulated in tag libraries.

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.

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.

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.

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.

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.

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.

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.

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.

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.

WHAT IS THE DIFFERENCE IN USING REQUEST.GETREQUESTDISPATCHER() AND CONTEXT.GETREQUESTDISPATCHER()?

request.getRequestDispatcher(path) is used in order to create it we need to give the relative path of the resource whereas context.getRequestDispatcher(path) in order to create it we need to give the absolute path of the resource.

HOW CAN THE OUTPUT OF 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.

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.

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.

WHAT IS BASIC DIFFERENCES BETWEEN THE JSP CUSTOM TAGS AND JAVA BEANS?

  • Custom tags can manipulate JSP content whereas beans cannot.
  • Complex operations can be reduced to a significantly simpler form with custom tags than with beans.
  • Custom tags require quite a bit more work to set up than do beans.
  • Custom tags are available only in JSP 1.1 and later, but beans can be used in all JSP 1.x versions.

CAN CONSTRUCTOR BE USED INSTEAD OF INIT(), TO INITIALIZE SERVLET?

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.