JSP Interview Questions
by Venkatesan M, on Nov 3, 2017 6:04:22 PM
Q1. What is JSP?
Ans: 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.
Q2. Explain JSP and tell its uses.
Ans: 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.
Q3. What is difference between hide comment and output comment?
Ans: 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.
Q4. What is the requirement of a tag library?
Ans: 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.
Q5. Is JSP technology extensible?
Ans: Yes. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.
Q6. Explain JSP Technology.
Ans: 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.
Q7. How can I implement a thread-safe JSP page? What are the advantages and Disadvantages of using it?
Ans: 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.
Q8. How can multiple submits due to refresh button clicks be prevented?
Ans: 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.
Q9. How can we handle the exceptions in JSP ?
Ans: 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.
Q10. Is JSP technology extensible?
Ans: Yes, JSP is easily extensible by use and modification of tags, or custom actions, encapsulated in tag libraries.
Q11. Differentiate between response.sendRedirect(url) and <jsp:forward page = …> .
Ans: <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.
Q12. Can we use the exception implicit object in any jsp page ?
Ans: No. The exception implicit object can only be used in the error page which defines it with the isErrorPage attribute of page directive.
Q13. Can a subsequent request be accessed with one’s servlet code, if a request attribute is already sent in his JSP?
Ans: 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.
Q14. How is JSP used in the MVC model?
Ans: 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.
Q15. How to include static files in a JSP page?
Ans: 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.
Q16. What are context initialization parameters?
Ans: 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.
Q17. Why is it that JComponent have add() and remove() methods but Component doesn’t?
Ans: JComponent is a subclass of Container. It contains other Components and JComponents.
Q18. What is the difference between ServletContext and PageContext?-
Ans: ServletContext gives the information about the container whereas PageContext gives the information about the Request.
Q19. How can a thread safe JSP page be implemented?
Ans: It can be done by having them implemented by the SingleThreadModel Interface. Add <%@page isThreadSafe=”false” %> directive in the JSP page.
Q20. What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?
Ans: 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.
Q21. How can the output of JSP or servlet page be prevented from being cached by the browser?
Ans: Using appropriate HTTP header attributes to prevent the dynamic content output by a JSP page from being cached by the browser.
Q22. What is EL in JSP?
Ans: 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.
Q23. How to restrict page errors display in a JSP page?
Ans: 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.
Q24. What is basic differences between the JSP custom tags and java beans?
Ans:
- 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.
Q25. Can constructor be used instead of init(), to initialize servlet?
Ans: 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.