JSP Interview Questions

What is a JSP ? What is it used for ? What do you understand by the term JSP translation phase or compilation phase? ?

JSP (Java ServerPages) is an extension of the Java Servlet technology. JSP is commonly used as the presentation layer for combining HTML and Java code. While Java Servlet technology is capable of generating HTML with use of PrintWriter class. This process of embedding HTML code with escape characters is cumbersome and hard to maintain. The JSP technology solves this by providing a level of abstraction so that the developer can use custom tags and action elements, which can speed up Web development and are easier to maintain.

The JSPs have a translation or a compilation process where the JSP engine translates and compiles a JSP file into a JSP Servlet. The translated and compiled JSP Servlet moves to the execution phase (run time) where they can handle requests and send response. Unless explicitly compiled ahead of time, JSP files are compiled the first time they are accessed. On large production sites, or in situations involving complicated JSP files, compilation may cause unacceptable delays to users first accessing the JSP page. The JSPs can be compiled ahead of time (ie precompiled) using application server tools/settings or by writing your own script.

Explain the life cycle methods of a JSP ?
  • Pre-translated: Before the JSP file has been translated and compiled into the Servlet.

  • Translated: The JSP file has been translated and compiled as a Servlet.

  • Initialized: Prior to handling the requests in the service method the container calls the jspInit() to initialize the Servlet. Called only once per Servlet instance.

  • Servicing: Services the client requests. Container calls this method for each request.

  • Out of service:The Servlet instance is out of service. The container calls the jspDestroy() method.

Learn more about JSP

What are different type of scripting elements?
  • Declaration Element: is the embedded Java declaration statement, which gets inserted at the Servlet class level.

Important: declaring variables via this element is not thread-safe, because this variable ends up in the generated Servlet as an instance variable, not within the body of the _jspservice() method. Ensure their access is either read-only or synchronized.

  • Expression Element: is the embedded Java expression, which gets evaluated by the service method.

  • Scriptlet Elements: are the embedded Java statements, which get executed as part of the service method. (Note: Not recommended to use Scriptlet elements because they don't provide reusability and maintainability. Use custom tags (like JSTL, JSF tags, etc) or beans instead).

  • Action Elements: A JSP element that provides information for execution phase. • Directive Elements: A JSP element that provides global information for the translation phase.

What are the different scope values or what are the different scope values for "jsp:usebean"?
Arrays are of fixed length. ArrayList is of variable length.
You cannot change the size of an array once it has been created. array once it has been created. Size of the ArrayList grows and shrinks as you add or remove the elements.
Arrays are of fixed length. ArrayList is of variable length.
You cannot change the size of an array once it has been created. array once it has been created. Size of the ArrayList grows and shrinks as you add or remove the elements.
You can use arrays to store both primitive types as well as reference types. You can store only reference types in an ArrayList.
Is JSP variable declaration thread safe?

No. The declaration of variables in JSP is not thread-safe, because the declared variables end up in the generated Servlet as an instance variable, not within the body of the _jspservice() method. The following declaration is not thread safe: because these are declarations, and will only be evaluated once when the page is loaded

The following declaration is thread safe: because the variables declared inside the scriplets have the local scope and not shared.

Explain JSP URL mapping? What is URL hiding or protecting the JSP page?

JSP resources usually reside directly or under subdirectories (e.g. myPath) of the document root, which are directly accessible to the user through the URL. If you want to protect your Web resources then hiding the JSP files behind the WEB-INF directory can protect the JSP files, css (cascading style sheets) files, Java Script files, pdf files, image files, html files etc from direct access. The request should be made to a servlet who is responsible for authenticating and authorising the user before returning the protected JSP page or its resources.

What are custom tags? Explain how to build custom tags?

Custom JSP tag is a tag you define. You define how a tag, its attributes and its body are interpreted, and then group your tags into collections called tag libraries that can be used in any number of JSP files. So basically it is a reusable and extensible JSP only solution. The pre-built tags also can speed up Web development.

Step 1 Create a Custom tag class using only doStartTag()

Step 2 The Tag library descriptor file (*.tld) maps the XML element names to the tag implementations. The code sample MyTagDesc.tld is shown below:

Step 3 The web.xml deployment descriptor maps the URI to the location of the *.tld (Tag Library Descriptor) file. The code sample web.xml file is shown below:

STEP: 4 The JSP file declares and then uses the tag library as shown below:

What is a Expression?
  • An expression tag contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, you can use an expression within text in a JSP file. Like

You cannot use a semicolon to end an expression.

Learn about Java in detail

Difference between forward and sendRedirect?

When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completly with in 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. Because the browser issues a completly new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward

What are implicit objects? List them?

Certain objects that are available for the use in JSP documents without being declared first. These objects are parsed by the JSP engine and inserted into the generated servlet. The implicit objects are listed below:

request response pageContext session application out config page exception

How do I prevent the output of my JSP or Servlet pages from being cached by the browser?

You will need to set the appropriate HTTP header attributes to prevent the dynamic content output by the JSP page from being cached by the browser. Just execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the browser. You need both the statements to take care of some of the older browser versions.

How to implement a thread-safe JSP page? What are the advantages and Disadvantages of using it?

JSPs can be thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive

within your JSP page. With this, instead of a single instance of the servlet generated for your JSP page loaded in memory, you will have N instances of the servlet loaded and initialized, with the service method of each instance effectively synchronized. You can typically control the number of instances (N) that are instantiated for all servlets implementing SingleThreadModel through the admin screen for your JSP engine. More importantly, avoid using the tag for variables. If you do use this tag, then you should set isThreadSafe to true, as mentioned above. Otherwise, all requests to that page will access those variables, causing a nasty race condition. SingleThreadModel is not recommended for normal use. There are many pitfalls, including the example above of not being able to use <%! %>. You should try really hard to make them thread-safe the old fashioned way: by making them thread-safe .

Why to use the HttpServlet Init method to perform expensive operations that need only be done once?

Because the servlet init() method is invoked when servlet instance is loaded, it is the perfect location to carry out expensive operations that need only be performed during initialization. By definition, the init() method is thread-safe. The results of operations in the HttpServlet.init() method can be cached safely in servlet instance variables, which become read-only in the servlet service method.

Why is it not a good practice to create HttpSessions in JSPs by default?

By default, JSP files create HttpSessions. This is in compliance with J2EETM to facilitate the use of JSP implicit objects, which can be referenced in JSP source and tags without explicit declaration. HttpSession is one of those objects. If you do not use HttpSession in your JSP files then you can save some performance overhead with the following JSP page directive:

What are the standard actions available in JSP?

The standard actions available in JSP are as follows:

  • < jsp:include >: It includes a response from a servlet or a JSP page into the current page. It differs from an include directive in that it includes a resource at request processing time, whereas the include directive includes a resource at translation time.

  • < jsp:forward >: It forwards a response from a servlet or a JSP page to another page.

  • < jsp:useBean >: It makes a JavaBean available to a page and instantiates the bean.

  • < jsp:setProperty >:It sets the properties for a JavaBean.

  • < jsp:getProperty >:It gets the value of a property from a JavaBean component and adds it to the response.

  • < jsp:param >: It is used in conjunction with ;, ; to add a parameter to a request. These parameters are provided using the name-value pairs.

  • < jsp:plugin >: It is used to include a Java applet or a JavaBean in the current JSP page.