JSP Interview Questions
1 Briefly explain about Java Server Pages technology?
Ans: JavaServer Pages (JSP) technology provides a simplified, fast way to create web pages that display dynamically-generated content. The JSP specification, developed through an industry-wide initiative led by Sun Microsystems, defines the interaction between the server and the JSP page, and describes the format and syntax of the page.
2 what is a JSP Page?
Ans: A JSP page is a text document that contains two types of text: static data, which can be expressed in any text-based format (such as HTML, WML, XML, etc), and JSP elements, which construct dynamic content.JSP is a technology that lets you mix static content with dynamically-generated content.
3 why do I need JSP technology if I already have servlets?
Ans: JSP pages are compiled into servlets, so theoretically you could write servlets to support your web-based applications. However, JSP technology was designed to simplify the process of creating pages by separating web presentation from web content. In many applications, the response sent to the client is a combination of template data and dynamically-generated data. In this situation, it is much easier to work with JSP pages than to do everything with servlets.
4 How are the JSP requests handled?
Ans: The following sequence of events happens on arrival of jsp request:
a. Browser requests a page with .jsp file extension in web server.
b. Web server reads the request.
c. Using jsp compiler, webserver converts the jsp into a servlet class that implement the javax.servletjsp.jsp page interface. The jsp file compiles only when the page is first requested or when the jsp file has been changed.
e. The response is sent to the client by the generated servlet.
5 what are the advantages of JSP?
Ans: The following are the advantages of using JSP:
a. JSP pages easily combine static templates, including HTML or XML fragments, with code that generates dynamic content.
b. JSP pages are compiled dynamically into servlets when requested, so page authors can easily make updates to presentation code. JSP pages can also be precompiled if desired.
c. JSP tags for invoking JavaBeans components manage these components completely, shielding the page author from the complexity of application logic.
d. Developers can offer customized JSP tag libraries that page authors access using an XML-like syntax.
e. Web authors can change and edit the fixed template portions of pages without affecting the application logic. Similarly, developers can make logic changes at the component level without editing the individual pages that use the logic.
6 How is a JSP page invoked and compiled?
Ans: Pages built using JSP technology are typically implemented using a translation phase that is performed once, the first time the page is called. The page is compiled into a Java Servlet class and remains in server memory, so subsequent calls to the page have very fast response times.
7 what are Directives?
Ans: Directives are instructions that are processed by the JSP engine when the page is compiled to a servlet. Directives are used to set page-level instructions, insert data from external files, and specify custom tag libraries.
8 what are the different types of directives available in JSP?
Ans: The following are the different types of directives:
a. include directive : used to include a file and merges the content of the file with the current page
b. page directive : used to define page specific attributes like scripting language, error page, buffer, thread safety, etc
c. taglib : used to declare a custom tag library which is used in the page.
9 what are JSP actions?
Ans: JSP actions are executed when a JSP page is requested. Actions are inserted in the jsp page using XML syntax to control the behavior of the servlet engine. Using action, we can dynamically insert a file, reuse bean components, forward the user to another page, or generate HTML for the Java plugin. Some of the available actions are as follows:
<jsp: include> – include a file at the time the page is requested.
<jsp: useBean> – find or instantiate a JavaBean.
<jsp: setProperty> – set the property of a JavaBean.
<jsp: getProperty> – insert the property of a JavaBean into the output.
<jsp: forward> – forward the requester to a new page.
<jsp: plugin> – generate browser-specific code that makes an OBJECT or EMBED tag for the Java plugin.
10 what are Script lets?
Ans: Script lets are blocks of programming language code (usually java) embedded within a JSP page. Scriptlet code is inserted into the servlet generated from the page. Scriptlet code is defined between <% and %>
11 what are Decalarations?
Ans: Declarations are similar to variable declarations in Java. Variables are defined for subsequent use in expressions or script lets.
12 How to declare instance or global variables in jsp?
Ans: Instance variables should be declared inside the declaration part. The variables declared with JSP declaration element will be shared by all requests to the jsp page.
13 what are Expressions?
Ans: Expressions are variables or constants that are inserted into the data returned by the web server.
14 what is meant by implicit objects? And what are they?
Ans: Implicit objects are those objects which are available by default. These objects are instances of classes defined by the JSP specification. These objects could be used within the jsp page without being declared.
The following are the implicit jsp objects:
1. application
2. Page
3. Request
4. Response
5. Session
6. Exception
7. Out
8. Config
9. Page Context
15 How do I use Java Beans components (beans) from a JSP page?
Ans: The JSP specification includes standard tags for bean use and manipulation. The <jsp: useBean> tag creates an instance of a specific JavaBean class. If the instance already exists, it is retrieved. Otherwise, a new instance of the bean is created. The <jsp: setProperty> and <jsp: getProperty> tags let you manipulate properties of a specific bean.
16 what is the difference between <jsp: include> and <%@include :>
Ans: Both are used to insert files into a JSP page.
<%@include :> is a directive which statically inserts the file at the time the JSP page is translated into a servlet.
<jsp: include> is an action which dynamically inserts the file at the time the page is requested.
17 what is the difference between forward and send Redirect?
Ans: Both requestDispatcher.forward () and response.sendRedirect () is used to redirect to new url.Forward is an internal redirection of user request within the web container to a new URL without the knowledge of the user (browser). The request object and the http headers remain intact. send Redirect is normally an external redirection of user request outside the web container. send Redirect sends response header back to the browser with the new URL. The browser sends the request to the new URL with fresh http headers. send Redirect is slower than forward because it involves extra server call.
18 Can I create XML pages using JSP technology?
Ans: Yes, the JSP specification does support creation of XML documents. For simple XML generation, the XML tags may be included as static template portions of the JSP page. Dynamic generation of XML tags occurs through bean components or custom tags that generate XML output.
19 How is Java Server Pages different from Active Server Pages?
Ans: JSP is a community driven specification whereas ASP is a similar proprietary technology from Microsoft. In JSP, the dynamic part is written in Java, not Visual Basic or other MS-specific language. JSP is portable to other operating systems and non-Microsoft Web servers whereas it is not possible with ASP
20 Can’t JavaScript be used to generate dynamic content rather than JSP?
Ans: JavaScript can be used to generate dynamic content on the client browser. But it can handle only handles situations where the dynamic information is based on the client’s environment. It will not able to harness server side information directly.