Friday 27 April 2018

JSP Interview Questions

1. 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.

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 database or other services. Some popular libraries are String tag library and Apache display tag library.

3. 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.

4. Explain Implicit objects in JSP.

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

5. 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.

6. Is JSP technology extensible?

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

7. Differentiate between response.sendRedirect(url) and <jsp:forward page = …>.

<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.

8. 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.

9. 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.

10. 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.

11. 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.

12. 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.

13. 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.

14. What are JSP Actions?

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.

<jsp:include/>
<jsp:getProperty/>
<jsp:forward/>
<jsp:setProperty/>
<jsp:usebean/>
<jsp:plugin/>

15. Differentiate between <jsp:include page=…> and <%@include file=…>.

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.

More about JSP:

No comments:

Post a Comment