JSP Interview Questions

October 2nd, 2007 Admin Posted in JSP Interview Questions No Comments »

JSP Action:

* JSP actions are XML tags that direct the server to use existing components or control the behavior of the JSP engine.
* Consist of typical (XML-base) prefix of ‘jsp’ followed by a colon, followed by the action name followed by one or more attribute parameters. For example: There are six JSP Actions: , , , , ,

What is the difference between and
<%@ include file = … >?

Both the tag includes the information from one page in another. The differences are as follows: : This is like a function call from one jsp to another jsp. It is executed ( the included page is executed and the generated html content is included in the content of calling jsp) each time the client page is accessed by the client. This approach is useful to for modularizing the web application. If the included file changed then the new content will be included in the output.

<%@ include file = … >: In this case the content of the included file is textually embedded in the page that have <%@ include file=”..”> directive. In this case in the included file changes, the changed content will not included in the output. This approach is used when the code from one jsp file required to include in multiple jsp files.

What is the difference between and
response.sendRedirect(url) ?

The element forwards the request object containing the client request information from one JSP file to another file. The target file can be an HTML file, another JSP file, or a servlet, as long as it is in the same application context as the forwarding JSP file.
sendRedirect sends HTTP temporary redirect response to the browser, and browser creates a new request to go the redirected page. The response.sendRedirect kills the session variables.

Implicit objects available to JSP:

http://www.roseindia.net/interviewquestions/jsp-interview-questions.shtml

Life-cycle methods of JSP:

1. jspInit(): Container calls jspInit() to initialize the servlet instance. It is called bfore any other method, and is called only once for a servlet instance.
2. _jspService(): The container calls the _jspservice() for each request and it passes the request and the response objects. This method cannot be overriden.
3. jspDestroy(): container calls this when its instance is about to be destroyed.

How will you handle runtime exception in your jsp page?

‘errorPage’ attribute of the page directive can be used to catch runtime exceptions automatically and then forwarded to an error processing page. For example: <%@ page errorPae=”dbaccessError.jsp” %> forwards request to dbaccessError.jsp pge if an uncaught exception is encountered during request processing. Within “dbaccessError.jsp”, you must indicate that it is an error processing page, via the directive: <%@ page isErrorPage=”true” %>.

How can you enable session tracking for JSP pages if the browser has disabled cookies: We can enable session tracking using URL rewriting. URL rewriting includes the sessionID within the link itself as a name/value pair. However, for this to be effective, you need to append the session Id for each and every link that is part of your servlet response. adding sessionId to a link is greatly simplified by means of a couple of methods: response.ecnodeURL() associates a session ID with a giver UIRl, and if you are using redirection, response.encodeRedirectURL() can be used by giving the redirected URL as input. Both encodeURL() and encodeRedirectURL() first determine whether cookies are supported by the browser; is so, the input URL is returned unchanged since the session ID wil lbe persisted as cookie.

Which is better fro threadsafe servlets and JSPs? SingleThreadModel Interface or Synchronization? Although the SingleThreadModel technique is easy to use, and works well for low volume sites, it does not scale well. JSps can be made thread safe by having them implement the SingleThreadModel interface. This is done by adding the directive <%@ page isThreadSage=”false” %> withi n your JSP page. With this, instead of a single instance of the servlet generated for your JSP page loaded in memory, you will have N instance of the servlet loaded and initialized, with the service method of each instance effectively synchronized.

How do I prevent the output of my JSP or servlet pages from being caches by the browser?

Set the appropriate HTTP header attributes to prevent the dynamic content output by the JSP page from being cached by the browser. Execute the following scriptlet at the beginning of JSP pages to prevent them from being caches at 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

%>

AddThis Social Bookmark Button