JSp Interview Questions with Answers
Question: What is difference between custom JSP tags and beans?
Answer: Custom JSP tag is a tag you defined. 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. To use custom JSP tags, you need to define three separate components: the tag handler class that defines the tag's behavior ,the tag library descriptor file that maps the XML element names to the tag implementations and the JSP file that uses the tag library
JavaBeans are Java utility classes you defined. Beans have a standard format for Java classes. You use tags
Custom tags and beans accomplish the same goals -- encapsulating complex behavior into simple and accessible forms. There are several differences:
Custom tags can manipulate JSP content; 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 usually define relatively self-contained behavior, whereas beans are often defined in one servlet and used in a different servlet or JSP page. Custom tags are available only in JSP 1.1 and later, but beans can be used in all JSP 1.x versions.
Question: How many JSP scripting elements are there and what are they?
Answer: There are three scripting language elements: declarations, scriptlets, expressions.
Question: How do I include static files within a JSP page?
Answer: Static resources should always be included using the JSP include directive. This way, the inclusion is performed just once during the translation phase.
Question: How can I implement a thread-safe JSP page?
Answer: You can make your JSPs thread-safe adding the directive
within your JSP page.<%@ page isThreadSafe="false" % >
Question: What are the lifecycle of JSP?
Answer: When presented with JSP page the JSP engine does the following 7 phases.
Page translation: -page is parsed, and a java file which is a servlet is created.
Page compilation: page is compiled into a class file
Page loading : This class file is loaded.
Create an instance :- Instance of servlet is created
jspInit() method is called
_jspService is called to handle service calls
_jspDestroy is called to destroy it when the servlet is not required.
Question: What are the different scopes available ?
Answer: page, request, session, application
Question: Is JSP extensible ?
Answer: Yes, it is. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.
Question: What is the difference between <%@ include ...> (directive include) and jsp:include ?
Answer: <%@ include is static include. It is inline inclusion. The contents of the file will get included on Translation phase. It is something like inline inclusion. We cannot have a dynamic filename for directive include.
Question: Can I just abort processing a JSP?
Answer: Yes. You can put a return statement to abort JSP processing.
Question: How do I prevent the output of my JSP or Servlet pages from being cached by the browser?
Answer: By setting appropriate HTTP header attributes we can prevent caching by the browser
response.setHeader("Cache-Control","no-store"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
Question: How to refer the "this" variable within a JSP page?
Answer: Under JSP 1.0, the page implicit object page is equivalent to "this", and returns a reference to the servlet generated by the JSP page.
Question: How many JSP scripting elements and what are they?
Answer: There are three scripting elements in JSP . They are declarations, scriptlets, expressions.
Question: Can we implement an interface in JSP ?
Question: What is JSP?
Answer: JSP is a server side scripting technology. JSP allows Java as well as a few special tags to be embedded into a web file (HTML/XML, etc). The suffix must ends with .jsp.
Question: What are JSP Actions?
Answer: JSP actions use constructs in XML syntax to control the behavior of the servlet engine. You can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin. Available actions include: jsp:include, jsp:useBean, jsp:setProperty, jsp:getProperty, jsp:forward and Jsp: plugin
Question: How do you pass data (including JavaBeans) to a JSP from a servlet?
Answer: By forwarding the request to the servlet ( the data must be there in the request scope) we can pass the data from a JSP to servlet. Also we can use a session to pass the data.
Question: What JSP lifecycle methods we can override?
Answer: You cannot override the _jspService() method within a JSP page. You can however, override the jspInit() and jspDestroy() methods within a JSP page.
Question: How will you include a static file in a JSP page?
Answer: You can include a static resource to a JSP using
Question: How you can perform browser redirection?
Answer: We can use the method sendRedirect of HttpServletResponse or forward method of RequestDispatcher.
Question: Can we use ServletOutputStream object from a JSP page?
Answer: No. You are supposed to use JSPWriter object (given to you in the form of the implicit object out) only for replying to clients.
Question: How can you stop JSP execution in the middle of processing a request?
Answer: We can use the return statement to stop the processing of JSP. Because JSP is compiled to servlet and all the statements will go inside service method, any time you can stop the processing using return statement.
Question: How can I invoke a JSP error page from a servlet?
Answer: You can invoke the JSP error page and pass the exception object to it from within a servlet. For that you need to create a request dispatcher for the JSP error page, and pass the exception object as a javax.servlet.jsp.jspException request attribute.
Question: How will you pass information from JSP to included JSP?
Answer: By using jsp:param tag.
Question: How does JSP handle runtime exceptions?
Answer: Using errorPage attribute of page directive JSP handles runtime exceptions. We need to specify isErrorPage=true if the current page is intended to use as a JSP error page.
Question: How can I enable session tracking for JSP pages if the browser has disabled cookies?
Question: How can you declare methods in your JSP page?
Answer: You can declare methods as declarations in your JSP page. The methods can then be invoked within any other methods you declare, or within JSP scriptlets and expressions.
Question: How do you pass an init parameter to a JSP?
Answer: You need to configure the DD for passing init parameter to a JSP. You can configure the DD as follows.
Question: What is a Java Bean?
Answer: A Java Bean is a software component that has been designed to be reusable in a variety of different environments.
Question: How can I print the stack trace of an exception from a JSP page?
Answer: By creating an object of PrintWriter we can print the stack trace in JSP page. Eg:
PrintWriter pw = response.getWriter();
Question: What is the differecnce between JspWriter and PrintWriter?
Answer: JspWriter is buffered.
Question: What is the difference between an application server and a web server?
Answer: Everything in web server must be achieved programmatically. But application server provides a lot of services like security, transaction , scalability. Application server will be having EJB support where web server don't have an EJB support.
Servlet Interview Questions & Answers
Servlet Interview Questions & Answers Part 2
Servlet Interview Questions & Answers Part 3