Java Programming-II - CM4109
Java Programming-II - CM4109
A Laboratory Manual
For
Java Programming-II
(CM4109)
Semester –V (CM)
2. Problem analysis: Identify and analyze well-defined engineering problems using discipline
specific knowledge.
4. Engineering Tools, Experimentation and Testing: Apply modern engineering tools and
appropriate technique to demonstrate & practice experimental measurements.
7. Life-long learning: Develop the ability to analyze individual needs and engage in updating
in the context of technological changes.
2. PSO2: Ability to apply programming, multimedia, internet, web, and software engineering
principles to solve entry level problems in software domain.
Course Outcome:
a. Develop GUI applications using Abstract Windowing Toolkit (AWT) and event handling.
b. Create GUI applications using Swing.
c. Develop client/server applications using TCP/IP and UDP socket programming.
d. Implement Java programs using databases with Java Data Base Connectivity (JDBC) as interface.
e. Develop applications for Remote Method Invocation (RMI).
f. Implement Java programs using Servlets.
Guidelines to Teacher
1. There will be two sheets of blank pages after every practical for the student to report other
matters (if any), which is not mentioned in the printed practicals.
2. For difficult practical’s if required, teacher could provide the demonstration of the practical
emphasizing of the skills which the student should achieve.
3. Teachers should give opportunity to students for hands-on after the demonstration.
4. Assess the skill achievement of the students and Cos of each unit.
5. One or two questions ought to be added in each practical for different batches. For these
teachers can maintain various practical related question bank for each course.
6. For effective implementation and attainment of practical outcomes, teacher ought to ensure
that in the beginning itself of each practical, students must read through the complete write-
up of that practical sheet.
7. During practical, ensure that each student gets chance and takes active part in taking
observations/readings and performing practical’s.
8. Teacher ought to assess the performance of the students continuously according to the
guidelines given.
1. Students shall listen carefully the lecture given by teacher about importance of subject,
learning structure, course outcomes.
2. Students shall understand the purpose of experiment and its practical implementation.
3. Students shall write the answers of the questions during practical.
4. Student should feel free to discuss any difficulty faced during the conduct of practical.
Students shall develop web based and window-based applications as expected by the
industries.
5. Student shall attempt to develop related hands-on skills and gain confidence.
6. Students shall refer technical magazines; websites related to the scope of the subjects
and update their knowledge and skills.
7. Students shall develop self-learning techniques.
8. Students should develop habit to submit the write-ups on the scheduled dates and time.
Content Page
List of Practical’s and Progressive Assessment Sheet
Sr. Title of the practical Page Date of Date of Assessment Dated Remar
No No. performance submission Marks (50) sign. of ks (if
teacher any)
Practical No. 1
Tittle: Program to design a form using various controls and different Layout’s
manager.
I. Practical Significance:
Text Field, Text Area, Button, Checkbox, Radio Buttons (Check Box Group) List and
Choice Components are the AWT components. Used to design the GUI in java. A
component is object having representation that can be displayed on the screen to interact
with the user. A layout manager automatically arranges your controls within a window.
Components.
2. Problem analysis: Identify and analyze well-defined engineering problems using discipline
specific knowledge.
4. Engineering Tools, Experimentation and Testing: Apply modern engineering tools and
appropriate technique to demonstrate & practice experimental measurements.
Using applet window, design following AWT components using add() method of
components class. Following are some AWT components
Layout Types
• Flow Layout
• Grid Layout
• Border Layout
• Card Layout
• Gridbag Layout
The default layout for different containers such as Applet, Frame, Panel. Grid Layout is used to
make a bunch of components equal in size and displays them in the requested number of rows
and columns. One component is displayed in each rectangle. The list of Constructor for
GridLayout are:
1. GridLayout(): creates a grid layout with one column per component in a row.
2. GridLayout(int rows, int columns): creates a grid layout with the given rows and
columns but no gaps between the components.
3. GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the
given rows and columns along with given horizontal and vertical gaps if we give
setLayout(null) the default layout is disabled.then we have to use setBounds method
to layout the components.
Constructors of CardLayout class
1. CardLayout(): creates a card layout with zero horizontal and vertical gap.
2. CardLayout(int hgap, int vgap): creates a card layout with the given horizontal
and vertical gap.
XIV. Conclusions
2. Design an applet/application to create form using Text Field, Text Area, Button and
Label.
3. State the difference between List and Choice control
4. Write the use of getSelectedItem() and getSelectedIndex() for List.
5. List the names of BorderLayout regions.
6. Write the default horizontal and vertical gap in FlowLayout
7. Write the use of Insets in border layout.
8. Write a program code to generate the following output.
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
......................................................................................................................................................
.......................................................................................................................................................
XVII. Rubrics for evaluation (subject to modification as per the suggestions from
theHODs/Faculties)
Dated
Marks Obtained signature
of
Teacher
Practical No. 2
I. Practical Significance:
The Menu Bar lets the user to select different menu options from the list of available
menu items and perform operations on the selected menu item.
4. Engineering Tools, Experimentation and Testing: Apply modern engineering tools and
appropriate technique to demonstrate & practice experimental measurements.
To create a menu bar, first create an instance of Menu Bar. This class only defines the
default constructor. Next, create instances of Menu that will define the selections
displayed on the bar.
Menu( ) // creates an empty menu.
Menu(String optionName) // name of the menu selection
Menu(String optionName, boolean removable) // If removable is true, the menu
can be removed and allowed to float free. Otherwise, it will remain attached to
the menu bar.
XIV. Conclusions
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
.....................................................................................................................................................
XVII. Rubrics for evaluation (subject to modification as per the suggestions from
theHODs/Faculties)
Dated
Marks Obtained signature
of
Teacher
Practical No. 3
Title: Program to design a form using basic swing components and use of tabbed panes
and scroll panes in Swing.
I. Practical Significance:
The Swing Components are very useful to design interactive application and it provide
rich look and feel to the components. Swing components are light weight and platform
independent. Swing supplies additional controls such as TabbedPane, ScrollPane.
• Engineering Tools, Experimentation and Testing: Apply modern engineering tools and
appropriate technique to demonstrate & practice experimental measurements.
XIV. Conclusions
Figure
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
.................................................................................................................................................
XVII. Rubrics for evaluation (subject to modification as per the suggestions from
theHODs/Faculties)
Dated
Marks Obtained signature
of
Teacher
Practical No.4
I. Practical Significance:
Tree is used to represent the hierarchical view of the data. The tree control shows the
data in tree like structure such as root and its leaves. A table is a component that displays
rows and columns of data. JTable class in Java which extends JComponent. The cursor
can be dragged on column boundaries to resize column.
• Engineering Tools, Experimentation and Testing: Apply modern engineering tools and
appropriate technique to demonstrate & practice experimental measurements.
JTree(Hashtable ht) : The first form creates a tree in which each element of the hash
table ht is a child node.
TreeNode interface :It declares methods that obtain information about a tree node.
Constructors of JTable:
1. JTable() : Creates a table with empty cells
2. JTable(Object data[ ][ ], Object colHeads[ ])
Here, data is a two-dimensional array of the information to be presented, and
colHeads
is a one-dimensional array with the column headings.
XIV. Conclusions
XVII. Rubrics for evaluation (subject to modification as per the suggestions from
theHODs/Faculties)
Dated
Marks Obtained signature
of
Teacher
Practical No. 5
Title: Program to retrieve hostname using methods in InetAddress class, URL and URL
Connection class.
I. Practical Significance:
Java provides the socket programming approach for communication between the client
and server. A user can write the code for both client and server as well for UDP& TCP
datagram packets. By using java’s network communication feature we can create
interactive application to communicate within a network.
The Uniform resource locator in java allows the user to access the particular file or resource
which might be stored on any local or remote machine. The URL specifies the complete
path by which user can access file. It contains protocol address, port number and location
of the particular resource.
• Engineering Tools, Experimentation and Testing: Apply modern engineering tools and
appropriate technique to demonstrate & practice experimental measurements.
methods.
1. static InetAddress getLocalHost( ) throws UnknownHostException
2. static InetAddress getByName(String hostName) throws UnknownHostException
3. static InetAddress[ ] getAllByName(String hostName) throws unknownHostException
The URL provides easy to understand format to uniquely identify or address information
on the internet. URLs are ample; every browser uses them to identify information on the
Web. URL class provides a simple, concise API to access information across the Internet
using URLs.
URLConnection is a class which is used to access the attributes of remote resource. Once
we made a connection to the remote resource, we are ready to use URLConnection to
check the properties of remote object before actually transportingit locally.
The URL and URLConnection classes are good enough for simple programs that wantto
connect to HTTP servers to fetch content.
Methods:
1. public String getPath()
2. public String getAuthority()
3. public String getHost()
4. public String getFile()
Methods:
1. Object getContent()
2. String getContentEncoding()
3. int getContentLength()
4. String getContentType()
5. public URL getURL()
XIV. Conclusions
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
........................................................................................................................................................
..............................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
........................................................................................................................................................
..............................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
........................................................................................................................................................
..............................................................................................................................................
.......................................................................................................................................................
XVII. Rubrics for evaluation (subject to modification as per the suggestions from
theHODs/Faculties)
Dated
Marks Obtained signature
of
Teacher
Practical No. 6
Title: Program that demonstrates TCP/IP and UDP based communication between client and
server.
I. Practical Significance:
Java provides the socket programming approach for communication between the client and
server. A user can write the code for both client and server as well for UDP& TCP datagram
packets. By using java’s network communication feature we cancreate interactive application
to communicate within a network.
The User Datagram Protocol (UDP) is connectionless, and unreliable protocol by which the
user can send short messages called datagrams. The java provides DatagramSocket and
DatagramPacket class to implement the concept of userdatagram protocol. By using these
classes we can transfer the data asynchronous manner.
• Engineering Tools, Experimentation and Testing: Apply modern engineering tools and
appropriate technique to demonstrate & practice experimental measurements.
The java.net.Socket class is used to communicate between client and server.The client can
obtain object by creating its instance whereas the server obtains a Socket objectfrom the
return value of the accept() method.
The Java DatagramSocket class is used to send and receive the datagrams it is
connection less socket. That is there is no guarantee of message delivery.
It has following constructors
1. DatagramSocket()
2. DatagramSocket(int port)
3. DatagramSocket(int port, InetAddress address)
The Java DatagramPacket is class that can be used to send the packets. If you send multiple
packet, it may arrive in any order. Additionally, packet delivery is not guaranteed.
It has following constructors
1. DatagramPacket(byte[] barr, int length)
2. DatagramPacket(byte[] barr, int length, InetAddress address, int port)
XIV. Conclusions
XVII. Rubrics for evaluation (subject to modification as per the suggestions from
theHODs/Faculties)
Dated
Marks Obtained signature
of
Teacher
Practical No. 7: Write a program to insert and retrieve data from database
using JDBC. Program to make connectivity with database using
JDBC API to send queries
I. Practical Significance:
ODBC isn't appropriate for direct use from the Java programming language because
it uses a C interface. The JDBC API was modeled after ODBC, but, because JDBC
is a Java API, it offers a natural Java interface for working with SQL. JDBC is
needed to provide a "pure Java" solution for application development.
Some of the current JDK add more features to JDBC that is embedded SQL which
makes direct mapping of relational databases to java classes. Embedded SQL
enables mixing of java into a SQL statement These statements are translated into
JDBC calls using SQL processor. In this type of direct mapping, each row of the
table becomes an instance of class and each column value corresponds to an
attribute of that instance.
JDBC DRIVERS:-
1. JDBC-ODBC Bridge: -.
2. Native-API: - partly java Driver: -
3. JDBC-Network pure java driver:
4. Native_protocol (100%) pure Java drivers: -
ResultSet Interface
The SQL statements that read data from a database query, return the data in a result
set. The SELECT statement is the standard way to select rows from a database and
view them in a result set. The java.sql.ResultSet interface represents the result set of a
database query.A ResultSet object maintains a cursor that points to the current row in
the result set.The term "result set" refers to the row and column data contained in a
ResultSetobject.The methods of the ResultSet interface can be broken down into three
categories −
Navigational methods:
Get methods:
Update methods
JDBC provides the following connection methods to create statements
Department of Computer Engineering G.P.Pune
Java Programming -II (CM4109)
Sr. Name of
Broad Specification Quantity Remarks (If
No. Resource
any)
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...........................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
IX. References/ Suggestions for Further Reading
1. https://www.javatpoint.com
2. https://www.tutorialspoint.com/java
3. The complete reference Java 2 by Herbert schildt
4. ftp://ftp.icm.edu.pl/packages/javasoft-docs/jdk1.1/jdbc.pdf
X. Assessment Scheme
I. Practical Significance:
ODBC isn't appropriate for direct use from the Java programming language because
it uses a C interface. The JDBC API was modeled after ODBC, but, because JDBC
is a Java API, it offers a natural Java interface for working with SQL. JDBC is
needed to provide a "pure Java" solution for application development.
Skeleton
The skeleton is an object, acts as a gateway for the server side object. All the incoming
requests are routed through it. When the skeleton receives the incoming request, it does the
following tasks:
Department of Computer Engineering G.P.Pune
Java Programming -II (CM4109)
Applications of RMI
1. The application need to locate the remote method
2. It need to provide the communication with the remote objects, and
3. The application need to load the class definitions for the objects.
Java RMI Example
The is given the 6 steps to write the RMI program.
1. Create the remote interface
2. Provide the implementation of the remote interface
3. Compile the implementation class and create the stub and skeleton objects using the rmic
tool
4. Start the registry service by rmiregistry tool
5. Create and start the remote application
6. Create and start the client application
RMI Example
In this example, we have followed all the 6 steps to create and run the rmi application. The client
application need only two files, remote interface and client application. In the rmi application, both
client and server interacts with the remote interface. The client application invokes methods on the
proxy object, RMI sends the request to the remote JVM. The return value is sent back to the proxy
object and then to the client application.
1) create the remote interfaceFor creating the remote interface, extend the Remote interface
and declare the RemoteException with all the methods of the remote interface. Here, we are
creating a remote interface that extends the Remote interface. There is only one method
named add() and it declares RemoteException.
import java.rmi.*;
public interface Adder extends Remote{
public int add(int x,int y)throws RemoteException;
}
2) Provide the implementation of the remote interface
o Now provide the implementation of the remote interface. For providing the
implementation of the Remote interface, we need to Either extend the
UnicastRemoteObject class, or use the exportObject() method of the
UnicastRemoteObject class .In case, you extend the UnicastRemoteObject class, you must
define a constructor that declares RemoteException.
import java.rmi.*;
import java.rmi.server.*;
public class AdderRemote extends UnicastRemoteObject implements Adder
{
AdderRemote()throws RemoteException{
super();
}
public int add(int x,int y){return x+y;}
}
3) create the stub and skeleton objects using the rmic tool.
Next step is to create stub and skeleton objects using the rmi compiler. The rmic tool invokes
the RMI compiler and creates stub and skeleton objects
rmic AdderRemote
4)Start the registry service by the rmiregistry tool
Now start the registry service by using the rmiregistry tool. If you don't specify the port
number, it uses a default port number. In this example, we are using the port number 5000.
rmiregistry 5000
5) Create and run the server application
Now rmi services need to be hosted in a server process. The Naming class provides methods
to get and store the remote object. The Naming class provides 5 methods.
Sr. Name of
Broad Specification Quantity Remarks (If
No. Resource
any)
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...........................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
IX. References/ Suggestions for Further Reading
1. https://www.javatpoint.com
2. https://www.tutorialspoint.com/java
3. The complete reference Java 2 by Herbert schildt
4. ftp://ftp.icm.edu.pl/packages/javasoft-docs/jdk1.1/jdbc.pdf
X. Assessment Scheme
Sr.No. Performance Indicators Weightage in
%
a. Correctness of Program 40
b. Debugging ability 20
c. Quality of input and output displayed 10
d. Preparing assignments 20
e. Submit assignment on time. 10
Total 100
List of student Team Members
1. ……………………...
2. ……………………...
3……………………….
4……………………….
Rubrics for evaluation(subject to modification as per the suggestions from the
HODs/Faculties)
I. Practical Significance:
Servlets are the Java programs that run on the Java-enabled web server or
application server. They are used to handle the request obtained from the web
server, process the request, produce the response, and then send response back to
the web server.
Sr. Name of
Broad Specification Quantity Remarks (If
No. Resource
any)
.............................................................................................................................................................
.............................................................................................................................................................
.............................................................................................................................................................
.............................................................................................................................................................
.............................................................................................................................................................
.............................................................................................................................................................
VII. References/ Suggestions for Further Reading
1. https://www.javatpoint.com
2. https://www.tutorialspoint.com/java
3. The complete reference Java 2 by Herbert Schildt
4. https://tomcat.apache.org/download-80.cgi
XIII. Assessment Scheme
Sr.No. Performance Indicators Weightage in
%
a. Correctness of Program 40
b. Debugging ability 20
c. Quality of input and output displayed 10
d. Preparing assignments 20
e. Submit assignment on time. 10
Total 100
List of student Team Members
1. ……………………...
2. ……………………...
3……………………….
4……………………….
Practical No. 10: Program to send username and password using HTML forms
and authenticate the user using Servlet
I. Practical Significance:
There is a possibility of developing „n‟ types of servlets, like httpservlet, ftpservlet,
smtp servlet etc. for all these protocol specific servlet classes GenericServlet is the
common super class containing common properties and logics. So, GenericServlet
is not a separate type of servlet.As of now Servlet API is giving only one subclass to
GenericServlet i.e HttpServlet class because all web servers are designed based on
the protocol http.
Sr. Name of
Broad Specification Quantity Remarks (If any)
No. Resource
1. Write a Program to send the username to server and server will send the
length ofusername to client.
2. Write the output of following code considering below HTML is front end
andservlet as back end
loginpage.html
<html>
<body>
<form action="
http://localhost:8080/examples/servlets/servlet/AthonticationServlet”
method=”POST”>
User Name:<input type="text" name="username"><br>
Password:<input type="password" name="password" ><br>
<input type="submit">
</form>
</body>
</html>
//AthonticationServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;import
javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AthonticationServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
Department of Computer Engineering G.P.Pune
Java Programming -II (CM4109)
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String pass="abhishek12345";String
username,password;
username=request.getParameter("username");
password=request.getParameter("password");
if(username.equals(uname) && password.equals(pass))
{
out.println("Login Successfull");
}
else
{
out.println("Login Unsuccessfull");
}
}
}
.............................................................................................................................................................
.............................................................................................................................................................
.............................................................................................................................................................
.............................................................................................................................................................
.............................................................................................................................................................
.............................................................................................................................................................
.............................................................................................................................................................
.............................................................................................................................................................
.............................................................................................................................................................
1. …………………………………..
2. …………………………………..
3. …………………………………..
Rubrics for evaluation (subject to modification as per the suggestions from the
HODs/Faculties)
Dated signature
Marks
of
Obtained
Teacher
Process
Product Related(15) Total(50)
Related(35)
I. Practical Significance:
container creates a session id for each user. The container uses this id to identify the
particular user. An object of HttpSession can be used to perform two tasks:
1. bind objects
2. view and manipulate information about a session, such as the session
identifier,creation time, and last accessed time.
URL Rewriting
You can append some extra data on the end of each URL that identifies the session,
and the server can associate that session identifier with data it has stored about that
session.
Types of Cookie
1. Non-persistent cookie
2. Persistent cookie
Non-persistent cookie
It is valid for single session only. It is removed each time when user closes the
browser.
Persistent cookie
It is valid for multiple session. It is not removed each time when user closes the
browser. It is removed only if user logout or sign-out.
javax.servlet.http.Cookie class provides the functionality of using cookies. It
provides a lot of useful methods for cookies.
Program
1. Write a Program to Session id and Session time in Browser Window.
2. Write the output of following code considering below HTML is front end and
servlet as back end
<html>
<body>
<form action="
http://localhost:8080/examples/servlet/SessionServlet">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="go"/>
</form>
</body>
</html>
//SessionServlet.java
import java.io.*;
import
javax.servlet.*;
import javax.servlet.http.*;
Sr.
Name of Resource Broad Specification Quantity Remarks (If any)
No.
Web Server Apache Tomcat 8.0
1
Software with JRE.
2
X. Assessment Scheme
1. …………………………………..
2. …………………………………..
3. …………………………………..
Dated
Marks Obtained
signature
of Teacher
Process
Product Related(15) Total(50)
Related(35)