Java Papers Word File
Java Papers Word File
Java Papers Word File
Example:import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Demo {
public Demo(){
JLabel text = new JLabel();
text.setVisible(true);
text.setBounds(50, 100, 200, 20);
JButton btn = new JButton("Click me");
btn.setVisible(true);
btn.setBounds(20, 20, 120, 20);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource().equals(btn)){
text.setText("ActionEvent Performed!!");
}
}
});
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(400, 400);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(btn);
f.add(text);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo d = new Demo();}}
-----------------------------------------------------------------------------------------------------------2.How does AWT create radio button?Explain with syntax and code
specification?(13-14)
JRadioButtons: This is normally used as one of a group of radio
buttons of which only one may be selected at a time. These are
grouped using a ButtonGroup and are usually used to select from a set
of mutually exclusive options.
It consists of a background rectangle and text and/or an icon. If it
includes an icon, the icon is used to visually reflect the current state of
the radio button.
Using the constructors listed below , radio buttons can be
created:
JRadioButton() : Creates an initially unselected radio button with no
set text. JRadioButton(Icon icon) : Creates an initially unselected
radio button with the specified image but no text.
JRadioButton(Icon icon, Boolean selected) : Creates a radio button
with the specified image and selection state, but no text.
JRadioButton(String text) : Creates an initially unselected radio
button with the specified text.
JRadioButton(String text, boolean selected) : Creates a radio
button with specified text and selection state.
JRadioButton(String text, Icon icon) : Creates a radio button that
has the specified text and image, and that is initially unselected.
Example:
import javax.swing.*;
import java.awt.*;
public class StudentBioData02
extends JFrame {
JLabel lbllang, lblstream;
JCheckBox cbeng, cbhin, cbmar;
JRadioButton rbart, rbcomm, rbsci;
ButtonGroup bg;
public StudentBioData02() {
lbllang = new JLabel("Languages Known");
lblstream = new JLabel("Stream");
cbeng = new JCheckBox("English", true);
cbhin = new JCheckBox("Hindi");
cbmar = new JCheckBox("Marathi");
rbart = new JRadioButton("Arts");
rbcomm = new JRadioButton("Commerce");
rbsci = new JRadioButton("Science");
bg = new ButtonGroup();
bg.add(rbart);
bg.add(rbcomm);
bg.add(rbsci);
Container con = getContentPane();
con.setLayout(new FlowLayout());
con.add(lbllang);
con.add(cbeng);
con.add(cbhin);
con.add(cbmar);
con.add(lblstream);
con.add(rbart);
con.add(rbcomm);
con.add(rbsci);
}
public static void main(String args[]) {
StudentBioData02 sbd = new StudentBioData02();
sbd.setSize(150, 200);
sbd.setVisible(true);}}
------------------------------------------------------------------------------------------------------------
Place components against any of the four borders of the container and
in the center.
The component in the center fills the available space.
You do not need specify all five areas of the container.
The component in the north region takes up the entire width of the
container along its top.
South does the same along the bottom.
The heights of north and south will be the preferred heights of the
added component.
The east and west areas are given the widths of the component each
contains, where the height is whatever is left in the container after
satisfying north's and south's height requirements.
Any remaining space is given to the component in the center region.
Constants used to specify areas: CENTER, EAST, NORTH, SOUTH, WEST
}} });
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(400, 400);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(btn);
f.add(text);}
public static void main(String[] args) {
Demo d = new Demo();}}
6.Explain adapter classes and inner classes?(14-15)
Java inner class or nested class is a class i.e. declared inside the
class or interface.
We use inner classes to logically group classes and interfaces in one
place so that it can be more readable and maintainable.
Additionally, it can access all the members of outer class including
private data members and methods.
Syntax of Inner class
class Java_Outer_class{ //code class Java_Inner_class{ //code
}}
Adapter Class is used to overcome the problem of writing diffrernt
method of empty immplementation.
Normally, if we use event Listener.then we need to override all its
methods.
If we want to override only certain specific method, then in that case
EventListener can be replaced with EventAdapater Class.
Example:
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class Demo extends JFrame{
public Demo(){
JLabel ulbl = new JLabel("Hello world!!");
ulbl.setVisible(true);
ulbl.setBounds(10, 10, 100, 25);
JButton btn = new JButton("Click me");
btn.setVisible(true);
btn.setBounds(10, 50, 120, 20);
btn.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e){
if(e.getSource().equals(btn)){
ulbl.setForeground(Color.red);}}});
setVisible(true);
setSize(200, 200);
setLayout(null);
add(ulbl);
add(btn);}
public static void main(String[] args) {
Demo d = new Demo();}}
example
import javax.swing.JFrame;
public class Main extends JFrame {
public static void main(String[] args) {
new Main();}
public Main() {
this.setSize(200, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Hello World!");
this.setVisible(true);}}
example
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);}});
8.what are different layoutManager classes in Java?Explain
CardLayout with example?(14-15)
The LayoutManagers are used to arrange components in a particular
manner. LayoutManager is an interface that is implemented by all the
classes of layout managers. There are following classes that represents
the layout managers:
java.awt.BorderLayout,java.awt.FlowLayout,java.awt.GridLayo
ut,java.awt.CardLayout,java.awt.GridBagLayout,javax.swing.Bo
xLayout,javax.swing.GroupLayout,javax.swing.ScrollPaneLayou
t,javax.swing.SpringLayout etc.
The BorderLayout is used to arrange the components in five regions:
north, south, east, west and center. Each region (area) may contain
one component only. It is the default layout of frame or window.
The FlowLayout is used to arrange the components in a line, one after
another. It is the default layout of applet or panel.
The BoxLayout is used to arrange the components either vertically or
horizontally. For this purpose, BoxLayout provides four constants.
cl.setDefaultCloseOperation(EXIT_ON_CLOSE); }}
9.Explain use of adapter classes with suitable example?(15-16)
An adapter class provides the default implementation of all methods
in an event listener interface. Adapter classes are very useful when
you want to process only few of the events that are handled by a
particular event listener interface. You can define a new class by
extending one of the adapter classes and implement only those events
relevant to you.
Example:
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class Demo extends JFrame{
public Demo(){
JLabel ulbl = new JLabel("Hello world!!");
ulbl.setVisible(true);
ulbl.setBounds(10, 10, 100, 25);
JButton btn = new JButton("Click me");
btn.setVisible(true);
btn.setBounds(10, 50, 120, 20);
btn.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e){
if(e.getSource().equals(btn)){
ulbl.setForeground(Color.red);}}});
setVisible(true);
setSize(200, 200);
setLayout(null);
add(ulbl);
add(btn);}
public static void main(String[] args) {
Demo d = new Demo();}}
a mouse is moved
So,The features of JFC are as Follows: JFC is short for Java Foundation Classes, which encompass a group of
features for building graphical user interfaces (GUIs) and adding rich
graphics functionality and interactivity to Java applications. It is defined
as containing the features shown in the table below.
2.How can the user be made aware about the software loading
process?Which component is facilitating the same?Explain with code
specifications?****
3.How to divide frame window in two parts?Explain with code
specifications?
Yes, the frame window in Java can be divided in to two parts by using
JSplitPane
A JSplitPane displays two components, either side by side or one on top
of the other.
By dragging the divider that appears between the components, the
user can specify how much of the split pane's total area goes to each
component. You can divide screen space among three or more
components by putting split panes inside of split panes, as described in
Nesting Split Panes.
f you want to create a split pane with an arbitrary number of
components, you should check out Hans Muller's article,
MultiSplitPane: Splitting Without Nesting.
The continuousLayout property setting determines how the
split pane reacts when the user drags the divider.
false (the default), only the divider is redrawn when dragged.
true, the JSplitPane resizes and redraws the components on each side
of the divider as the user drags the divider.
Example:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
public class SwingSplitSample {
public static void main(String args[]) {
JFrame frame = new JFrame("JSplitPane Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
4.write down java swing program that creates the below mentioned
hierarchical tree data Structure? Science>BSCIT>--NS>--ST>-ASP.NET>--Advance Java>--Linux
5.Explain Root pane, Glass pane, Layered pane, Content Pane and
Desktop pane.
RootPane
Each top-level container relies on a reclusive intermediate container
called the root pane. The root pane manages the content pane and the
menu bar, along with a couple of other containers.
If you need to intercept mouse clicks or paint over multiple
components, you should get acquainted with root panes.
We've already discussed about the content pane and the optional
menu bar. The two other components that a root pane adds are a
layered pane and a glass pane.
The layered pane directly contains the menu bar and content pane,
and enables Zordering of other components you might add. The glass
pane is often used to intercept input events occuring over the top-level
container, and can also be used to paint over multiple components.
Syntax:
javax.swing.JRootPane Panes = new
javax.swing.JRootPane();
glassPane
The glassPane sits on top of all other components in the JRootPane.
This positioning makes it possible to intercept mouse events, which is
LayeredPane:
The layeredPane is the parent of all children in the JRootPane. It is an
instance of JLayeredPane, which provides the ability to add
components at several layers.
This capability is very useful when working with popup menus, dialog
boxes, and dragging -- situations in which you need to place a
component on top of all other components in the pane.
Syntax:
javax.swing.JLayeredPane jlp = new
javax.swing.JLayeredPane();
ContentPane:
Here's the code that is used to get a frame's content pane and add the
yellow label to it: frame.getContentPane().add(yellowLabel,
BorderLayout.CENTER);
As the code shows, you find the content pane of a top-level container
by calling the getContentPane method. The default content pane is a
simple intermediate container that inherits from JComponent, and that
uses a BorderLayout as its layout manager.
It's easy to customize the content pane -- setting the layout manager
or adding a border, for example. The getContentPane method returns a
Container object, not a JComponent object.
DesktopPane:
A container used to create a multiple-document interface or a virtual
desktop. You create JInternalFrame objects and add them to
the JDesktopPane.
JDesktopPane extends JLayeredPane to manage the potentially
overlapping internal frames. It also maintains a reference to an
instance of DesktopManager that is set by the UI class for the current
look and feel (L&F). Note that JDesktopPane does not support borders.
This class is normally used as the parent of JInternalFrames to provide
a pluggable DesktopManager object to the JInternalFrames.
The installUI of the L&F specific implementation is responsible for
setting the desktopManager variable appropriately.
When the parent of a JInternalFrame is a JDesktopPane, it should
delegate most of its behavior to the desktopManager (closing, resizing,
etc).
implements Accessible
4.DIFFERENCE BETWEEN JAVABEANS AND ENTERPRISE JAVABEANS
As you see in the above figure, response of second servlet is sent to the
client. Response of the first servlet is not displayed to the user.
As you can see in the above figure, response of second servlet is included in
the response of the first servlet that is being sent to the client.
Syntax:- public RequestDispatcher getRequestDispatcher(String
resource);
In this example, we are validating the password entered by the user. If
password is servlet, it will forward the request to the WelcomeServlet,
otherwise will show an error message: sorry username or password error!. In
this program, we are cheking for hardcoded information. But you can check it
to the database also that we will see in the development chapter. In this
example, we have created following files:
</welcome-file-list>
</web-app>
4.Explain the lifecycle phases of servlet?
Java Servlets are server-side Java program modules that process and
answer client requests and implement the servlet interface. It helps in
enhancing Web server functionality with minimal overhead,
maintenance and support.
A servlet acts as an intermediary between the client and the server. As
servlet modules run on the server, they can receive and respond to
requests made by the client. Request and response objects of the
servlet offer a convenient way to handle HTTP requests and send text
data back to the client.
it also possesses all the Java features such as high portability, platform
independence, security and Java database connectivity.
The web container maintains the life cycle of a servlet instance.
Let's see the life cycle of the servlet:
Servlet class is loaded.
Servlet instance is created.
init method is invoked.
service method is invoked.
destroy method is invoked.
there are three states of a servlet: new, ready and end. The servlet is
in new state if servlet instance is created. After invoking the init()
method, Servlet comes in the ready state. In the ready state, servlet
performs all the tasks. When the web container invokes the destroy()
method, it shifts to the end state.
1) Servlet class is loaded
The classloader is responsible to load the servlet class. The servlet
class is loaded when the first request for the servlet is received by the
web container.
2) Servlet instance is created
The web container creates the instance of a servlet after loading the
servlet class. The servlet instance is created only once in the servlet
life cycle.
3) init method is invoked
The web container calls the init method only once after creating the
servlet instance. The init method is used to initialize the servlet. It is
the life cycle method of the javax.servlet.Servlet interface. Syntax of
the init method is given below:
public void init(ServletConfig config) throws ServletException
The web container calls the service method each time when request
for the servlet is received. If servlet is not initialized, it follows the first
three steps as described above then calls the service method. If servlet
is initialized, it calls the service method. Notice that servlet is initialized
only once. The syntax of the service method of the Servlet interface is
given below:
public void service(ServletRequest request, ServletResponse
response)
throws ServletException, IOException
5) destroy method is invoked
The web container calls the destroy method before removing the
servlet instance from the service. It gives the servlet an opportunity to
clean up any resource for example memory, thread etc. The syntax of
the destroy method of the Servlet interface is given below:
public void destroy()
7.Explain GenericServlet with its constructors and methods.
GenericServlet class implements Servlet, ServletConfig and Serializable
interfaces. It provides the implementation of all the methods of these
interfaces except the service method.
GenericServlet class can handle any type of request so it is protocolindependent.
You may create a generic servlet by inheriting the GenericServlet class
and providing the implementation of the service method.
There are many methods in GenericServlet class. They are as follows:
public void init(ServletConfig config) is used to initialize the
servlet.
public abstract void service(ServletRequest request,
ServletResponse response) provides service for the incoming
request. It is invoked at each time when user requests for a servlet.
public void destroy() is invoked only once throughout the life cycle
and indicates that servlet is being destroyed.
public ServletConfig getServletConfig() returns the object of
ServletConfig.
public String getServletInfo() returns information about servlet
such as writer, copyright, version etc.
public void init() it is a convenient method for the servlet
programmers, now there is no need to call super.init(config)
public ServletContext getServletContext() returns the object of
ServletContext.
JSP Support: JSPs doesnt look like normal java classes but every JSP
in the application is compiled by container and converted to Servlet
and then container manages them like other servlets.
Miscellaneous Task: Servlet container manages the resource pool,
perform memory optimizations, execute garbage collector, provides
security configurations, support for multiple applications, hot
deployment and several other tasks behind the scene that makes a
developer life easier.
FilterConfig
A filter configuration object used by a servlet container to
pass information to a filter during initialization.
RequestDispatcher Defines an object that receives requests from the
client and sends them to any resource (such as a servlet, HTML file, or JSP
file) on the server.
Servlet
Defines methods that all servlets must implement.
ServletConfig A servlet configuration object used by a servlet container
to pass information to a servlet during initialization.
ServletContext Defines a set of methods that a servlet uses to
communicate with its servlet container, for example, to get the MIME type of
a file, dispatch requests, or write to a log file.
ServletContextAttributeListener
Implementations of this interface
receive notifications of changes to the attribute list on the servlet context of
a web application.
ServletContextListener
Implementations of this interface receive
notifications about changes to the servlet context of the web application they
are part of.
ServletRequest Defines an object to provide client request information to a
servlet.
ServletRequestAttributeListener
A ServletRequestAttributeListener
can be implemented by the developer interested in being notified of request
attribute changes.
ServletRequestListener
A ServletRequestListener can be implemented
by the developer interested in being notified of requests coming in and out of
scope in a web component.
ServletResponse
Defines an object to assist a servlet in sending a
response to the client.
SingleThreadModel Deprecated. As of Java Servlet API 2.4, with no direct
replacement.
Q:4.Answer the following questions
1.Explain the components of JDBC.
JDBC has following components :
1. JDBC API
Out: This is used for writing content to the client (browser). It has several
methods which can be used for properly formatting output message to the
browser and for dealing with the buffer.
Request: The main purpose of request implicit object is to get the data on a
JSP page which has been entered by user on the previous JSP page. While
dealing with login and signup forms in JSP we often prompts user to fill in
those details, this object is then used to get those entered details on an
another JSP page (action page) for validation and other purposes.
Response: It is basically used for modfying or delaing with the response
which is being sent to the client(browser) after processing the request.
Session: It is most frequently used implicit object, which is used for storing
the users data to make it available on other JSP pages till the user session is
active.
Application: This is used for getting application-wide initialization
parameters and to maintain useful data across whole JSP application.
Exception: Exception implicit object is used in exception handling for
displaying the error messages. This object is only available to the JSP pages,
which has isErrorPage set to true.
Page: Page implicit object is a reference to the current Servlet instance
(Converted Servlet, generated during translation phase from a JSP page). We
can simply use this in place of it. Im not covering it in detail as it is rarely
used and not a useful implicit object while building a JSP application.
pageContext: It is used for accessing page, request, application and
session attributes.
Config: This is a Servlet configuration object and mainly used for accessing
getting configuration information such as servlet context, servlet name,
configuration parameters etc.
consistently throughout the swing component set. The view and controller
parts of the architecture are combined in the component.
String m = b1.getEmail();
if (m==null ||m.equals("") || m.indexOf("@") == -1) {
return mapping.findForward(FAILURE);}
else{
return mapping.finForward("SUCESS");
}}
(B) Serve as Data Carrier
Action serves as data carrier from request to the view, where data get stored
as JavaBean
properties. It allows accessing the data locally during the execution of
business logic.
Java bean code specifications to store the username and email from HTTP
request is :
private String username;
private String email;
public String getEmail() {
return email;}
public void setEmaiL(String email) {
this.email = email;}
public String getUsername() {
return username;}
public void setUsername(String username) {
this.username = username;}
What is HibernateTemplate class?
When Spring and Hibernate integration started, Spring ORM provided
two helper classes HibernateDaoSupport and HibernateTemplate. The
reason to use them was to get the Session from Hibernate and get the
benefit of Spring transaction management.
However from Hibernate 3.0.1, we can use SessionFactory
getCurrentSession() method to get the current session and use it to get
the spring transaction management benefits.
If you go through above examples, you will see how easy it is and
thats why we should not use these classes anymore.
One other benefit of HibernateTemplate was exception translation but
that can be achieved easily by using @Repository annotation with
service classes, shown in above spring mvc example.
This is a trick question to judge your knowledge and whether you are
aware of recent developments or not.
3.Explain the importance of mapping and show the creation of
mapping file in hibernate framework?...
Mapping File of hibernate application (hbm.xml)
Mapping file is the heart of hibernate application.
(1) The working of hibernate will state when persistent object i.e.
POJO has been created by the java application.
(2) Hibernate layer is divided into following different object which are use to
perform different operation.
(i) Configuration :
(i) This object is used to establish a connection with the database.
(ii) It contains following 2 files
(a) hibernate. properties _ it is use to give some additional information about
the hibernate layer.
Four
JScrollPane.VERTICAL_SCROLLBAR_NEVER
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
Corner Component
void setCorner(String cornerlocation, Compnent c):the components
can be placed in the corners of a JScrollPane Object. Valid Corner
Locations are:
JScrollPane.LOWER_LEFT_CORNER
JScrollPane.LOWER_RIGHT_CORNER
JScrollPane.UPPER_LEFT_CORNER
JScrollPane.UPPER_RIGHT_CORNER
Example:
import java.awt.BorderLayout;
import javax.swing.*;
public class ScrollList extends JFrame {
JScrollPane scrollpane;
public ScrollList() {
super("JScrollPane Demonstration");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
String categories[] = { "Household", "Office", "Extended Family",
"Company (US)", "Company (World)", "Team", "Will",
"Birthday Card List", "High School", "Country", "Continent",
"Planet" };
JList list = new JList(categories);
scrollpane = new JScrollPane(list);
getContentPane().add(scrollpane, BorderLayout.CENTER);}
public static void main(String args[]) {
ScrollList sl = new ScrollList();
sl.setVisible(true);}}
2.What are the different types of enterprise beans?Explain.
There are two main types of EJBs
1- Session Beans
2- Message Driven Beans
1- Session Beans
A session bean encapsulates business logic that can be invoked
programmatically by a client over local, remote, or web service client
views. A session bean is not persistent, means its data is not saved to
a database. EJB Session Beans has three types which are
Stateful Session Bean
A "lib" directory, designated for JAR files that are automatically added
to the Web application's classpath at runtime.
A "classes" directory designated for any classes needed by the
application that are not in a JAR file.
Any client classes for EJBs packaged in .jar files.
DefaultWebApp/WEB-INF/web.xml
The DefaultWebApp/WEB-INF/web.xml file is the Web application
deployment descriptor that configures the Web application.
DefaultWebApp/WEB-INF/weblogic.xml
The DefaultWebApp/WEB-INF/weblogic.xml file is the WebLogic-specific
deployment descriptor file that defines how named resources in the
web.xml file are mapped to resources residing elsewhere in WebLogic
Server. This file is also used to define JSP and HTTP session attributes.
DefaultWebApp/WEB-INF/classes
The DefaultWebApp/WEB-INF/classes directory contains server-side
classes such as HTTP servlets and utility classes.
DefaultWebApp/WEB-INF/lib
The DefaultWebApp/WEB-INF/lib directory contains JAR files used by
the Web application, including JSP tag libraries.
6.List and Explain different types of Enterprise beans?
EJB stands for Enterprise Java Beans. EJB is an essential part of a J2EE
platform. J2EE platform have component based architecture to provide multitiered, distributed and highly transactional features to enterprise level
applications.
EJB provides an architecture to develop and deploy component based
enterprise applications considering robustness, high scalability and high
performance. An EJB application can be deployed on any of the application
server compliant with J2EE 1.3 standard specification.
There are two main types of EJBs
1- Session Beans
2- Message Driven Beans
1- Session Beans
A session bean encapsulates business logic that can be invoked
programmatically by a client over local, remote, or web service client
views. A session bean is not persistent, means its data is not saved to
a database. EJB Session Beans has three types which are
Stateful Session Bean
In stateful session bean, the state of an object consists of the values of
its instance variables. In a stateful session bean, the instance variables
represent the state of a unique client / bean session.
Stateless Session Bean