You are on page 1of 15

ADVANCED JAVA ANSWER KEY

OCTOBER 2015
1. Attempt any two of the following:
a. Explain the use of adapter class with suitable example.
Java provides a special feature, called an adapter class, that can simplify the creation of
event handlers in certain situations. An adapter class provides an empty implementation
of all methods in an event listener interface. Adapter classes are useful when you want to
receive and process only some of the events that are handled by a particular event listener
interface.
You can define a new class to act as an event listener by extending one of the adapter
classes and implementing only those events in which you are interested.
For example, the MouseMotionAdapter class has two methods, mouseDragged( )
and mouseMoved( ), which are the methods defined by the MouseMotionListener
interface. If you were interested in only mouse drag events, then you could simply extend
MouseMotionAdapter and override mouseDragged( ). The empty implementation of
mouseMoved( ) would handle the mouse motion events for you.
The following example demonstrates an adapter. It displays a message in the status bar of
an applet viewer or browser when the mouse is clicked or dragged. However, all other
mouse events are silently ignored. The program has three classes. AdapterDemo extends
Applet. Its init( ) method creates an instance of MyMouseAdapter and registers that
object to receive notifications of mouse events. It also creates an instance of
MyMouseMotionAdapter
and registers that object to receive notifications of mouse motion events. Both of the
constructors take a reference to the applet as an argument.
MyMouseAdapter extends MouseAdapter and overrides the mouseClicked( ) method.
The other mouse events are silently ignored by code inherited from the MouseAdapter
class. MyMouseMotionAdapter extends MouseMotionAdapter and overrides the
mouseDragged( ) method. The other mouse motion event is silently ignored by code
inherited from the MouseMotionAdapter class.
Note that both of the event listener classes save a reference to the applet. This
information is provided as an argument to their constructors and is used later to invoke
the showStatus( ) method.
// Demonstrate an adapter.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="AdapterDemo" width=300 height=100>
</applet>
*/
public class AdapterDemo extends Applet {
public void init() {
addMouseListener(new MyMouseAdapter(this));
addMouseMotionListener(new MyMouseMotionAdapter(this));
}
}
class MyMouseAdapter extends MouseAdapter {
AdapterDemo adapterDemo;

10

public MyMouseAdapter(AdapterDemo adapterDemo) {


this.adapterDemo = adapterDemo;
}
// Handle mouse clicked.
public void mouseClicked(MouseEvent me) {
adapterDemo.showStatus("Mouse clicked");
}
}
class MyMouseMotionAdapter extends MouseMotionAdapter {
AdapterDemo adapterDemo;
public MyMouseMotionAdapter(AdapterDemo adapterDemo) {
this.adapterDemo = adapterDemo;
}
// Handle mouse dragged.
public void mouseDragged(MouseEvent me) {
adapterDemo.showStatus("Mouse dragged");
}
}
As you can see by looking at the program, not having to implement all of the methods
defined by the MouseMotionListener and MouseListener interfaces saves you a
considerable amount of effort and prevents your code from becoming cluttered with
empty methods. As an exercise, you might want to try rewriting one of the keyboard
input examples shown earlier so that it uses a KeyAdapter.
b. Explain the event delegation model.
The Delegation Event Model
The modern approach to handling events is based on the delegation event model, which
defines standard and consistent mechanisms to generate and process events. Its concept is
quite simple:
a source generates an event and sends it to one or more listeners. In this scheme, the
listener simply waits until it receives an event. Once an event is received, the listener
processes the event and then returns. The advantage of this design is that the application
logic that processes events is cleanly separated from the user interface logic that
generates those events. Auser interface element is able to delegate the processing of an
event to a separate piece of code.
In the delegation event model, listeners must register with a source in order to receive an
event notification. This provides an important benefit: notifications are sent only to
listeners that want to receive them. This is a more efficient way to handle events than the
design used by the old Java 1.0 approach. Previously, an event was propagated up the
containment hierarchy until it was handled by a component. This required components to
receive events that they did not process, and it wasted valuable time. The delegation
event model eliminates this overhead.
NOTE Java also allows you to process events without using the delegation event model.
However, the delegation event model is the preferred design for the reasons just cited.
The following sections define events and describe the roles of sources and listeners.
Events
In the delegation model, an event is an object that describes a state change in a source. It
can be generated as a consequence of a person interacting with the elements in a
graphical user interface. Some of the activities that cause events to be generated are

pressing a button, entering a character via the keyboard, selecting an item in a list, and
clicking the mouse. Many other user operations could also be cited as examples.
Events may also occur that are not directly caused by interactions with a user interface.
For example, an event may be generated when a timer expires, a counter exceeds a value,
a software or hardware failure occurs, or an operation is completed. You are free to
define events that are appropriate for your application.
Event Sources
Asource is an object that generates an event. This occurs when the internal state of that
object changes in some way. Sources may generate more than one type of event.
Asource must register listeners in order for the listeners to receive notifications about
a specific type of event. Each type of event has its own registration method. Here is the
general form:
public void addTypeListener(TypeListener el)
Here, Type is the name of the event, and el is a reference to the event listener. For
example, the method that registers a keyboard event listener is called addKeyListener( ).
The method that registers a mouse motion listener is called addMouseMotionListener( ).
When an event occurs, all registered listeners are notified and receive a copy of the event
object. This is known as multicasting the event. In all cases, notifications are sent only to
listeners that register to receive them.
Some sources may allow only one listener to register. The general form of such a method
is this:
public void addTypeListener(TypeListener el)
throws java.util.TooManyListenersException
Here, Type is the name of the event, and el is a reference to the event listener. When such
an event occurs, the registered listener is notified. This is known as unicasting the event.
A source must also provide a method that allows a listener to unregister an interest
in a specific type of event. The general form of such a method is this:
public void removeTypeListener(TypeListener el)
Here, Type is the name of the event, and el is a reference to the event listener. For
example, to remove a keyboard listener, you would call removeKeyListener( ).
The methods that add or remove listeners are provided by the source that generates
events. For example, the Component class provides methods to add and remove keyboard
and mouse event listeners.
Event Listeners
Alistener is an object that is notified when an event occurs. It has two major
requirements.
First, it must have been registered with one or more sources to receive notifications about
specific types of events. Second, it must implement methods to receive and process these
notifications.
The methods that receive and process events are defined in a set of interfaces found in
java.awt.event. For example, the MouseMotionListener interface defines two methods to
receive notifications when the mouse is dragged or moved. Any object may receive and
process one or both of these events if it provides an implementation of this interface.
Many other listener interfaces are discussed later in this and other chapters.
c. Write AWT based Java program that will read a string from user and display the
number of characters in the string.
d. List various Event listener interfaces. Explain any two.
Different event listener interfaces are :

ActionListener
AdjustmentListener
ComponentListener
ContainerListener
FocusListener
ItemListener
KeyListener
MouseListener
MouseMotionListener
MouseWheelListener
TextListener
WindowFocusListener
WindowListener
Explanation
The AdjustmentListener Interface
This interface defines the adjustmentValueChanged( ) method that is invoked when an
adjustment event occurs. Its general form is shown here:
void adjustmentValueChanged(AdjustmentEvent ae)
The ComponentListener Interface
This interface defines four methods that are invoked when a component is resized,
moved,
shown, or hidden. Their general forms are shown here:
void componentResized(ComponentEvent ce)
void componentMoved(ComponentEvent ce)
void componentShown(ComponentEvent ce)
void componentHidden(ComponentEvent ce)
The ContainerListener Interface
This interface contains two methods. When a component is added to a container,
componentAdded( ) is invoked. When a component is removed from a container,
componentRemoved( ) is invoked. Their general forms are shown here:
void componentAdded(ContainerEvent ce)
void componentRemoved(ContainerEvent ce)
The FocusListener Interface
This interface defines two methods. When a component obtains keyboard focus,
focusGained( )
is invoked. When a component loses keyboard focus, focusLost( ) is called. Their general
forms are shown here:
void focusGained(FocusEvent fe)
void focusLost(FocusEvent fe)
The ItemListener Interface
This interface defines the itemStateChanged( ) method that is invoked when the state of
an
item changes. Its general form is shown here:
void itemStateChanged(ItemEvent ie)
The KeyListener Interface
This interface defines three methods. The keyPressed( ) and keyReleased( ) methods are
invoked when a key is pressed and released, respectively. The keyTyped( ) method is

invoked when a character has been entered.


For example, if a user presses and releases the A key, three events are generated in
sequence: key pressed, typed, and released. If a user presses and releases the HOME key,
two key events are generated in sequence: key pressed and released.
The general forms of these methods are shown here:
void keyPressed(KeyEvent ke)
void keyReleased(KeyEvent ke)
void keyTyped(KeyEvent ke)
The MouseListener Interface
This interface defines five methods. If the mouse is pressed and released at the same
point,
mouseClicked( ) is invoked. When the mouse enters a component, the mouseEntered( )
method is called. When it leaves, mouseExited( ) is called. The mousePressed( ) and
mouseReleased( ) methods are invoked when the mouse is pressed and released,
respectively.
The general forms of these methods are shown here:
void mouseClicked(MouseEvent me)
void mouseEntered(MouseEvent me)
void mouseExited(MouseEvent me)
void mousePressed(MouseEvent me)
void mouseReleased(MouseEvent me)
The MouseMotionListener Interface
This interface defines two methods. The mouseDragged( ) method is called multiple
times as the mouse is dragged. The mouseMoved( ) method is called multiple times as the
mouse is moved. Their general forms are shown here:
void mouseDragged(MouseEvent me)
void mouseMoved(MouseEvent me)
The MouseWheelListener Interface
This interface defines the mouseWheelMoved( ) method that is invoked when the mouse
wheel is moved. Its general form is shown here:
void mouseWheelMoved(MouseWheelEvent mwe)
The TextListener Interface
This interface defines the textChanged( ) method that is invoked when a change occurs in
a text area or text field. Its general form is shown here:
void textChanged(TextEvent te)
The WindowFocusListener Interface
This interface defines two methods: windowGainedFocus( ) and windowLostFocus( ).
These are called when a window gains or loses input focus. Their general forms are
shown here:
void windowGainedFocus(WindowEvent we)
void windowLostFocus(WindowEvent we)
The WindowListener Interface
This interface defines seven methods. The windowActivated( ) and windowDeactivated(
) methods are invoked when a window is activated or deactivated, respectively. If a
window is iconified, the windowIconified( ) method is called. When a window is
deiconified, the windowDeiconified( ) method is called. When a window is opened or
closed, the windowOpened( ) or windowClosed( ) methods are called, respectively. The
windowClosing( ) method is called when a window is being closed. The general forms of
these methods are

void windowActivated(WindowEvent we)


void windowClosed(WindowEvent we)
void windowClosing(WindowEvent we)
void windowDeactivated(WindowEvent we)
void windowDeiconified(WindowEvent we)
void windowIconified(WindowEvent we)
void windowOpened(WindowEvent we)
2. Attempt any two of the following:
a. Write a Java program using swing components that displays table containg
employee information.
b. What is use of JcolorChooser? Write down the constructors and methods of the
same.
JColorChooser provides a pane of controls designed to allow a user to manipulate and
select a color.
Constructors
JColorChooser()
Creates a color chooser pane with an initial color of white.
JColorChooser(Color initialColor)
Creates a color chooser pane with the specified initial color.
JColorChooser(ColorSelectionModel model)
Creates a color chooser pane with the specified ColorSelectionModel.
Methods
Color getColor()
Gets the current color value from the color chooser.
void setColor(Color color)
Sets the current color of the color chooser to the specified color.
void setColor(int c)
Sets the current color of the color chooser to the specified color.
void setColor(int r, int g, int b)
Sets the current color of the color chooser to the specified RGB color.
static Color showDialog(Component component, String title, Color initialColor)
Shows a modal color-chooser dialog and blocks until the dialog is hidden.
c. Write a Java program using swing components to display tabbed pane with three
tabs for one for FirstYear, Second for Second Year and third for Third year. Every
tab displays a list of all subjects of respective year.
d. Explain JTree with example.
Atree is a component that presents a hierarchical view of data. The user has the ability to
expand or collapse individual subtrees in this display. Trees are implemented in Swing by
the JTree class. A sampling of its constructors is shown here:
JTree(Object obj[ ])
JTree(Vector<?> v)
JTree(TreeNode tn)
In the first form, the tree is constructed from the elements in the array obj. The second
form constructs the tree from the elements of vector v. In the third form, the tree whose
root node is specified by tn specifies the tree.
Although JTree is packaged in javax.swing, its support classes and interfaces are
packaged in javax.swing.tree. This is because the number of classes and interfaces

10

needed to support JTree is quite large.


JTree relies on two models: TreeModel and TreeSelectionModel. A JTree generates a
variety of events, but three relate specifically to trees: TreeExpansionEvent,
TreeSelectionEvent, and TreeModelEvent. TreeExpansionEvent events occur when a
node is expanded or collapsed. A TreeSelectionEvent is generated when the user selects
or deselects a node within the tree. ATreeModelEvent is fired when the data or structure
of the tree changes. The listeners for these events are TreeExpansionListener,
TreeSelectionListener,
and TreeModelListener, respectively. The tree event classes and listener interfaces are
packaged in javax.swing.event.
The event handled by the sample program shown in this section is TreeSelectionEvent.
To listen for this event, implement TreeSelectionListener. It defines only one method,
called valueChanged( ), which receives the TreeSelectionEvent object. You can obtain
the path to the selected object by calling getPath( ), shown here, on the event object.
TreePath getPath( )
It returns a TreePath object that describes the path to the changed node. The TreePath
class encapsulates information about a path to a particular node in a tree. It provides
several constructors and methods. In this book, only the toString( ) method is used. It
returns a string that describes the path.
The TreeNode interface declares methods that obtain information about a tree node.
For example, it is possible to obtain a reference to the parent node or an enumeration of
the child nodes. The MutableTreeNode interface extends TreeNode. It declares methods
that can insert and remove child nodes or change the parent node.
The DefaultMutableTreeNode class implements the MutableTreeNode interface. It
represents a node in a tree. One of its constructors is shown here:
DefaultMutableTreeNode(Object obj)
Here, obj is the object to be enclosed in this tree node. The new tree node doesnt have a
parent or children.
To create a hierarchy of tree nodes, the add( ) method of DefaultMutableTreeNode can be
used. Its signature is shown here:
void add(MutableTreeNode child)
Here, child is a mutable tree node that is to be added as a child to the current node.
JTree does not provide any scrolling capabilities of its own. Instead, a JTree is typically
placed within a JScrollPane. This way, a large tree can be scrolled through a smaller
viewport.
Here are the steps to follow to use a tree:
1. Create an instance of JTree.
2. Create a JScrollPane and specify the tree as the object to be scrolled.
3. Add the tree to the scroll pane.
4. Add the scroll pane to the content pane.
The following example illustrates how to create a tree and handle selections. The
program creates a DefaultMutableTreeNode instance labeled Options. This is the top
node of the tree hierarchy. Additional tree nodes are then created, and the add( ) method
is called to connect these nodes to the tree. A reference to the top node in the tree is
provided as the argument to the JTree constructor. The tree is then provided as the
argument to the JScrollPane constructor. This scroll pane is then added to the content
pane. Next, a label is created and added to the content pane. The tree selection is
displayed in this label. To receive selection events from the tree, a TreeSelectionListener
is registered for the tree.

Inside the valueChanged( ) method, the path to the current selection is obtained and
displayed.
// Demonstrate JTree.
import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.tree.*;
/*
<applet code="JTreeDemo" width=400 height=200>
</applet>
*/
public class JTreeDemo extends JApplet {
JTree tree;
JLabel jlab;
public void init() {
try {
SwingUtilities.invokeAndWait(
new Runnable() {
public void run() {
makeGUI();
}
}
);
} catch (Exception exc) {
System.out.println("Can't create because of " + exc);
}
}
private void makeGUI() {
// Create top node of tree.
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options");
// Create subtree of "A".
DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
top.add(a);
DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
a.add(a1);
DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");
a.add(a2);
// Create subtree of "B".
DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
top.add(b);
DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
b.add(b1);
DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
b.add(b2);
DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3");
b.add(b3);
// Create the tree.
tree = new JTree(top);
// Add the tree to a scroll pane.

JScrollPane jsp = new JScrollPane(tree);


// Add the scroll pane to the content pane.
add(jsp);
// Add the label to the content pane.
jlab = new JLabel();
add(jlab, BorderLayout.SOUTH);
// Handle tree selection events.
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent tse) {
jlab.setText("Selection is " + tse.getPath());
}
});
}
}
3. Attempt any two of the following:
a. Explain the ServletRequest and ServletResponse interface of Servlet.
When a servlet accepts a call from a client, it receives two objects.
ServletRequest encapsulates the communication from the client to server.
ServletResponse encapsulates the communication from the server to client.
ServletREquest interface allows the Servlet to access information such as
Names of parameter passed by client
Protocol scheme such as HTTP POST and PUT methods being used by client.
Names of the remote host that made the request.
Servlet that receives it
An input stream for reading binary data from the request body.
Subclass os ServletRequest allow the servlet to retrieve more protocol specific data.
Eg. HttpServletRequest
Defines an object to provide client request information to a servlet. The servlet container
creates a ServletRequest object and passes it as an argument to the servlet's service
method.
A ServletRequest object provides data including parameter name and values, attributes,
and an input stream.
Methods
java.lang.Object getAttribute(java.lang.String name)
Returns the value of the named attribute as an Object, or null if no attribute of the
given name exists.
java.util.Enumeration getAttributeNames()
Returns an Enumeration containing the names of the attributes available to this
request.
java.lang.String getParameter(java.lang.String name)
Returns the value of a request parameter as a String, or null if the parameter does
not exist.
java.util.Enumeration
getParameterNames()
Returns an Enumeration of String objects containing the names of the parameters
contained in this request.
java.lang.String[] getParameterValues(java.lang.String name)
Returns an array of String objects containing all of the values the given request
parameter has, or null if the parameter does not exist.

10

RequestDispatcher getRequestDispatcher(java.lang.String path)


Returns a RequestDispatcher object that acts as a wrapper for the resource located
at the given path.
java.lang.String getServerName()
Returns the host name of the server that received the request.
void removeAttribute(java.lang.String name)
Removes an attribute from this request.
void setAttribute(java.lang.String name, java.lang.Object o)
Stores an attribute in this request.
ServletResponse interface provides methods to the Servlet for replying to the client.
It allows Servlet
To set the content length and mime type of the reply.
Provides an output stream and a Writer.
Through ServletREsponse interface the Servlet can send the reply data.
Subclasses of ServletResponse provide the Servlet with more protocol-specific
capabilities.
Eg. HttpServletResponse
java.util.Locale getLocale()
Returns the locale assigned to the response.
ServletOutputStream getOutputStream()
Returns a ServletOutputStream suitable for writing binary data in the response.
java.io.PrintWriter getWriter()
Returns a PrintWriter object that can send character text to the client.
void reset()
Clears any data that exists in the buffer as well as the status code and headers.
void setLocale(java.util.Locale loc)
Sets the locale of the response, setting the headers (including the Content-Type's
charset) as appropriate.
b. Write a servlet program to display the factorial of a given number.
c. What is servlet? What are different tasks carried out by servlets?
A java Servlet is a server side program that services HTTP requests and returns the result
as HTTP responses. A servlet is a small, self-contained, portion of code that is executed
at the Web Server in response to an HTTP request from clients browser. Servlets are the
Web Server side counterpart to Applets. Applets run only on the client side within a Java
enabled browser.
Applets are Java application components, which are downloaded, on demand, run in a
Java enabled client browser and thus participate in the commercial application, which
needs them.
A Servlet is a module written using Java that runs in a server application.
It is most commonly used with HTTP and the world Servlet is often used in the meaning
of HTTP Servlet.
It is supported by virtually all Web servers and Application Servers.
It solves the performance problem by executing all requests as threads in one process
It can easily share resources
It can be easily ported to other platforms that support Java, as it runs inside a JVM.
When a user issues a request for a URL that corresponds to a Java Servlet, the server
hands the request off to the servlet for processing.
The servlet dynamically produces a response to the request, typically an HTML web page

and sends it back to the requesting web browser.


Servlets are also platform independent, they run using a set of standard interfaces (which
makes up the servlet Engine) and a JVM.
It is the JVM that is built to run on a specific hardware platform.
Thus if JVM exists for a specific hardware [platform any servlet crafted and validated on
any other hardware platform will run successfully.
Java servlet provide an object-oriented and extensible middle tire for web server based
applications.
Servlet can access all of the enterprise JAVA APIs such as JDBC,RMI ect.
Different tasks carried out by servlet are
A servlet can process and/or store data submitted by an HTML form
A servlet can provide dynamic content. Eg result of a query
A servlet can manage state information on top of the stateless HTTP.
(A stateless protocol does not require the server to retain session information or status
about each communications partner for the duration of multiple requests.)
Servlet can be used as a plug-in, that features a search engine or semi-customized
application such as web-based banking or inventory.
A servlet can handle multiple requests concurrently and these requests can be
synchronized to support systems allowing collaboration between people.
d. List various classes of Servlet API. Write the purpose of each.
GenericServlet
ServletInputStream
ServletOutputStream
Cookies
HttpServlet
HttpSessionBindingEvent
Httputil

4. Attempt any two of the following:


a. What are the disadvantages of JSP?
1. Attractive Java code
2. Java code required
3. Simple tasks are hard to code
4. Difficult looping in JSP
5. Occupies a lot of space
b. List various directives of JSP. Explain any two.
1. The Page directive
2. The include directive
3. the taglib directive
c. List and explain different types of JDBC drivers.
1. JDBC-ODBC Driver
2. Java Native Interface Driver
3. Java Network Protocol Driver
4. Java Database Protocol Driver
d. Write the purpose of the following JDBC classes

10

i. DriverManager ii. ResultSet


PreparedStatement

iii. Statement

iv. Connection

v.

DriverManager : Loads all the drivers in the memory at run time.


ResultSet : is a database result set generated from currently executed SQL statement.
Statement : interface represents a static SQL statement that can be used to retrieve
Resultset object(s). The objective is to pass to the database the SQL command for
execution and to retrieve output results from the database in th eform of Resultset.
Connection : interface reprsents a connection with a data source. Can be used to retrieve
information regarding the tables in th edatabase to which connection is opened.
PreparedStatement : is an SQL statement that is pre-compled and stored. This object
can then be executed multiple times much more efficiently than preparing and issuing the
same statement each time it is needed.
5. Attempt any two of the following:
a. List various phases of JSF lifecycle. Explain in short.
JSF application lifecycle consist of six phases which are as follows
Restore view phase
Apply request values phase; process events
Process validations phase; process events
Update model values phase; process events
Invoke application phase; process events
Render response phase
b. What is Facelet? Write the features of Facelet?
Facelets is commonly used term to refer to JavaServer Faces View Defination
Framework which is page declaration language developed for use with Java server Faces
technology. The concept of VDL introduced in JavaServer Faces 2.0 allows declaration
of UI components in different presentation technologies. Both JSP and facelets are
considered different inpmementation of VDL.
Facelet is built specifically for JavServer Faces. It is now theprefered technology for
building JavaServer Faces based applications and offers several advantages over using
JSP technology.
Facelets is a powerful but lightweight page declaration language that is used to build
JavaServer Faces views using HTML style templates and to build component trees.
Facelets features include the following:
Facelets as presentation technology.
Templatting and Composite Components through Faceltes.
New HTML tags for easier page creation.
Bookmarkability to generate hyperlinks based on component properties at render
time .
New components and event types for additional functionality.
Resource registration and relocation using annotations.
Implicit Navigation Rules if none are present in the application configuration
resource files.
Support for Bean Validation.
Project Stage to describe the status of the application in the project lifecycle.
Support for Ajax Integration.
c. Write the benefits of EJB.

10

Complete focus only on business logic


Reusable components
Portable
Fast Building Application
One business logic having many presentation logics
Distributed Deployment
Application Interoperability

d. Write the code that define stateless bean which convert an amount from one
currency to other.
6. Attempt any two of the following:
a. What is the need of Hibernate?
b. With suitable diagram explain the architecture of hibernate.

c. What is Value stack? Explain.


Value stack is a stack of objects.
The value stack is a set of several objects which keeps the following objects in the
provided order:
Temporary Objects
There are various temporary objects which are created during execution of a page. For
example the current iteration value for a collection being looped over in a JSP tag.
The Model Object
If you are using model objects in your struts application, the current model object is
placed before the action on the value stack
The Action Object
This will be the current action object which is being executed.
Named Objects
These objects include #application, #session, #request, #attr and #parameters and refer
to the corresponding servlet scopes
d. What is the purpose of Hibernate mapping file?
Write the Hibernate mapping file that maps the application to the table GuestBook
of GuestBook database. GuestBook table contain columns VisitorNo (Unique)
Integer, VisitorName varchar(75), VisitorMessage varchar(100) ,MessageDate

10

varchar(20).
7. Attempt any three of the following:
15
a. Write AWT based Java program that demonstrate the use of checkbox and
radiobuttons.
b. Write a Java program using swing components to divide the window into two
horizontal windows. First window displays a text box, a label and a button. Textbox
is used to accept a number from user. When user clicks on button the table of the
number is displayed in second window
c. What is the purpose of WEB-INF file? Explain.
A Web application exists as a structured hierarchy of directories. The root of this
hierarchy serves as the document root for files that are part of the application.
A special directory exists within the application hierarchy named WEB-INF. This
directory contains all things related to the application that arent in the document root of
the application. The WEB-INF node is not part of the public document tree of the
application. No file contained in the WEB-INF directory may be served directly to a
client by the container. However, the contents of the WEBINF directory are visible to
servlet code using the getResource and getResourceAsStream method calls on the
ServletContext, and may be exposed using the RequestDispatcher calls. Hence, if the
Application Developer needs access, from servlet code, to application specific
configuration information that he does not wish to be exposed directly to the Web client,
he may place it under this directory. Since requests are matched to resource mappings in
a case-sensitive manner, client requests for /WEB-INF/foo, /WEb-iNf/foo, for
example, should not result in contents of the Web application located under /WEB-INF
being returned, nor any form of directory listing thereof. The contents of the WEB-INF
directory are:
The /WEB-INF/web.xml deployment descriptor.
The /WEB-INF/classes/ directory for servlet and utility classes. The classes in this
directory must be available to the application class loader.
The /WEB-INF/lib/*.jar area for Java ARchive files. These files contain servlets,
beans, and other utility classes useful to the Web application. The Web application class
loader must be able to load classes from any of these archive files.
The Web application class loader must load classes from the WEB-INF/ classes directory
first, and then from library JARs in the WEB-INF/lib directory. Also, any requests from
the client to access the resources in WEB-INF/ directory must be returned with a
SC_NOT_FOUND(404) response. SRV.9.5.1 Exam
d. Explain the Request and Response implicit objects of JSP.
Request
The request object is an instance of a javax.servlet.http.HttpServletRequest object. Each
time a client requests a page the JSP engine creates a new object to represent that
request.
The request object provides methods to get HTTP header information including form
data, cookies, HTTP methods etc.
There are following important methods which can be used to read HTTP header in JSP
program. These methods are available with HttpServletRequest object which represents
client request to webserver.
getContextPath(),getCookies(),getQuerystring(), getRequestURL(), getServletPath(),

getSession(), getRequestURL(), authenticate(), login(), logout(), getParts(), getPart()


The response Object:
The response object is an instance of a javax.servlet.http.HttpServletResponse object.
Just as the server creates the request object, it also creates an object to represent the
response to the client.
The response object also defines the interfaces that deal with creating new HTTP
headers. Through this object the JSP programmer can add new cookies or date stamps,
HTTP status codes etc.
methods are available with HttpServletResponse object are:
addCookie(), encodeURL(), sendRedirect(), setStatus().

e. List and explain different types of Enterprise Beans.


1. Session Beans
a. Stateless Session Bean
b. Stateful Session Bean
c. Sigleton Session Bean
2. Message Driven Beans
f. What is the use of Action component of Strut 2? Write and explain various roles of
Action component.
Action is heart and soul of the Struts 2 framework. It processes input and interacts with
other layers of the application.
It is associated with HTTP request from user. Each HTTP request is mapped to specific
Action. This Action class holds the code spec that helps sreve the user request
Actions are used to :
Encapulate the actuat work to be done for a given request.
Serve as a data carrier from the request to the view.
Assist the framework in choosing theresponse that has to be the user i.e. view.
Role of Action
Performs as a model
Serves as data carrier
Helps determone result
Dingle or multiple results

You might also like