Java Cie2 Ans Key
Java Cie2 Ans Key
4. The standard data structure in Java can be implemented in Java using some
library classes and methods. These classes are present in java.util package.
Example :
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
names.add("Bob");
names.add("Charlie");
names.add("David");
names.add("Eve");
// Sort the list using the sort method of the Collections class
Collections.sort(names);
Q2) Explain different iterators used for accessing the elements with an example?
• Iterator: It can be used for any collection that implements the Collection
interface, such as List, Set, Queue, etc. It can traverse in the forward direction
and can remove elements. It has three methods: hasNext(), next(), and
remove().
• ListIterator: This is a special iterator introduced in JDK 1.2. It can only be
used for lists that implement the List interface, such as ArrayList, LinkedList,
etc. It can traverse in both forward and backward directions and can add,
remove, and modify elements. It has several methods, such as hasNext(),
add(), set(), remove(), etc.
Example :
import java.util.Iterator;
Q3) Write a program to copy one program to copy one file to another ?
A) // Import the Path and Files classes
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
• In short JDBC helps the programmers to write java applications that manage
these three programming activities: 1. Connect to a a data source, like a
database. 2. Send queries and update statements to the data base. 3.
Retrieve and process the results received from the database in answer to the
query.
• The JDBC driver manager ensures that the correct driver is used to access
each data source. The driver manager is capable of supporting multiple
concurrent drivers connected to multiple heterogeneous databases.
• Type 1: JDBC-ODBC bridge driver. This driver uses the ODBC driver to connect
to the database. It converts JDBC method calls into ODBC function calls. This
driver is discouraged because of performance and compatibility issues. It is
also removed from Java 8
• Type 2: Native-API driver. This driver uses the client-side libraries of the
database. It converts JDBC method calls into native calls of the database
API. This driver is not written entirely in Java and requires installation on the
client machine2
• Type 4: Thin driver. This driver converts JDBC calls directly into the vendor-
specific database protocol. This driver is fully written in Java and does not
require any installation or configuration on the client or server side. It is the
most recommended driver for Java applications2.
Q5) Write a program to demonstrate Applet and discusses about it’s life cycle
A) Program :
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
c:\>javac First.java
c:\>appletviewer First.java
init(): The init() method is the first method to run that initializes the applet. It can be
invoked only once at the time of initialization.
start(): The start() method contains the actual code of the applet and starts the
applet. It is invoked immediately after the init() method is invoked. Every time the
browser is loaded or refreshed, the start() method is invoked
stop(): The stop() method stops the execution of the applet. The stop () method is
invoked whenever the applet is stopped, minimized, or moving from one tab to
another in the browser, the stop() method is invoked.
destroy(): The destroy() method destroys the applet after its work is done. It is
invoked when the applet window is closed or when the tab containing the webpage
is closed. It removes the applet object from memory and is executed only once. We
cannot start the applet once it is destroyed.
paint(): The paint() method belongs to the Graphics class in Java. It is used to draw
shapes like circle, square, trapezium, etc., in the applet. It is executed after the start()
method and when the browser or applet windows are resized.
The init(), start(), stop() and destroy() methods belongs to the applet.Applet class.
Stages of the Servlet Life Cycle: The Servlet life cycle mainly goes through four
stages,
Loading a Servlet.
Initializing the Servlet.
Request handling.
Destroying the Servlet.
The following figure depicts a typical servlet life-cycle scenario.
The servlet is initialized by calling the init() method.
The servlet calls service() method to process a client's request.
The servlet is terminated by calling the destroy() method.
Finally, servlet is garbage collected by the garbage collector of the JVM.
The init method is called only once. It is called only when the servlet is created, and
not called for any user requests afterwards. So, it is used for one-time initializations,
just as with the init method of applets.
public void init() throws ServletException {
// Initialization code...
}
The service() method is the main method to perform the actual task. The servlet
container (i.e. web server) calls the service() method to handle requests coming
from the client( browsers) and to write the formatted response back to the client.
A GET request results from a normal request for a URL or from an HTML form that
has no METHOD specified and it should be handled by doGet() method.
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}
A POST request results from an HTML form that specifically lists POST as the
METHOD and it should be handled by doPost() method.
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}
The destroy() method is called only once at the end of the life cycle of a servlet.
After the destroy() method is called, the servlet object is marked for garbage
collection.
public void destroy() {
// Finalization code...
}
// Create a JLabel
JLabel label = new JLabel("Enter your name:");
panel.add(label);
// Create a JTextField
JTextField textField = new JTextField(20);
panel.add(textField);
2. doGet() and doPost(): These are the most commonly used methods for
handling HTTP requests in a servlet. The doGet() method is invoked for HTTP
GET requests, while doPost() is invoked for HTTP POST requests. You can
override these methods in your servlet to process the incoming requests.
4. Request Headers: Servlets can access various request headers sent by the
client, such as User-Agent, Referer, Cookie, etc. These headers provide
additional information about the client or the request itself. You can use
methods like getHeader() or getHeaders() to retrieve specific headers or
iterate over all headers.
5. Request Attributes: Servlets can set and retrieve request attributes. Request
attributes are objects that are specific to a particular request and can be
accessed by other servlets or JSPs involved in processing the request.
Request attributes are useful for passing data between components during
request processing.