JSP Interview Questions

What is the difference between RequestDispatcher and sendRedirect?

RequestDispatcher: server-side redirect with request and response objects.

sendRedirect : Client-side redirect with new request and response objects.

Explain the mechanism in which JSP can handle runtime exceptions?

JSP handles runtime exceptions using  errorPage attribute of page directive and also we need to specify isErrorPage=true if the current page is intended to URL redirecting of a JSP.

How can you  delete a Cookie in a JSP?

Cookie cook = new Cookie(“name”,”value”);
response.addCookie(cook);
Cookie killcook = new Cookie(“cook”,”value”);
killcook.setMaxAge(0);
killcook.setPath(“/”);
killcook.addCookie(killcook);

 

How can we play around with the JSP and SSI #include?

If you are planning to include only bare HTML, it’s better to use the #include directive as usual inside the ‘.jsp’ file.

e.g.:

<!–#include file=”data.inc”–>

But it will be different if you are planning to have the server to check the JSP code that is inside the included file.If the data.inc file has the jsp code you may have to have

<%@ vinclude=”data.inc” %>

The <!#include file=”data.inc”> is used for including non-JSP files.

What do you mean by hidden comment in JSP?

A hidden is one that documents the JSP page but that is not dispatched to the client. The JSP engine normally ignores the hidden comment, and does it does not process any code within hidden comment tags. This useful if you want to document what the JSP does or if you are experimenting with the code.