Monday, December 29, 2008

JSP Interview Questions

Q)What are Jsp Tags?

A)Directive tag
Declaration tag
Expression tag
Scriptlet tag
This tag is used for defining the functions and variables to be used in the JSP.

In this tag we can insert any amount of valid java code and these codes are placed in _jspService method by the JSP engine.

We can use this tag to output any data on the generated page. These data are automatically converted to string and printed on the output stream

In the directives we can import packages, define error handling pages or the session information of the JSP page.


Q)What are implicit objects?

A)Certain objects that are available for the use in JSP documents without being declared first. These objects are parsed by the JSP engine and inserted into the generated servlet.


Q)Difference between forward and sendRedirect?

A)When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completly with in the web container.

When a sendRedirtect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completly new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward.and you want to forward to is not in the same web application, use sendRedirect().

Q)What are the different scope values for the Jsp Use Bean ?
1. page2. request3.session4.application

Q)Explain the life-cycle mehtods in JSP?
JSP Page Translation :
java servlet file is generated from the JSP source file and the generated java servlet file is compiled into a java servlet class.

jspInit( ):
This method is called when the instance is created and it is called only once during JSP life cycle. It is called for the servlet instance initialization.
This method is called for every request of this JSP during its life cycle. It passes the request and the response objects. _jspService() cannot be overridden.
This method is called when this JSP is destroyed and will not be available for future requests.

Q)How can I implement a thread-safe JSP page? What are the advantages and Disadvantages of using it?

A)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. With this, instead of a single instance of the servlet generated for your JSP page loaded in memory, you will have N instances of the servlet loaded and initialized, with the service method of each instance effectively synchronized. You can typically control the number of instances (N) that are instantiated for all servlets implementing SingleThreadModel through the admin screen for your JSP engine. More importantly, avoid using the tag for variables. If you do use this tag, then you should set isThreadSafe to true, as mentioned above. Otherwise, all requests to that page will access those variables, causing a nasty race condition. SingleThreadModel is not recommended for normal use. There are many pitfalls, including the example above of not being able to use <%! %>. You should try really hard to make them thread-safe the old fashioned way: by making them thread-safe .

Q)How can I enable session tracking for JSP pages if the browser has disabled cookies?

A)The session tracking uses cookies by default to associate a session identifier with a unique user. If the browser does not support cookies, or if cookies are disabled, you can still enable session tracking using URL rewriting. URL rewriting essentially includes the session ID 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 the session ID to a link is greatly simplified by means of of a couple of methods: response.encodeURL() associates a session ID with a given URL, and if you are using redirection, response.encodeRedirectURL() can be used by giving the redirected URL as input. Both encodeURL() and encodeRedirectedURL() first determine whether cookies are supported by the browser; if so, the input URL is returned unchanged since the session ID will be persisted as a cookie.

No comments:

Post a Comment

Your Ad Here ]]>