0% found this document useful (0 votes)
245 views

Advance Java Interview Questions and Answers

This document provides a summary of advance Java questions and answers related to JDBC, networking, servlets, and JSP. It covers topics such as the basic steps of using JDBC in Java, the main components of JDBC including DriverManager, Driver, Connection, Statement, and ResultSet. It also discusses prepared statements, stored procedures, isolation levels, scrollable and insensitive result sets, and different types of RowSets. The document is organized into sections for each topic and provides short answers to 25 questions.

Uploaded by

Deepak Bhagat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
245 views

Advance Java Interview Questions and Answers

This document provides a summary of advance Java questions and answers related to JDBC, networking, servlets, and JSP. It covers topics such as the basic steps of using JDBC in Java, the main components of JDBC including DriverManager, Driver, Connection, Statement, and ResultSet. It also discusses prepared statements, stored procedures, isolation levels, scrollable and insensitive result sets, and different types of RowSets. The document is organized into sections for each topic and provides short answers to 25 questions.

Uploaded by

Deepak Bhagat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

ADVANCE JAVA

Questions and Answers

Compiled by: Deepak Bhagat


Fusion Functional Consultant,
Solution Architect & ADF Trainer
Advance Java Questions and Answers

Table of Contents
1 JDBC ....................................................................................................................... 1
2 Networking: ............................................................................................................. 5
3 Servlets: .................................................................................................................. 6
4 JSP ....................................................................................................................... 11

ADF Essentials Training by Deepak Bhagat


Advance Java Questions and Answers

1 JDBC
1. What is JDBC?
Java Database Connectivity (JDBC) is a standard Java API to interact with relational databases
from Java. JDBC has a set of classes and interfaces which can use from Java application and talk
to the database without learning RDBMS details and using Database Specific JDBC Drivers

2. What are the basic steps of using JDBC in java?


The basic steps are:
Load the RDBMS specific JDBC driver because this driver communicates with the database.
Open the connection to the database which is then used to send SQL statements and get
results back.
Create a JDBC Statement object. This object contains the SQL query.
Execute statement which returns resultset(s). Resultset contains the tuples of the
database table as a result of the SQL query.
Process the result set.
Close the connection.

3. What is the main component of JDBC?


The main components are:
DriverManager
Driver
Connection
Statement
Resultset

4. What is DriverManager?
DriverManager is a static class. It manages a list of database drivers. Matches connection requests
from the java application with the proper database driver using the communication sub-protocol.
The first driver that recognizes a certain subprotocol under JDBC will be used to establish a
database connection.

5. What is a Driver?
The JDBC API defines the Java interfaces and classes that programmers use to connect to
databases and send queries. A JDBC driver implements these interfaces and classes for a particular
DBMS vendor.database communications link, handling all communication with the database.
Normally, once the driver is loaded, the developer need not call it explicitly.

6. What is the connection?


Interface with all methods for contacting a database. The connection object represents
communication context, i.e., all communication with the database is through connection object
only

7. What is the statement?


Encapsulates an SQL statement that is passed to the database.

1/17
ADF Essentials Training by Deepak Bhagat
Advance Java Questions and Answers

8. What is the resultset?


The Resultset represents a set of rows retrieved due to query execution.

9. How we load a database driver with JDBC?


Provided the JAR file containing the driver is properly configured, just place the JAR file in the
classpath. Java developers NO longer need to explicitly load JDBC drivers using code like
Class.forName() to register a JDBC driver.
The DriverManager class takes care of this by automatically locating a suitable driver when the
DriverManager.getConnection() method is called. This feature is backward-compatible, so
no changes are needed to the existing JDBC code.

10. What is the JDBC Driver interface?


The JDBC Driver interface provides vendor-specific implementations of the abstract classes
provided by the JDBC API. Each vendor driver must provide implementations of the
java.sql.Connection,Statement,PreparedStatement, CallableStatement,
ResultSet and Driver

11. What does the connection objects represent?


The connection object represents communication context, i.e., all communication with the
database is through connection object only.

12. What is the statement?


The statement acts like a vehicle through which SQL commands can be sent. Through the
connection object, we create a statement kind of object.

13. What is the prepared statement?


A prepared statement is an SQL statement that is precompiled by the database. Through
precompilation, prepared statements to improve the performance of SQL commands that are
executed multiple times. Once compiled, prepared statements can be customized before each
execution by altering predefined SQL parameters.

14. What is the difference between statement and PreparedStatement?


The difference is:
A standard Statement is used to create a Java representation of a literal SQL statement and
execute it on the database. A PreparedStatement is a precompiled statement. This means
that when the PreparedStatement is executed, the RDBMS can just run the
PreparedStatement SQL statement without having to compile it first.
The statement has to verify its metadata against the database every time. While a prepared
statement has to verify its metadata against the database only once.
If you want to execute the SQL statement once go for STATEMENT. If you want to execute a
single SQL statement multiple numbers of times, then go for PREPAREDSTATEMENT.
PreparedStatement objects can be reused with passing different values to the queries

15. What is the callable statement?


Callable statements are used from a JDBC application to invoke stored procedures and functions.

2/17
ADF Essentials Training by Deepak Bhagat
Advance Java Questions and Answers

16. How to call a stored procedure from JDBC?


PL/SQL stored procedures are called from within JDBC programs using the prepareCall()
method of the Connection object created. A call to this method takes variable bind parameters as
input parameters as well as output variables and creates an object instance of the
CallableStatement class.

17. What are the types of JDBC Driver?


The types are:
Type 1: JDBC/ODBC
Type2: Native API (Partly-Java driver)
Type 3: Open Protocol-Net
Type 4: Proprietary Protocol-Net (pure Java driver)

18. Which type of JDBC driver is the faster one?


JDBC Net pure Java driver (Type IV) is the fastest driver because it converts the JDBC calls into
vendor specific protocol calls and it directly interacts with the database.

19. Does the JDBC-ODBC Bridge support multiple concurrent open


statements per connection?
No, you can open only one Statement object per connection when you are using the JDBC-ODBC
Bridge.

20. What are the standard isolation levels defined by the JDBC?
The standard isolation levels are:
TRANSACTION_NONE
TRANSACTION_READ_COMMITTED
TRANSACTION_READ_UNCOMMITTED
TRANSACTION_REPEATABLE_READ
TRANSACTION_SERIALIZABLE

21. What is the resultset?


The ResultSet represents set of rows retrieved due to query execution. Example:
ResultSetrs = stmt.executeQuery(sqlQuery);

22. What are the types of resultset?


The types are:
TYPE_FORWARD_ONLY specifies that a resultset is not scrollable, that is, rows within it
can be advanced only in the forward direction.
TYPE_SCROLL_INSENSITIVE specifies that a resultset is scrollable in either direction but
is insensitive to changes committed by other transactions or other statements in the same
transaction.
TYPE_SCROLL_SENSITIVE specifies that a resultset is scrollable in either direction and is
affected by changes committed by other transactions or statements within the same
transaction.

3/17
ADF Essentials Training by Deepak Bhagat
Advance Java Questions and Answers

23. What is the difference between TYPE_SCROLL_INSENSITIVE and


TYPE_SCOLL_SENSITIVE?
An insensitive resultset is like the snapshot of the data in the database when the query was
executed. A sensitive resultset does NOT represent a snapshot of data; rather it contains points
to those rows which satisfy the query condition.
After we get the resultset the changes made to data are not visible through the resultset,
and hence they are known as insensitive. After we obtain the resultset if the data is modified
then such modifications are visible through a resultset.

24. What is the RowSet?


A RowSet is an object that encapsulates a set of rows from either Java Database Connectivity
(JDBC) result sets or tabular data sources like a file or spreadsheet. RowSets support component-
based development models like JavaBeans, with a standard set of properties and an event
notification mechanism.

25. What are the different types of RowSet?


The different types are:
Connected - A connected RowSet object connects to the database once and remains
connected until the application terminates.
Disconnected - A disconnected RowSet object connects to the database, executes a query to
retrieve the data from the database and then closes the connection. A program may change
the data in a disconnected RowSet while it is disconnected. Modified data can be updated in
the database after a disconnected RowSet re-establishes the connection with the database.

26. What is the need of BatchUpdates?


The BatchUpdates feature allows us to group SQL statements and send them to the database
server in one single trip.

27. What is the data source?


A DataSource object is the representation of a data source in the Java programming language. In
basic terms,
A DataSource is a facility for storing data.
DataSource can be referenced by JNDI.
Data Source may point to RDBMS; file System, any DBMS, etc.

28. What are the advantages of data source?


The advantages are:
An application does not need to hardcode driver information, as it does with the
DriverManager.
The DataSource implementations can easily change the properties of data sources.
The DataSource facility allows developers to implement a DataSource class to take advantage
of features like connection pooling and distributed transactions.

29. What is the main advantage of connection pooling?


A connection pool is a mechanism to reuse connections created. Connection pooling can increase
performance dramatically by reusing connections rather than creating a new physical connection
each time a connection is requested.
4/17
ADF Essentials Training by Deepak Bhagat
Advance Java Questions and Answers

2 Networking:
30. What is multiprogramming?
Multiprogramming is a rapid switching of the CPU back and forth between processes.

31. What is the difference between TCP and UDP?


TCP is designed to provide reliable communication across a variety of reliable and unreliable
networks and internets.UDP provides a connectionless so it is an unreliable service. Delivery
and duplicate protection are not guaranteed.

32. What is the socket?


The combination of an IP address and a port number is called a socket.

33. What is the advantage of java socket?


The advantages are:
Sockets are flexible and sufficient.
Efficient socket based programming can be easily implemented for general communications.
Sockets cause low network traffic.

34. What is the disadvantage of java socket?


The disadvantages are:
Security restrictions are sometimes overbearing because a Java applet running in a Web
browser is only able to establish connections to the machine where it came from, and to
nowhere else on the network.
Despite all of the useful and helpful Java features, Socket based communications allow only
to send packets of raw data between applications. Both the client-side and server-side have
to provide mechanisms to make the data useful in any way.
Since the data formats and protocols remain application-specific, the re-use of socket-based
implementations is limited.

35. What is RMI?


It stands for Remote Method Invocation. RMI is a set of APIs that allows building distributed
applications. RMI uses interfaces to define remote objects to turn local method invocations into
remote method invocations.

36. What is socket()?


The socket() is very similar to socketPair() except that only one socket is created instead of
two. This is most commonly used when if the process you wish to communicate with is not the
child process.

37. What is ServerSocket?


The ServerSocket class is used to create serverSocket. This object is used to communicate with
the client.

5/17
ADF Essentials Training by Deepak Bhagat
Advance Java Questions and Answers

38. What is bind()?


It binds the socket to the specified server and port in the SocketAddress object. Use this method
if you instantiated the ServerSocket using the no-argument constructor.

39. What is Datagram?


A datagram is an independent, self-contained message sent over the network whose arrival, arrival
time, and content are not guaranteed.

40. What is getLocalPort()?


It returns the port that the server socket is listening on. This method is useful if you passed in 0
as the port number in a constructor and let the server find a port for you.

What is accept()?
It waits for an incoming client. This method blocks until either a client connects to the server on
the specified port or the socket times out, assuming that the time-out value has been set using
the setSoTimeout() method. Otherwise, this method blocks indefinitely.

41. What is the network interface?


A network interface is the point of interconnection between a computer and a private or public
network. A network interface is generally a network interface card (NIC) but does not have to
have a physical form.

42. What is the encapsulation technique?


Hiding data within the class and making it available only through the methods. This technique is
used to protect your class against accidental changes to fields, which might leave the class in an
inconsistent state.

43. How does race condition occur?


It occurs when two or more processes are reading or writing some shared data and the final result
depends on who runs precisely when.

44. What information is needed to create a TCP Socket?


The socket is created from this information:
Local System's: IP Address and Port Number
Remote System’s: IPAddress and Port Number

3 Servlets:
45. What is servlet?
A servlet is a Java programming language class that is used to extend the capabilities of servers
that host applications accessed using a request-response programming model. Before the servlet,
CGI scripting language was used as a server-side programming language.

46. What is the use of servlet?


Uses of servlet include:
Processing and storing data submitted by an HTML form.
Providing dynamic content.
6/17
ADF Essentials Training by Deepak Bhagat
Advance Java Questions and Answers
A Servlet can handle multiple requests concurrently and be used to develop a high-
performance system
Managing state information on top of the stateless HTTP.

47. What is the life cycle of a servlet?


The life cycle of Servlet:
Servlet class loading
Servlet instantiation
Initialization (call the init method)
Request handling (call the service method)
Removal from service (call the destroy method)

48. Why do we need a constructor in a servlet if we use the init()?


Even though there is an init method in a servlet that gets called to initialize it, a constructor is still
required to instantiate the servlet. Even though you as the developer would never need to explicitly
call the servlet's constructor, it is still being used by the container.

49. How servlet is loaded?


The servlet is loaded by:
The first request is made.
The server starts up (auto-load).
There is only a single instance which answers all requests concurrently. This saves memory
and allows a Servlet to easily manage persistent data.
The administrator manually loads.

50. When the servlet is unloaded?


Servlet gets unloaded when:
The server shuts down.
The administrator manually unloads.

51. What is the servlet interface?


The central abstraction in the Servlet API is the Servlet interface. All servlets implement this
interface, either directly or more commonly by extending a class that implements it.

52. What is the generic servlet class?


GenericServlet is an abstract class that implements the Servlet interface and the
ServletConfig interface. In addition to the methods declared in these two interfaces, this class also
provides simple versions of the lifecycle methods init () and destroy (), and implements the
log method declared in the ServletContext interface.

53. What is the difference between GenericServlet and HttpServlet?


The difference is:
The GenericServlet is an abstract class that is extended by HttpServlet to provide
HTTP protocol-specific methods. But HttpServlet extends the GenericServlet base
class and provides a framework for handling the HTTP protocol.

7/17
ADF Essentials Training by Deepak Bhagat
Advance Java Questions and Answers
The GenericServlet does not include protocol-specific methods for handling request
parameters, cookies, sessions and setting response headers. The HttpServlet subclass
passes generic service method requests to the relevant doGet () or doPost () method.
GenericServlet is not specific to any protocol. HttpServlet only supports HTTP and
HTTPS protocol.

54. Why the HttpServlet class is declared abstract?


The HttpServlet class is declared abstract because the default implementations of the main
service methods do nothing and must be overridden. This is a convenience implementation of the
Servlet interface, which means that developers do not need to implement all service methods.
If your servlet is required to handle doGet () requests, for example, there is no need to write a
doPost () method too.

55. Can servlet have a constructor?


Yes

56. What is the type of protocols supported by the HttpServlet?


It extends the GenericServlet base class and provides a framework for handling the HTTP
protocol. So, HttpServlet only supports HTTP and HTTPS protocol.

57. What is the difference between the doGet () and doPost ()?
The difference is:
In doGet() the parameters are appended to the URL and sent along with header information.
In doPost(), send the information through a socket back to the webserver and it won't show
up in the URL bar.
The amount of information you can send back using a GET is restricted as URLs can only be
1024 characters. You can send much more information to the server by using post and it's not
restricted to textual data either. It is possible to send files and even binary data such as
serialized Java objects!
DoGet() is a request for information.It does not change anything on the server.
(doGet () should be idempotent). doPost () provides information (such as placing an order
for merchandise) that the server is expected to remember.

58. When to use doGet() and when doPost()?


Always prefer to use GET (As because GET is faster than POST), except mentioned in the following
reason:
If data is sensitive.
Data is greater than 1024 characters.
If your application doesn’t need bookmarks.

59. How do I support both doGet () and doPost () from the same servlet?
The easy way is, just support POST, then have your doGet method call your doPost method.

8/17
ADF Essentials Training by Deepak Bhagat
Advance Java Questions and Answers

60. Should I override the service () method?


We never override the service method, since the HTTP Servlets have already taken care of it. The
default service function invokes the doXXX() method corresponding to the method of the HTTP
request. For example, if the HTTP request method is GET, doGet () method is called by default.
A servlet should override the doXXX() method for the HTTP methods that servlet supports.
Because the HTTP service method checks the request method and calls the appropriate handler
method, it is not necessary to override the service method itself. Only override the appropriate
doXXX() method.

61. What is ServletContext?


A servlet context object contains the information about the Web application of which the servlet
is a part. It also provides access to the resources common to all the servlets in the application.
Each Web application in a container has a single servlet context associated with it.

62. What is the difference between the ServletConfig and ServletContext


interface?
The ServletConfig interface is implemented by the servlet container to pass configuration
information to a servlet. The server passes an object that implements the ServletConfig
interface to the servlet's init () method. A ServletContext defines a set of methods that
a servlet uses to communicate with its servlet container.

63. What is the difference between forward() and sendRedirect()?


The difference is:
A forward is performed internally by the servlet. A redirect is a two-step process, where the
web application instructs the browser to fetch a second URL, which differs from the original.
The browser is completely unaware that it has taken place, so its original URL remains intact.
But in sendRedirect, the browser, in this case, is doing the work and knows that it's making a
new request.

64. What is the difference between forward() and include()?


The RequestDispatcher include() method inserts the contents of the specified resource directly in
the flow of the servlet response as if it were part of the calling servlet. The RequestDispatcher
forward() method is used to show a different resource in place of the servlet that was originally
called.

65. What is the use of servlet wrapper classes?


The HttpServletRequestWrapper and HttpServletResponseWrapper classes are designed to make
it easy for developers to create custom implementations of the servlet request and response types.
The classes are constructed with the standard HttpServletRequest and
HttpServletResponse instances respectively and their default behavior is to pass all
method calls directly to the underlying objects.

66. What is a deployment descriptor?


A deployment descriptor is an XML document with an .xml extension. It defines a component's
deployment settings. It declares transaction attributes and security authorization for an enterprise
bean.

9/17
ADF Essentials Training by Deepak Bhagat
Advance Java Questions and Answers
The information provided by a deployment descriptor is declarative and therefore it can be
modified without changing the source code of a bean.

67. What is the preinitialization of servlet?


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.
The servlet specification defines the 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.

68. What is the <load-on-startup> element?


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.

69. What is a session?


A session refers to all the requests that a single client might make to a server in the course of
viewing any pages associated with a given application. Sessions are specific to both the individual
user and the application.

70. What is session tracking?


Session tracking is a mechanism that servlets use to maintain state about a series of requests
from the same user (requests originating from the same browser) across some period.

71. What is the need for session tracking in a web application?


HTTP is a stateless protocol. Every request is treated as a 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 to as "state". To keep track of this state we need session
tracking.

72. What are the different types of session tracking?


Different types are:
URL rewriting
Hidden Form Fields
Cookies
Secure Socket Layer (SSL) Sessions

73. How do I use cookies to store the session state on the client?
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 single cookie name may have multiple
values.
To get all of the cookies associated with a single HTTP request, use the getCookies() method
of class HttpServletRequest

10/17
ADF Essentials Training by Deepak Bhagat
Advance Java Questions and Answers

74. What are the advantages of storing session state in cookies?


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.

75. What is URL rewriting?


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. The server can associate this session identifier
with the data it has stored about that session.

76. How can destroyed session in servlet?


Using session.invalidate() method.

77. What is servlet lazy loading?


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.

78. 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

79. What is the filter?


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.

4 JSP
80. What are the advantages of jsp over servlet?
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.

81. What is the life cycle of jsp?


Life cyle of jsp:
Translation
Compilation
Loading the class
Instantiating the class
jspInit()
_jspService()
jspDestroy()

11/17
ADF Essentials Training by Deepak Bhagat
Advance Java Questions and Answers

82. 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.

83. What is the _jspService ()?


The _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.

84. What is the jspDestroy ()?


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.

85. What jsp life cycle method 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().

86. What are the implicit objects in jsp?


Implicit objects in JSP are the Java objects that the JSP Container makes available to developers
on 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.

87. How many implicit objects are available in jsp?


These implicit objects are available in jsp:
Request
Response
PageContext
session
application
Out
config
page
exception

88. 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.

12/17
ADF Essentials Training by Deepak Bhagat
Advance Java Questions and Answers

89. What is the page directive?


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 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">

90. What are the attributes of the 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.

91. What is the include directive?


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.

92. 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 Java Bean, to forward a request to a new page, to generate a browser-specific code, etc.

93. What are the standards actions available in jsp?


The standards actions include:
<jsp:include>
<jsp:forward>
<jsp:useBean>
<jsp:setProperty>
<jsp:getProperty>
<jsp:param>
<jsp:plugin>

94. What is the <jsp: useBean> standard action?


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.

95. What is the scope available in <jsp: useBean>?


Scope includes:
Page scope
Request scope
application scope
13/17
ADF Essentials Training by Deepak Bhagat
Advance Java Questions and Answers
session scope

96. 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.

97. 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, include action is used for resources that
change frequently. The resource to be included must be in the same context.

98. What is the difference between include directive and include action?
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.

99. What is the difference 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.

100. What is the <jsp: setProperty> action?


You use jsp: setProperty to give values to the properties of beans that have been referenced
earlier.

101. What is the <jsp: getProperty> action?


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 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" />

102. 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.

14/17
ADF Essentials Training by Deepak Bhagat
Advance Java Questions and Answers

103. What is the <jsp: plugin> action?


This action lets you insert the browser-specific OBJECT or EMBED element needed to specify that
the browser runs an applet using the Java plugin.

104. What is the scripting element?


JSP scripting elements let you insert Java code into the servlet that will be generated from the
current JSP page.
Expressions
Scriptlet
Declarations
comment

105. What is the 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 scriptlet are local to the service() method. A scriptlet is
written between the <% and %>tags and is executed by the container at request processing
time.

106. What is the jsp declaration?


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.

107. What is 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 scriptlet. 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.

108. 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.

109. Why is _jspService () start with ‘_’?


_jspService() method will be written by the container hence any methods which are not to be
overridden by the end user are typically written starting with a '_'. This is the reason why we don't
override _jspService() method in any JSP page.

110. How to pre-compile jsp?


Add jsp_precompile as a request parameter and send a request to the JSP file. This will make the
jsp pre-compile. http://localhost:8080/jsp1/test.jsp?jsp_precompile=true
It causes execution of JSP life cycle until jspInit() method without executing _jspService() method.

111. What is the benefit of pre-compile jsp page?


It removes the start-up lag that occurs when a container must translate a JSP page upon receipt
of the first request.
15/17
ADF Essentials Training by Deepak Bhagat
Advance Java Questions and Answers

112. What is the difference between a variable declared inside the declaration
tag and variable declared in scriptlet?
Variable declared inside declaration part is treated as an instance variable and will be placed
directly at class level in the generated servlet. Variable declared in a scriptlet will be placed inside
_jspService () method of generated servlet. It acts like a local variable.

113. What are the three kinds of comments in jsp?


These are the three types of commenst in jsp:
JSP Comment: <%-- this is jsp comment -- %>
HTML Comment: <! -- this is HTMl comment -- >
Java Comments: <% // single line java comment /* this is multiline comment */ %>

114. What is the output comment?


The comment which is visible in the source of the response is called output comment. <! -- this is
HTML comment -- >

115. What is a hidden comment?


This is also known as JSP comment and it is visible only in the JSP and in the rest of phases of
JSP life cycle, it is not visible. <%-- this is jsp comment -- %>

116. How does jsp handle the run time exception?


You can use the errorPage attribute of the page directive to have uncaught run-time exceptions
automatically forwarded to an error processing page.

117. How can I implement the thread safe jsp page?


You can make your JSPs thread-safe by having them implement the SingleThreadModel interface.
This is done by adding the directive in the JSP. <%@ page isThreadSafe="false" %>

118. Is there a way to reference the “this” variable within the jsp?
Yes, there is. The page implicit object is equivalent to "this", and returns a reference to the
generated servlet.

119. Can you make the use of servletOutputStream object within jsp?
Yes. By using getOutputStream () method on response implicit object we can get it.

120. What is 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 an error.

121. What is the different scope available in jsp?


The different scopes are:
Page: Within the same page.
Request: After forward or include also you will get the request scope data.

16/17
ADF Essentials Training by Deepak Bhagat
Advance Java Questions and Answers
Session: After sendRedirect also you will get the session scope data. All data stored in the
session is available to end-users until the session closed or browser closed.
Application: Data will be available throughout the application. One user can store data in
application scope and others can get the data from application scope.

122. When to use the application scope?


If we want to make our data available to the entire application, then we have to use the application
scope.

123. Can a JSP page instantiate a serialized bean?


No problem! The use of Bean action specifies the bean Name attribute, which can be used for
indicating a serialized bean.

124. In which situation we can use the static include and dynamic include?
If the target resource won’t change frequently, then it is recommended to use include directives.
If the target resource will change frequently, then it is recommended to use include action.

17/17
ADF Essentials Training by Deepak Bhagat

You might also like