JSP Java Interview Questions – Set 06

What is JSP directive

A JSP element that gives an instruction to the JSP container and is interpreted at translation time.

How will you handle the runtime exception in your jsp page

The errorPage attribute of the page directive can be used to catch run-time exceptions automatically and then forwarded to an error processing page. You can define the error page to which you want the request forwarded to, in case of an exception, in each JSP Page. Also, there should be another JSP that plays the role of the error page which has the flag isErrorPage set to True.

What do you understand by JSP Actions

JSP actions are XML tags that direct the server to use existing components or control the behavior of the JSP engine. JSP Actions consist of a typical (XML-based) prefix of “jsp” followed by a colon, followed by the action name followed by one or more attribute parameters.

There are six JSP Actions:

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

Difference between JSP include directive and JSP include action

  • <%@ include file=”filename” %> is the JSP include directive.At JSP page translation time, the content of the file given in the include directive is ‘pasted’ as it is, in the place where the JSP include directive is used. Then the source JSP page is converted into a java servlet class. The included file can be a static resource or a JSP page. Generally JSP include directive is used to include header banners and footers.The JSP compilation procedure is that, the source JSP page gets compiled only if that page has changed. If there is a change in the included JSP file, the source JSP file will not be compiled and therefore the modification will not get reflected in the output.
  • <jsp:include page=”relativeURL” /> is the JSP include action element.The jsp:include action element is like a function call. At runtime, the included file will be ‘executed’ and the result content will be included with the soure JSP page. When the included JSP page is called, both the request and response objects are passed as parameters.If there is a need to pass additional parameters, then jsp:param element can be used. If the resource is static, its content is inserted into the calling JSP file, since there is no processing needed.

Why does JComponent have add() and remove() methods but Component does not

because JComponent is a subclass of Container, and can contain other components and jcomponents. How can I implement a thread-safe JSP page? – 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

What is JSP Output Comments

JSP Output Comments are the comments that can be viewed in the HTML source file. They are comments that are enclosed within the < ! – – Your Comments Here – – >

What is the role of JSP in MVC Model

JSP is mostly used to develop the user interface, It plays are role of View in the MVC Model.

How to improve Servlet Performance

  • Cache static data using jspInit() method.
  • Release static data in jspDestroy() method.
  • To concatenate string use, StringBuffer.
  • Do not use println() method.
  • Do not use PrintWriter to send binary data. Use ServletOutputStream.
  • Always flush data in sections.
  • Use getLastModified() method to handle browser and server cache.
  • Use application server cache.
  • Use session in following order: HttpSession, Hidden fields, Cookies, URL rewriting.
  • Always remove HttpSession explicitly.
  • Disable Servlet auto reloading.

Can we override the jspInit(), _jspService() and jspDestroy() methods

We can override jspinit() and jspDestroy() methods but not _jspService().

Is there a way I can set the inactivity lease period on a per-session basis

Typically, a default inactivity lease period for all sessions is set within your JSPengine admin screen or associated properties file. However, if your JSP engine supports the Servlet 2.1 API, you can manage the inactivity lease period on a per-session basis.This is done by invoking the HttpSession.setMaxInactiveInterval() method, right after the session has been created.