JSP and Servlet Interview Questions Part-2

1} What is the <load-on-startup> element?
Ans: The <load-on-startup> element of a deployment descriptor is used to load a servlet file when the server starts instead of waiting for the first request. It is also used to specify the order in which the files are to be loaded.

2} What is session?
Ans: A session refers to all the requests that a sing le client mig ht make to a server in the course of viewing any pag es associated with a g iven application. Sessions are specific to both the individual user and the application.

3} What is the session tracking ?
Ans: Session tracking is a mechanism that servlets use to maintain state about a series of requests from the same user (requests orig inating from the same browser) across some period of time.

4} What is the need of session trac king in web application?
Ans: HT T P is a stateless protocol. Every request is treated as new request. For web applications to be more realistic they have to retain information across multiple requests. Such information which is part of the application is referred as "state". T o keep track of this state we need session tracking .

5} What are the different types of session tracking ?
Ans: Different types are:
URL rewriting
Hidden Form Fields
Cookies
Secure Socket Layer (SSL) Sessions

6} How do I use cookies to store session state on client?
Ans: In a servlet, the HttpServletResponse and HttpServletRequest objects passed to method HttpServlet. Service () can be used to create cookies on the client and use cookie information transmitted during client requests. JSPs can also use cookies, in scriptlet code or, preferably, from within custom tag code. To set a cookie on the client, use the addCookie() method in class HttpServletResponse. Multiple cookies may be set for the same request, and a sing le cookie name may have multiple values. T o g et all of the cookies associated with a sing le HTTP request, use the g etCookies() method of class
HttpServletRequest

7} What are the advantages of storing session state in cookies?
Ans: Cookies are usually persistent, so for low-security sites, user data that needs to be stored long -term (such as a user ID, historical information, etc.) can be maintained easily with no server interaction. For small- and medium- sized session data, the entire session data (instead of just the session ID) can be kept in the cookie.

8} What is URL rewriting ?
Ans: URL rewriting is a method of session tracking in which some extra data is appended at the end of each URL. This extra data identifies the session. T he server can associate this session identifier with the data it has stored about that session.

9} How can destroyed session in servlet?
Ans: Using session.invalidate() method.

10} What is servlet lazy loading ?
Ans: A container does not initialize the servlets as soon as it starts up; it initializes a servlet when it receives a request for that servlet first time. This is called lazy loading .

What is servlet chaining ? 
Ans: Servlet Chaining is a method where the output of one servlet is piped into a second servlet. The output of the second servlet could be piped into a third servlet, and so on. The last servlet in the chain returns the output to the Web browser

11} What is filter?
Ans: Filters are Java components that are used to intercept an incoming request to a Web resource and a response sent back from the resource. It is used to abstract any useful information contained in the request or response.

12} What are the advantages of jsp over servlet?
Ans: The advantag e of JSP is that they are document-centric. Servlets, on the other hand, look and act like prog rams. A Java Server Pag e can contain Java prog ram frag ments that instantiate and execute Java classes, but these occur inside an HT ML template file and are primarily used to g enerate dynamic content. Some of the JSP functionality can be achieved on the client, using JavaScript. The power of JSP is that it is server-based and provides a framework for Web application development. 

13} What is the life cycle of jsp?
Ans: Life cyle of jsp:
T ranslation
Compilation
Loading the class
Instantiating the class
jspInit()
_jspService()
jspDestroy()

14} What is the jspInit() method?
Ans: The jspInit() method of the javax.servlet.jsp.JspPag e interface is similar to the init() method of servlets. This method is invoked by the container only once when a JSP pag e is initialized. It can be overridden by a page author to initialize resources such as database and network connections, and to allow a JSP page to read persistent config uration data.

15} What is the _jspServic e ()?
Ans: The _jspService() method of the javax.servlet.jsp.HttpJspPag e interface is invoked every time a new request comes to a JSP page. This method takes the HttpServletRequest and HttpServletResponse objects as its arg uments. A page author cannot override this method, as its implementation is provided by the container.

16} What is the jspDestroy ()?
Ans: The jspDestroy() method of the javax.servlet.jsp.JspPag e interface is invoked by the container when a JSP pag e is about to be destroyed. This method is similar to destroy() method of servlets. It can be overridden by a pag e author to perform any cleanup operation such as closing a database connection.

17} What jsp life cycle method can I override?
Ans: You cannot override the _jspService() method within a JSP page. You can however, override the jspInit() and jspDestroy() methods within a JSP page. JspInit() can be useful for allocating resources like database connections, network connections, and so forth for the JSP page. It is good prog ramming practice to free any allocated resources within jspDestroy().

18} What are implicit objects in jsp?
Ans: Implicit objects in JSP are the Java objects that the JSP Container makes available to developers in each pag e. These objects need not be declared or instantiated by the JSP author. They are automatically instantiated by the container and are accessed using standard variables; hence, they are called implicit objects.

19} How many implicit objects are available in jsp?
Ans: These implicit objects are available in jsp:
Request
Response
Pag eContext
session
application
Out
config
pag e
exception

20} What are jsp directives?
Ans: JSP directives are messages for the JSP engine. i.e., JSP directives serve as a message from a JSP page to the JSP container and control the processing of the entire page. They are used to set global values such as a class declaration, method implementation, output content type, etc.
They do not produce any output to the client.

What is page directive?
Ans: Page Directive is:
A page directive is to inform the JSP engine about the headers or facilities that page should get from the environment. The page directive is found at the top of almost all of our JSP pag es. There can be any number of page directives within a JSP page (although the attribute – value pair must be unique).
The syntax of the include directive is: <%@ page attribute="value">

21} What are the attributes of page directive?
Ans: There are thirteen attributes defined for a page directive of which the important attributes are as follows:
Import: It specifies the packages that are to be imported.
Session: It specifies whether a session data is available to the JSP page.
ContentType: It allows a user to set the content-type for a page.
IsELIgnored: It specifies whether the EL expressions are ig nored when a JSP is translated to a servlet.

22} What is the include directive?
Ans: Include directive is used to statically insert the contents of a resource into the current JSP. This enables a user to reuse the code without duplicating it, and includes the contents of the specified file at the translation time.

What are the jsp standard actions?
Ans: The JSP standard actions affect the overall runtime behaviour of a JSP page and also the response sent back to the client. They can be used to include a file at the request time, to find or instantiate a Java Bean, to forward a request to a new page, to generate a browser-specific code, etc.

23} What are the standards actions available in jsp?
Ans: The standards actions include:
<jsp:include>
<jsp:forward>
<jsp:useBean>
<jsp:setProperty>
<jsp:g etProperty>
<jsp:param>
<jsp:plug in>

24} What is the <jsp: useBean> standard action?
Ans: The <jsp: useBean> standard action is used to locate an existing Java Bean or to create a Java Bean if it does not exist. It has attributes to identify the object instance, to specify the lifetime of the bean, and to specify the fully qualified class path and type.

25} What is the scope available in <jsp: useBean>?
Ans: Scope includes:
Pag e scope
Request scope
application scope
session scope

26} What is the <jsp:forward> standard action?
Ans: The <jsp:forward> standard action forwards a response from a servlet or a JSP page to another page. The execution of the current page is stopped and control is transferred to the forwarded page.

27} What is the <jsp: include> standard action?
Ans: The <jsp: include> standard action enables the current JSP pag e to include a static or a dynamic resource at runtime. In contrast to the include directive, include action is used for resources that change frequently. The resource to be included must be in the same context.

28} What is the difference between include directive and include action?
Ans: The difference is:
Include directive, includes the content of the specified file during the translation phase–when the page is converted to a servlet. Include action, includes the response generated by executing the specified page
(a JSP page or a servlet) during the request processing phase–when the page is requested by a user. Include directive is used to statically insert the contents of a resource into the current JSP. Include standard action enables the current JSP page to include a static or a dynamic resource at runtime.

29} What is the difference between pag eContext.include () and <jsp: include>?
Ans: The <jsp: include> standard action and the pageContext.include() method are both used to include resources at runtime. However, the pageContext.include () method always flushes the output of the current page before
including the other components, whereas <jsp: include> flushes the output of the current page only if the value offlush is explicitly set to true.

30} What is the <jsp: setProperty> action?
Ans: You use jsp: setProperty to give values to properties of beans that have been referenced earlier.

31} What is the <jsp: getProperty> action?
Ans: The <jsp: getProperty> action is used to access the properties of a bean that was set using the action. The container converts the property to a String as follows:
If it is an object, it uses the toString () method to convert it to a String . If it is a primitive, it converts it directly to a String using the valueOf() method of the corresponding Wrapper class. The syntax of the <jsp: getProperty> method is: 
<jsp: getProperty name="Name" property="Property" />

32} What is the <jsp: param> standard action?
Ans: The <jsp: param> standard action is used with <jsp: include> or <jsp: forward> to pass parameter names
and values to the target resource.

What is the <jsp: plug in> action?
Ans: This action lets you insert the browser-specific OBJECT or EMBED element needed to specify that the browser run an applet using the Java plug in.

33} What is the scripting element?
Ans: JSP scripting elements let you insert Java code into the servlet that will be generated from the current JSP
page.
Expressions
Scriptlet
Declarations
comment

33} What is the scriptlet?
Ans: A scriptlet contains Java code that is executed every time a JSP is invoked. When a JSP is translated to a servlet, the scriptlet code g oes into the service() method.
Hence, methods and variables written in scriptlet are local to the service() method. A scriptlet is written between the <% and %>tag s and is executed by the container at request processing time.

34} What is the jsp declaration?
Ans: JSP declarations are used to declare class variables and methods in a JSP pag e. They are initialized when the class is initialized. Anything defined in a declaration is available for the whole JSP pag e. A declaration block is enclosed between the <%! and %>tag s. A declaration is not included in the service() method when a JSP is
translated to a servlet.

35} What is the jsp expression?
Ans: A JSP expression is used to write an output without using the out.print statement. It can be said as a shorthand representation for scriptlet. An expression is written between the <%= and %> tag s. It is not required to end the expression with a semicolon, as it implicitly adds a semicolon to all the expressions within the expression tags.

36} How is scripting disabled?
Ans: Scripting is disabled by setting the scripting -invalid element of the deployment descriptor to true. It is a subelement of jsp-property-group. Its valid values are true and false.