Sunday, June 21, 2009

JSP interview Questions

1. What is JSP?
JSP Stands for Java server pages. JSP is Java technology which is used by developers across the world to create dynamically generated websites with the easy use of other documents like HTML, XML. This technology of java allows developers to include java code and some pre defined actions into static content. Java server pages are compiled into java servlet by the java compiler or the java compiler may directly generate the byte code for the servlet.

2. How servlet differ from JSP?
Both Servlet and Java Server Pages are API which generate dynamic web content. A servlet is nothing but a java class which implements the interface to run within a web and on the other hand Java server pages is a bit complicated thing which contain a mixture of Java scripts, Java directives, Java elements and HTML. The main difference among both servlet and Java server pages is that JSP is document oriented and servlet on the other hand act likes a program.

3. What are the advantages of JSP?
There are many advantages of JSP and JSP is very much preferred by every coder. The main advantage of JSP over anything is its auto compilation and the length of code is reduced by using custom tags and tag library. Secondly, it is portable that is it works on all operating system and non – Microsoft web servers. Java components can be easily embedded into the dynamic pages. JSP have all the features of java and can easily separate dynamic part from the static part of the page.

4. What are the implicit objects in JSP?
There are all total 9 implicit objects in JSP. Application interface refers to the web application’s interface whereas Session interface refers to the user’s session. Request interface refers to the page which is currently requested whereas Response interface refers to the response which is currently made by the user. Config interface refers to the servlet configuration. Class like out, page, page Context and exception refers to the output stream of the page, servlet instance of the page, environment of the page, error handling respectively.

5. How JSP calls a stored procedure?
Java Server Pages possess all the characteristics of java and to call and have similar syntax to call a function. Functions and stored procedures of a database can be called by using the statement callable. Another way to call the stored procedure is by writing the JDBC code in between the tag of scriptlet tab.write.

6.What are the lifecycle phases of a JSP?
JSP page looks like a HTML page but is a servlet. When presented with JSP page the JSP engine does the following 7 phases.

1.Page translation: -page is parsed, and a java file which is a servlet is created.
2.Page compilation: page is compiled into a class file
3.Page loading : This class file is loaded.
4.Create an instance :- Instance of servlet is created
5.jspInit() method is called
6._jspService is called to handle service calls
7._jspDestroy is called to destroy it when the servlet is not required.

7.Why are JSP pages the preferred API for creating a web-based client program?
Because no plug-ins or security policy files are needed on the client systems(applet does). Also, JSP pages enable cleaner and more module application design because they provide a way to separate applications programming from web page design. This means personnel involved in web page design do not need to understand Java programming language syntax to do their jobs.

8.Is JSP technology extensible?
Yes, it is. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.

9.How does JSP handle run-time exceptions?
You can use the errorPage attribute of the page directive to have uncaught run-time exceptions automatically forwarded to an error processing page. For example: <%@ page errorPage="error.jsp" %>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: <%@ page isErrorPage="true" %> 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.

10. How do we use a scriptlet to initialize a newly instantiated bean?
A jsp:useBean action may optionally have a body. If the body is specified, its contents will be automatically invoked when the specified bean is instantiated. Typically, the body will contain scriptlets or jsp:setProperty tags to initialize the newly instantiated bean, although you are not restricted to using those alone.
The following example shows the “today” property of the Foo bean initialized to the current date when it is instantiated. Note that here, we make use of a JSP expression within the jsp:setProperty action.

"><%-- scriptlets calling bean setter methods go here --%>

11.What are custom tags and why it is needed?
JSP tags are extended by creating a custom set of tags which is called as tag library (taglib). The page which uses custom tags declares taglib and uniquely names, defines and associates a tag prefix to differentiate the usage of those tags.

12.How cookies is deleted in JSP?
There are two ways by which the cookies can be deleted in JSP. Firstly, by setting the setMaxAge() of the cookie class to zero. And secondly by setting a timer in the header file that is response. setHeader(expires {Mention the time} attribute), which will delete the cookies after that prescribed time.

13.Is it possible by a JSP page to process HTML form data?
Yes ,it is possible by simply obtaining the data from the FORM input via the request implicit object which lies with a scriptlet or expression but it doesn't require to implement any HTTP – Protocol methods like goGet() or doPost() within the JSP page.

No comments:

Post a Comment