Search

JSP Interview Questions Part -2

  • Multiple simultaneous sessions are possible for a single user. Session information is local to each browser instance, since it's stored in URLs in each page being displayed. This scheme isn't foolproof, though, since users can start a new browser instance using a URL for an active session, and confuse the server by interacting with the same session through two instances.
  • Entirely static pages cannot be used with URL rewriting, since every link must be dynamically written with the session state. It is possible to combine static and dynamic content, using (for example) templating or server-side includes. This limitation is also a barrier to integrating legacy web pages with newer, servlet-based pages.

DisAdvantages
  • Every URL on a page which needs the session information must be rewritten each time a page is served. Not only is this expensive computationally, but it can greatly increase communication overhead.
  • URL rewriting limits the client's interaction with the server to HTTP GETs, which can result in awkward restrictions on the page.
  • URL rewriting does not work well with JSP technology.
  • If a client workstation crashes, all of the URLs (and therefore all of the data for that session) are lost.

38.How can an existing session be invalidated?
An existing session can be invalidated in the following two ways:
  • Setting timeout in the deployment descriptor: This can be done by specifying timeout between the <session-timeout>tags as follows:
<session-config>

       <session-timeout>10</session-timeout>

</session-config>
This will set the time for session timeout to be ten minutes.

  • Setting timeout programmatically: This will set the timeout for a specific session. The syntax for setting the timeout programmatically is as follows:
public void setMaxInactiveInterval(int interval)
The setMaxInactiveInterval() method sets the maximum time in seconds before a session becomes invalid.
Note :Setting the inactive period as negative(-1), makes the container stop tracking session, i.e, session never expires.

39.How can the session in Servlet can be destroyed?
An existing session can be destroyed in the following two ways:
  • Programatically : Using session.invalidate() method, which makes the container abonden the session on which the method is called.
  • When the server itself is shutdown.

40.A client sends requests to two different web components. Both of the components access the session. Will they end up using the same session object or different session ?
Creates only one session i.e., they end up with using same session .
Sessions is specific to the client but not the web components. And there is a 1-1 mapping between client and a session.

41.What is servlet lazy loading?
  • A container doesnot initialize the servlets ass soon as it starts up, it initializes a servlet when it receives a request for that servlet first time. This is called lazy loading.
  • The servlet specification defines the <load-on-startup> element, which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up.
  • The process of loading a servlet before any request comes in is called preloading or preinitializing a servlet.

42.What is Servlet Chaining?
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.

43.How are filters?
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. Some of the important functions performed by filters are as follows:
  • Security checks
  • Modifying the request or response
  • Data compression
  • Logging and auditing
  • Response compression
Filters are configured in the deployment descriptor of a Web application. Hence, a user is not required to recompile anything to change the input or output of the Web application.

44.What are the functions of an intercepting filter?
The functions of an intercepting filter are as follows:
  • It intercepts the request from a client before it reaches the servlet and modifies the request if required.
  • It intercepts the response from the servlet back to the client and modifies the request if required.
  • There can be many filters forming a chain, in which case the output of one filter becomes an input to the next filter. Hence, various modifications can be performed on a single request and response.
Intercepting Filter.gif


45.What are the functions of the Servlet container?
The functions of the Servlet container are as follows:
  • Lifecycle management : It manages the life and death of a servlet, such as class loading, instantiation, initialization, service, and making servlet instances eligible for garbage collection.
  • Communication support : It handles the communication between the servlet and the Web server.
  • Multithreading support : It automatically creates a new thread for every servlet request received. When the Servlet service() method completes, the thread dies.
  • Declarative security : It manages the security inside the XML deployment descriptor file.
  • JSP support : The container is responsible for converting JSPs to servlets and for maintaining them.

JSP Interview Questions

1) Explain and state about JSP?
Java server pages are meant to generate dynamic content in response to the user query. This technology helps you to interact with the user very effectively. Certain Java code elements and predefined actions are embedded into the functioning of the pages which help in generating dynamic content in static pages.
2) Explain about the process of compilation with regard to JSP?
A JSP complier helps to compile Java server pages into Java servlets. In turn this JSP compiler will generate a Java code which is compiled by a Java Compiler. It can generate byte code for the servlet directly. Java server pages can be modified on the fly which helps to provide a faster output without many modifications or reloads.
3) Give an outline or the steps involved in a JSP?
Java server page can be broken down into following contents: -
1) Static data
2) Directives of JSP such as include
3) Scripting elements and variables
4) JSP actions
5) Library and custom tags.
Java server pages directive controls the functioning of servlets.
4) Explain about the include directive?
This directive informs the JSP compiler about inclusion of a complete file for processing. Include directive helps in pasting or copying a full file into the compiler for processing. Files which have include directive have the extension jspf.
5) Explain about autoflush?
This command is used to autoflush the contents. If a value of true is used it indicates to flush the buffer whenever it is full. In case of false it indicates that an exception should be thrown whenever the buffer is full. If you are trying to access the page at the time of conversion of a JSP into servlet will result in error.
6) Explain the benefits of JSP?
These are some of the benefits due to the usage of JSP they are
• Portability, reusability and logic components of the language can be used across various platforms.
• Memory and exception management.
• Has wide range of API which increases the output functionality.
• Low maintenance and easy deployment.
• Robust performance on multiple requests.
7) Explain about the reusability of JSP?
Reusability is much required because it decreases the cost of development. These pages rely on reusable cross platform components. Java beans and Java beans components are used to perform complex functions with minimum ease. These components are cross platform which gives the leverage to share and use components.
8) Explain and state the three basic Java scripting elements?
The three basic Java scripting elements are declaration tag, scriptlet tag and expression tag. Declaration tag allows the developer to insert a variable definition inside the body of the servlet. Scriptlet allows contained statements and declaration allows methods.
9) Explain about JSP actions?
JSP actions are nothing but XML tags which extract the functionality of the web server. These actions are executed at run time. Some of the JSP actions are either developed by a developer or they are just built in functions.
10) Explain about JSP standard Tag library?
This tag forms the component of the java web application development platform. This adds a specification to the JSP tag thus giving it much more functionality to perform various tasks such as XML data processing, loops and internationalization and conditional execution.

11) Why model view controller paradigm should be used?
Model view controller paradigm should be used to separate three different view of the coding they are presentation, computer data storage and request processing. When the request is passed the control is transferred to the JSP. There are various platforms to split the model, design and data from the page.
12) Explain the four types of data variables which happen during the server request?
The four types of data variables happening during request are page scope, request scope, application scope and session scope. These objects act according to the scope specified to them. Some objects are accessible only in the page requested.
13) Explain about translation and execution of Java Server pages?
A java server page is executed within a Java container. A Java container converts a Java file into a servlet. Conversion happens only once when the application is deployed onto the web server. During the process of compilation Java compiler checks for modifications if any modifications are present it would modify and then execute it.
14) Explain the differences between ASP and JSP?
The biggest difference between both of these technologies lies with the design of the software. JSP technology is server and platform independent whereas ASP relies primarily on Microsoft technologies.
15) Explain about the role of servlets in JSP pages?
Whenever a JSP page is called it is first compiled into a servlet. Servlets help in building dynamic content in static pages. JSP is an extension of server technology. It is much easier to maintain pages with JSP technology.
16) What is the different form in which application logic is present?
Application logic is present in three different forms such as
1) Java scriplets
2) External functions are called by JSP tags.
3) Tags of JSP are customized. It gives the author access to Java Beans to perform basic functionality.

17) Explain about the java server Plugin?
Plugin makes a developer to embed applets into a web page. The

1.What are the advantages of JSP over Servlet?
JSP is a serverside technology to make content generation a simple appear.The advantage of JSP is that they are document-centric. Servlets, on the other hand, look and act like programs. A Java Server Page can contain Java program fragments that instantiate and execute Java classes, but these occur inside an HTML template file and are primarily used to generate 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.

2.What is the life-cycle of JSP?
When a request is mapped to a JSP page for the first time, it translates the JSP page into a servlet class and compiles the class. It is this servlet that services the client requests.
A JSP page has seven phases in its lifecycle, as listed below in the sequence of occurrence:
  • Translation
  • Compilation
  • Loading the class
  • Instantiating the class
  • jspInit() invocation
  • _jspService() invocation
  • jspDestroy() invocation

3.What is the jspInit() method?
The jspInit() method of the javax.servlet.jsp.JspPage interface is similar to the init() method of servlets. This method is invoked by the container only once when a JSP page 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 configuration data.

4.What is the _jspService() method?
SThe _jspService() method of the javax.servlet.jsp.HttpJspPage interface is invoked every time a new request comes to a JSP page. This method takes the HttpServletRequest and HttpServletResponse objects as its arguments. A page author cannot override this method, as its implementation is provided by the container.


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

6.What JSP lifecycle methods can I override?
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 programming practice to free any allocated resources within jspDestroy().

7.How can I override the jspInit() and jspDestroy() methods within a JSP page?
The jspInit() and jspDestroy() methods are each executed just once during the lifecycle of a JSP page and are typically declared as JSP declarations:
<%! 
        public void jspInit() {
               . . .
        }
%>
<%!     
        public void jspDestroy() {
               . . .   
        }
%>

8.What are implicit objects in JSP?
Implicit objects in JSP are the Java objects that the JSP Container makes available to developers in each page. 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.The implicit objects available in JSP are as follows:
  • request
  • response
  • pageContext
  • session
  • application
  • out
  • config
  • page
  • exception
The implicit objects are parsed by the container and inserted into the generated servlet code. They are available only within the jspService method and not in any declaration.
9.What are the different types of JSP tags?
The different types of JSP tags are as follows:
JSP tags

10.What are JSP directives?
  • 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.
  • Directives are always enclosed within <%@ ….. %> tag.
  • Ex: page directive, include directive, etc.

11.What is page directive?
  • A page directive is to inform the JSP engine about the headers or facilities that page should get from the environment.
  • Typically, the page directive is found at the top of almost all of our JSP pages.
  • 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">
  • Example:<%@ include file="header.jsp" %>

12.What are the attributes of page directive?
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 ignored when a JSP is translated to a servlet.

13.What is the include directive?
There are thirteen attributes defined for a page directive of which the important attributes are as follows:
  • The 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.
  • The syntax of the include directive is as follows:
    <%@ include file = "FileName" %>
  • This directive has only one attribute called file that specifies the name of the file to be included.

14.What are the JSP standard actions?
  • The JSP standard actions affect the overall runtime behavior 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 JavaBean, to forward a request to a new page, to generate a browser-specific code, etc.
  • Ex: include, forward, useBean,etc. object

15.What are the standard actions available in JSP?
The standard actions available in JSP are as follows:
  • <jsp:include>: It includes a response from a servlet or a JSP page into the current page. It differs from an include directive in that it includes a resource at request processing time, whereas the include directive includes a resource at translation time.
  • <jsp:forward>: It forwards a response from a servlet or a JSP page to another page.
  • <jsp:useBean>: It makes a JavaBean available to a page and instantiates the bean.
  • <jsp:setProperty>: It sets the properties for a JavaBean.
  • <jsp:getProperty>: It gets the value of a property from a JavaBean component and adds it to the response.
  • <jsp:param>: It is used in conjunction with <jsp:forward>;, <jsp:, or plugin>; to add a parameter to a request. These parameters are provided using the name-value pairs.
  • <jsp:plugin>: It is used to include a Java applet or a JavaBean in the current JSP page.

16.What is the <jsp:useBean> standard action?
The <jsp:useBean> standard action is used to locate an existing JavaBean or to create a JavaBean 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 classpath and type.

17.What are the scopes available in <jsp:useBean>?
The scopes available in <jsp:useBean> are as follows:
  • page scope:: It specifies that the object will be available for the entire JSP page but not outside the page.
  • request scope: It specifies that the object will be associated with a particular request and exist as long as the request exists.
  • application scope: It specifies that the object will be available throughout the entire Web application but not outside the application.
  • session scope: It specifies that the object will be available throughout the session with a particular client.

18.What is the <jsp:forward> standard action?
  • 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.
  • The syntax of the <jsp:forward> standard action is : 
    <jsp:forward page="/targetPage" />
    Here, targetPage can be a JSP page, an HTML page, or a servlet within the same context.
  • If anything is written to the output stream that is not buffered before <jsp:forward>, an IllegalStateException will be thrown.
Note : Whenever we intend to use <jsp:forward> or <jsp:include> in a page, buffering should be enabled. By default buffer is enabled.

19.What is the <jsp:include> standard action?
The <jsp:include> standard action enables the current JSP page to include a static or a dynamic resource at runtime. In contrast to the include directive, the include action is used for resources that change frequently. The resource to be included must be in the same context.The syntax of the <jsp:include> standard action is as follows:
<jsp:include page="targetPage" flush="true"/>
Here, targetPage is the page to be included in the current JSP.

20.What is the difference between include directive and include action?
Include directive
Include action
The include directive, includes the content of the specified file during the translation phase–when the page is converted to a servlet.
The 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.
The include directive is used to statically insert the contents of a resource into the current JSP.
The include standard action enables the current JSP page to include a static or a dynamic resource at runtime.
Use the include directive if the file changes rarely. It’s the fastest mechanism.
Use the include action only for content that changes often, and if which page to include cannot be decided until the main page is requested.
21.Differentiate between pageContext.include and jsp:include?
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 of flush is explicitly set to true as follows:
         <jsp:include page="/index.jsp" flush="true"/> 

22.What is the jsp:setProperty action?
You use jsp:setProperty to give values to properties of beans that have been referenced earlier. You can do this in two contexts. First, you can use jsp:setProperty after, but outside of, a jsp:useBean element, as below:
<jsp:useBean id="myName" ... />
...
<jsp:setProperty name="myName" property="myProperty" ... />
In this case, the jsp:setProperty is executed regardless of whether a new bean was instantiated or an existing bean was found.

A second context in which
jsp:setProperty can appear is inside the body of a jsp:useBean element, as below:
<jsp:useBean id="myName" ... >
  ...
  <jsp:setProperty name="myName" 
                   property="someProperty" ... />
</jsp:useBean>
Here, the jsp:setProperty is executed only if a new object was instantiated, not if an existing one was found.


23.What is the jsp:getProperty action?
The <jsp:getProperty> action is used to access the properties of a bean that was set using the <jsp:getProperty> 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" />
Here, name is the id of the bean from which the property was set. The property attribute is the property to get. A user must create or locate a bean using the <jsp:useBean> action before using the <jsp:getProperty> action.


24.What is the <jsp:param> standard action?
The <jsp:param> standard action is used with <jsp:include> or <jsp:forward> to pass parameter names and values to the target resource. The syntax of the <jsp:param> standard action is as follows:
<jsp:param name="paramName" value="paramValue"/>

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

26.What are scripting elements?
JSP scripting elements let you insert Java code into the servlet that will be generated from the current JSP page. There are three forms:
  1. Expressions of the form <%= expression %> that are evaluated and inserted into the output,
  2. Scriptlets of the form <% code %> that are inserted into the servlet's service method,
  3. Declarations of the form <%! code %> that are inserted into the body of the servlet class, outside of any existing methods.

27.What is a scriptlet?
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 goes into the service() method. Hence, methods and variables written in scriptlets are local to the service() method. A scriptlet is written between the <% and %> tags and is executed by the container at request processing time.


28.What are JSP declarations?
As the name implies, JSP declarations are used to declare class variables and methods in a JSP page. They are initialized when the class is initialized. Anything defined in a declaration is available for the whole JSP page. A declaration block is enclosed between the <%! and %> tags. A declaration is not included in the service() method when a JSP is translated to a servlet.

29.What is a JSP expression?
A JSP expression is used to write an output without using the out.print statement. It can be said as a shorthand representation for scriptlets. An expression is written between the <%= and %> tags. 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.

30.How is scripting disabled?
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. The syntax for disabling scripting is as follows:
<jsp-property-group>
   <url-pattern>*.jsp</url-pattern>
   <scripting-invalid>true</scripting-invalid>
</jsp-property-group>

Differences between jsp & servelet?
* JSP are good for operations that are HTML-intensive. 
* Servlets are good for operations that are Java-intensive. 
1. User enters information into an HTML form and the form is submitted to a servlet. 

2. This servlet retrieves the form information, and stores the changes in model JavaBeans (which typically save the data someplace permanent like a database). 

3. The controller servlet automatically redirects to a view JSP. 

4. The view JSP retrieves data from the model JavaBean and creates and HTML page to display the results. 

]Servlets are Fast because they are pure java classes...whereas Jsp is slow because first they coverted in java code that in class file....
1] Servlet is a pure java class..whereas jsp is not....
2] We can put HTML Code inside servlets...and jsp.
3]when we use HTML in java code than it is called servlet and when we use Java code is html thsn it is called jsp.......
4]servlet is static where as jsp is Dynamic.........

1 comment: