Pr.Pg Next Pg

JSP Interview Questions and Answers Part 3

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


22.How do you restrict page errors display in the JSP page?

  • You first set "Errorpage" attribute of PAGE directory to the name of the error page (ie Errorpage="error.jsp")in your jsp page .

  • Then in the error jsp page set "isErrorpage=TRUE". When an error occur in your jsp page it will automatically call the error page.


23.What JSP lifecycle methods can I override?

  • You cannot override the _jspService() method within a JSP page.

  • You can however, override the jspInit() and jspDestroy() methods within a JSP page. jspInit() can be useful for allocating resources like database connections, network connections, and so forth for the JSP page.

  • It is good programming practice to free any allocated resources within jspDestroy().
    The jspInit() and jspDestroy() methods are each executed just once during the lifecycle of a JSP page and are typically declared as JSP declarations:


24.How do I perform browser redirection from a JSP page?

  • You can use the response implicit object to redirect the browser to a different resource, as:

  • You can also physically alter the Location HTTP header attribute, as shown below:

  • You can also use the:

  • Also note that you can only use this before any output has been sent to the client. I beleve this is the case with the response.sendRedirect() method as well.

  • If you want to pass any paramateres then you can pass using >


25.How does JSP handle run-time exceptions?

  • You can use the errorPage attribute of the page directive to have uncaught runtime exceptions automatically forwarded to an error processing page.

  • For example:
    redirects the browser to the JSP page error.jsp if an uncaught exception is encountered during request processing. Within error.jsp, if you indicate that it is an error-processing page, via the directive:
    the Throwable object describing the exception may be accessed within the error page via the exception implicit object.

  • Note: You must always use a relative URL as the value for the errorPage attribute.

26.How do I use comments within a JSP page?

  • You can use "JSP-style" comments to selectively block out code while debugging or simply to comment your scriptlets. JSP comments are not visible at the client.

  • For example:
    You can also use HTML-style comments anywhere within your JSP page. These comments are visible at the client.

  • For example:
    Of course, you can also use comments supported by your JSP scripting language within your scriptlets.


27.Is it possible to share an HttpSession between a JSP and EJB? What happens when I change a value in the HttpSession from inside an EJB?

  • You can pass the HttpSession as parameter to an EJB method, only if all objects in session are serializable.

  • This has to be consider as "passed-by-value", that means that it's read-only in the EJB.
    If anything is altered from inside the EJB, it won't be reflected back to the HttpSession of the Servlet Container.

  • The "pass-byreference" can be used between EJBs Remote Interfaces, as they are remote references.

  • While it IS possible to pass an HttpSession as a parameter to an EJB object, it is considered to be "bad practice" in terms of object oriented design.

  • This is because you are creating an unnecessary coupling between back-end objects (ejbs) and front-end objects (HttpSession). Create a higher-level of abstraction for your ejb's api.

  • Rather than passing the whole, fat, HttpSession (which carries with it a bunch of http semantics), create a class that acts as a value object (or structure) that holds all the data you need to pass back and forth between front-end/back-end.

  • Consider the case where your ejb needs to support a non-http-based client. This higher level of abstraction will be flexible enough to support it.


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

29.How can I declare methods within my JSP page?

  • You can declare methods for use within your JSP page as declarations.

  • The methods can then be invoked within any other methods you declare, or within JSP scriptlets and expressions.

  • Do note that you do not have direct access to any of the JSP implicit objects like request, response, session and so forth from within JSP methods.

  • However, you should be able to pass any of the implicit JSP variables as parameters to the methods you declare.

For example:
<%test(out);% >


30.Can I stop JSP execution while in the midst of processing a request?

  • Yes.

  • Preemptive termination of request processing on an error condition is a good way to maximize the throughput of a high-volume JSP engine.

  • The trick (assuming Java is your scripting language) is to use the return statement when you want to terminate further processing.


Pr.Pg border                                              Next Pg