Vous êtes sur la page 1sur 48

ADVANCED JAVA PROGRAMMING LAB MANUAL (R16)

1. Write a program to prompt the user for a hostname and then looks up the IP address
for the hostname and displays the results.

/* 1. Write a program to prompt the user for a hostname and then looks up the IP address
for thehostname and displays the results. */

import java.net.UnknownHostException;
import java.net.InetAddress;

class IPTest
{
public static void main(String args[]) throws UnknownHostException
{
InetAddress addr = InetAddress.getLocalHost();
//Getting IPAddress of localhost - getHostAddress return IP Address
// in textual format
String ipAddress = addr.getHostAddress();
System.out.println("IP address of localhost from Java Program: " +
ipAddress);

//Hostname
String hostname = addr.getHostName();
System.out.println("Name of hostname : " + hostname);
}
}

AJ Lab, Department of IT, VNITSW [DD] Page 1


2. Write a program to read the webpage from a website and display the contents of the
webpage.

import java.io.BufferedReader;
import java.io.*;
import java.net.*;

public class NewClass {


public static void main(String[] args) {
URL url;
InputStream is = null;
BufferedReader br;
String line;
try {
url = new
URL("https://www.tutorialspoint.com/javaexamples/net_singleuser.htm");
is = url.openStream(); // throws an IOException
br = new BufferedReader(new InputStreamReader(is));

while ((line = br.readLine()) != null) {


System.out.println(line);
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
finally {
try {
if (is != null) is.close();
} catch (IOException ioe) {}
}
}
}
Displays output as the contents in https:// www. tutorialspoint.com /javaexamples
/net_singleuser.htm

AJ Lab, Department of IT, VNITSW [DD] Page 2


3. Write programs for TCP server and Client interaction as per given below.
i). A program to create TCP server to send a message to client.
ii). A program to create TCP client to receive the message sent by the server.

Filename:3server.java
import java.io.*;
import java.net.*;
class Server
{
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader
(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream

(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}

Filename: 3client.java
import java.io.*;
import java.net.*;
class Client
{
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader( new
InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 6789);
DataOutputStream outToServer = new
DataOutputStream(clientSocket.getOutputStream());

AJ Lab, Department of IT, VNITSW [DD] Page 3


BufferedReader inFromServer = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}

AJ Lab, Department of IT, VNITSW [DD] Page 4


4. Write programs for Datagram server and Client interaction as per given below.
i). A program to create Datagram server to send a message to client.
ii). A program to create Datagram client to receive the message sent by the server

Filename:4server.java
import java.net.*;
import java.io.*;
class dataserver
{
public static DatagramSocket ds;
public static int clientport=789,serverport=790;
public static void main(String a[]) throws Exception
{
byte buffer[]=new byte[1024];
ds=new DatagramSocket(serverport);
BufferedReader dis=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Type a message to Client ... ");
InetAddress is=InetAddress.getByName("localhost");
while(true)
{
String str=dis.readLine();
if((str==null || str.equals("end")))
break;
buffer=str.getBytes();
ds.send(new
DatagramPacket(buffer,str.length(),is,clientport));
}
}
}
Filename: 4client.java
import java.net.*;
import java.io.*;
class dataclient
{
public static DatagramSocket ds;
public static byte buffer[]=new byte[1024];
public static int clientport=789,serverport=790;
public static void main(String a[])throws Exception
{
ds=new DatagramSocket(clientport);
System.out.println("Waiting for Server's Message...");
System.out.println("Use ...Ctrl + c... to Exit");
while(true)

AJ Lab, Department of IT, VNITSW [DD] Page 5


{
DatagramPacket p=new DatagramPacket(buffer,buffer.length);
ds.receive(p);
String psx=new String(p.getData(),0,p.getLength());
System.out.println(psx);
}
}
}

AJ Lab, Department of IT, VNITSW [DD] Page 6


5. Write a program by using JDBC to execute a SQL query for a database and display the
results.

Create Project:
i. Open Netbeans
ii. Goto File à click on New Project
iii. Goto Categories à choose Java, in Projects à choose Java Application
iv. Click on next
v. Project Name à write title(MYProject), and choose location
vi. Click on Finish
Set environment:
i. Right Click on Project Title(MYProject)
ii. In categories à click on Libraries
iii. In compile à click on Add JAR/Folder

C:/oraclexe/app/oracle/product/10.2.0/server/jdbc/lib/ojdbc14.jar
iv. Click open
v. Click ok
Database connectivity:
i. Goto window à click on services
ii. In left hand side, choose services
iii. Right click on Databases
iv. Click on New Connection
Choose addDriver
click on add button

C:/oraclexe/app/oracle/product/10.2.0/server/jdbc/lib/ojdbc14.jar

v. Name : Choose Oracle


vi. Database URL: write <Host> as the following
C:/oraclexe/app/oracle/product/10.2.0/server/NETWORK/ADMIN/tnsnames.ora
Open with Notepad
Find HOST and its values(name)

<HOST> = vnitsw_lab2-PC
<PORT> = 1521
<SID> = XE
vii. Write username and password i.e. system and nirula.
viii. Click on ok
ix. Connected with database

Check connection:
i. Goto databases
ii. Click on Oracle

AJ Lab, Department of IT, VNITSW [DD] Page 7


iii. Finds all tables and others
iv. Right click on tables
v. Click on create table
vi. Give table name, and columns with size
vii. Right click on the tablename
viii. View the details
ix. Modify the query to insert values
x. Then compile and execute the program

Cycle5.java
import java.lang.*;
import java.sql.*;
import java.util.*;
Public class Main
{
public static void main(String args[])throws Exception
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@vnitsw_lab1-
PC:1521:XE","system","nirula");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from aatable");
System.out.println("Hello");
while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
}

con.close();
}
catch(Exception e)
{System.out.println(e);}
}
}

AJ Lab, Department of IT, VNITSW [DD] Page 8


6. Write a program by using JDBC to execute an update query without using Prepared
Statement and display the results.

Cycle6.java

package cycle6;

import java.sql.*;
import java.util.*;

public class Main


{
public static void main(String args[])throws Exception
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","syste
m","nirula");
Statement st=con.createStatement();
st.executeQuery("UPDATE aaatle SET name='vvnirula',
address='gggnt' WHERE num=101");
System.out.println("one record updated.....");

con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

AJ Lab, Department of IT, VNITSW [DD] Page 9


AJ Lab, Department of IT, VNITSW [DD] Page 10
7. Write a program by using JDBC to execute an update query by using Prepared
Statement and display the results.

package cycle7;

import java.sql.*;
import java.util.*;

public class Main


{
public static void main(String args[])throws Exception
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","syste
m","nirula");
Statement st=con.createStatement();

String sql = "UPDATE aaatle SET name=?, address=? WHERE


num=?";
PreparedStatement statement = con.prepareStatement(sql);
statement.setString(1, "nirulaa2");
statement.setString(2, "Gunturr2");
statement.setInt(3, 1002);

int rowsInserted = statement.executeUpdate();


if (rowsInserted > 0) {
System.out.println("one RECORD was updated successfully!");
}

con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

AJ Lab, Department of IT, VNITSW [DD] Page 11


AJ Lab, Department of IT, VNITSW [DD] Page 12
8. Write a program to execute a stored procedure in the database by using Callable
Statement and display the results.

create or replace procedure aaatleupdate(


u_num in aaatle.num%type,
u_name in aaatle.name%type,
u_address in aaatle.address%type)
is
begin
update aaatle set name=u_name, address=u_address where num=u_num;
commit;
end;
/

package cycle8;

import java.sql.*;
import java.util.*;

public class Main


{
public static void main(String args[])throws Exception
{
Scanner input= new Scanner(System.in);
System.out.println("Enter Number :");
int num=Integer.parseInt(input.nextLine());
System.out.println("Enter Name :");
String name=input.nextLine();

AJ Lab, Department of IT, VNITSW [DD] Page 13


System.out.println("Enter Address :");
String address=input.nextLine();
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","syste
m","nirula");
CallableStatement stmt=con.prepareCall("{call
aaatleupdate(?,?,?)}");
stmt.setInt(1,num);
stmt.setString(2,name);
stmt.setString(3,address);
stmt.execute();

System.out.println("one record updated.....");

con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

AJ Lab, Department of IT, VNITSW [DD] Page 14


AJ Lab, Department of IT, VNITSW [DD] Page 15
9. Write a program to display a greeting message in the browser by using Http Servlet.

Steps:

1. Goto File  New Project


2. Choose project as Java Web  Web Application
3. Click on next
4. Write Project Name as Cycle9
5. Click next and Finish
6. Goto Projects  Cycle9  Source Packages
7. Right click on <default packages>  New  Servlet
8. Write class name as Servlet9 and Click on next and Finish
9. Click on Servlet9.java
10. Write the following code in try block
out.println("<h1>Welcome to Servlet Technology"+" "+"</h1>");
11. Right click on webpages  HTML
12. Write HTML file name as index and click Finish
13. Goto webpages  open index.html
14. Write the following code with in body
<h4> Click here to go to <a href=”Servlet9”>Cycle9 Servlet Page</a> </h4>
15. Goto webpages  WEB-INF
16. Click on web.xml
17. Update the following :
<welcome-file>index.html</welcome-file>
18. Right click on Cycle9  Run

Servlet9.java
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Servlet9 extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {

out.println("<h1>Welcome to Servlet Technology"+" "+"</h1>");

} finally {
out.close();
}
}
}

AJ Lab, Department of IT, VNITSW [DD] Page 16


Web.xml

<?xml version="1.0" encoding="UTF-8"?>


<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Servlet9</servlet-name>
<servlet-class>Servlet9</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet9</servlet-name>
<url-pattern>/Servlet9</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

Index.html

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h4> Click here to go to <a href="Servlet9">Cycle9 Servlet Page</a></h4>
</body>
</html>

AJ Lab, Department of IT, VNITSW [DD] Page 17


Output:

AJ Lab, Department of IT, VNITSW [DD] Page 18


10. Write a program to receive two numbers from a HTML form and display their sum in
the browser by using Http Servlet.

Servlet10.java

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Servlet9 extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {

int sum;
int x,y;
String a = request.getParameter("num1");
String b = request.getParameter("num2");
x = Integer.parseInt(a);
y = Integer.parseInt(b);
sum = x + y;
out.println("The sum is" + sum);

} finally {
out.close();
}
}
}

Web.xml

<?xml version="1.0" encoding="UTF-8"?>


<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Servlet10</servlet-name>
<servlet-class>Servlet10</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet10</servlet-name>
<url-pattern>/Servlet10</url-pattern>
</servlet-mapping>

AJ Lab, Department of IT, VNITSW [DD] Page 19


<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

Index.html

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="Servlet10">
Number1:<input type="text" name="num1" /><br/>
Number2:<input type="text" name="num2" /><br/>
<input type="submit" value="button" />
</form>
</body>
</html>

Output:

AJ Lab, Department of IT, VNITSW [DD] Page 20


AJ Lab, Department of IT, VNITSW [DD] Page 21
11. Write a program to display a list of five websites in a HTML form and visit to the
selected website by using Response redirection.

Servlet11.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet11 extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

try {
} finally {

}
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name=request.getParameter("website");
response.sendRedirect("http://"+name);

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
}

AJ Lab, Department of IT, VNITSW [DD] Page 22


Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Servlet11</servlet-name>
<servlet-class>Servlet11</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet11</servlet-name>
<url-pattern>/Servlet11</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Index.html
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form name="Form1" method="get" action="Servlet11">
<B>Select a website:</B>
<select name="website" size="1">
<option value="www.google.com">google</option>
<option value="www.yahoo.com">yahoo</option>
<option value="www.bing.com">bing</option>
<option value="www.ask.com">ask</option>
<option value="www.aol.com">aol</option>
</select>
<br><br>
<input type=submit value="Submit">
</form>
</body>
</html>

AJ Lab, Department of IT, VNITSW [DD] Page 23


Output:

AJ Lab, Department of IT, VNITSW [DD] Page 24


AJ Lab, Department of IT, VNITSW [DD] Page 25
12. Write a program to store the user information into Cookies. Write another program to
display the above stored information by retrieving from Cookies.

Index.html
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="Servlet12a">
User Name:<input type="text" name="userName"/><br/>
Password:<input type="password" name="userPassword"/><br/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
Servlet12a.java
import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

protected void processRequest(HttpServletRequest request,


HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
response.setContentType("text/html");
PrintWriter pwriter = response.getWriter();

String name = request.getParameter("userName");


String password = request.getParameter("userPassword");
pwriter.print("Hello "+name);
pwriter.print("Your Password is: "+password);

//Creating two cookies


Cookie c1=new Cookie("userName",name);
Cookie c2=new Cookie("userPassword",password);

//Adding the cookies to response header


response.addCookie(c1);

AJ Lab, Department of IT, VNITSW [DD] Page 26


response.addCookie(c2);
pwriter.print("<br><a href='Servlet12b'>View Details</a>");
pwriter.close();
} finally {
out.close();
}
}

}
Servlet12b.java

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Servlet12b extends HttpServlet {

protected void processRequest(HttpServletRequest request,


HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
response.setContentType("text/html");
PrintWriter pwriter = response.getWriter();

//Reading cookies
Cookie c[]=request.getCookies();
//Displaying User name value from cookie
pwriter.print("Name: "+c[0].getValue());
//Displaying user password value from cookie
pwriter.print("Password: "+c[1].getValue());

pwriter.close();
} finally {
out.close();
}
}
}

AJ Lab, Department of IT, VNITSW [DD] Page 27


Web.xml

<?xml version="1.0" encoding="UTF-8"?>


<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Servlet12a</servlet-name>
<servlet-class>Servlet12a</servlet-class>
</servlet>
<servlet>
<servlet-name>Servlet12b</servlet-name>
<servlet-class>Servlet12b</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet12a</servlet-name>
<url-pattern>/Servlet12a</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet12b</servlet-name>
<url-pattern>/Servlet12b</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

AJ Lab, Department of IT, VNITSW [DD] Page 28


Output:

AJ Lab, Department of IT, VNITSW [DD] Page 29


AJ Lab, Department of IT, VNITSW [DD] Page 30
13. Write a program in Java Beans to add a Button to the Bean and display the number of
times the button has been clicked.

Installation:

1. Goto browser
2. Type bdk dowonload
3. Click on first link and download

4. Extract the zip file into C: drive

Creation:

1. Goto C:\Beans\demo\sunw\demo
2. Create a Folder Name Cycle13
3. Goto Cycle13
4. Create a file Cycle13.java

package sunw.demo.Cycle13;
import java.io.*;

AJ Lab, Department of IT, VNITSW [DD] Page 31


import java.awt.*;

public class Cycle13 extends Canvas


{
public int count;
public Cycle13()
{
count=0;
}
public void paint(Graphics g)
{
int c=count++;
String msg="COUNT = "+c;
g.drawString(msg,10,10);
}
}

5. Goto start  run  cmd


6. In cmd, Goto C:\Beans\demo\sunw\demo\Cycle13

7. Goto C:\Beans\demo
8. Create a manifest file with name Cycle13.mft

AJ Lab, Department of IT, VNITSW [DD] Page 32


9. Goto start  run  cmd
10. In cmd, Goto C:\Beans\demo
11. Create jar file with name Cycle13.jar in C:\Beans\jars

12. Now Cycle13.jar created in C:\Beans\jar


13. Goto MyComputer  C:\Beans\beanbox
14. Here we will find two kinds run files
15. Click on run(i.e. Windows Batch File)

AJ Lab, Department of IT, VNITSW [DD] Page 33


16. In Tool Box(i.e. in left hand side ) we will find Cycle13
17. Drag Cycle13 from Tool Box to Bean Box

18. Drag OurButton from Tool Box to Bean Box

AJ Lab, Department of IT, VNITSW [DD] Page 34


19. Click on press(button)
20. in Bean Box, EditEvents mouse mouseClicked

21. Drag on to count = 0 (Cycle13 in Bean Box)

AJ Lab, Department of IT, VNITSW [DD] Page 35


22. Then it shows a window , choose a target method as repaint
23. Click OK

24. Wait for some time

AJ Lab, Department of IT, VNITSW [DD] Page 36


25. Initially count =0

26. Click on press(button) and observe the count value(i.e.1)

27. Click on press(button) and observe the count value(i.e.2)

AJ Lab, Department of IT, VNITSW [DD] Page 37


28. Click on press(button) and observe the count value(continuously)

:
:
:
:
AJ Lab, Department of IT, VNITSW [DD] Page 38
:
29. Click on press(button) and observe the count value

AJ Lab, Department of IT, VNITSW [DD] Page 39


14. Write a program for Java Bean with Simple property by using SimpleBeanInfo class.

Creation:

1. Goto C:\Beans\demo\sunw\demo
2. Create a Folder Name Cycle14
3. Goto Cycle14
4. Create a file Cycle14.java

package sunw.demo.Cycle14;
import java.awt.*;
public class Cycle14 extends Canvas
{
private int depth,height,width;

public Cycle14()
{
setSize(120,30);
height=0;
depth=0;
width=0;
}

public int getDepth()


{
return depth;
}

public void setDepth(int depth)


{
this.depth=depth;
}

public int getHeight()


{
return height;
}
public void setHeight(int height)
{
this.height=height;
}
public int getWidth()
{

AJ Lab, Department of IT, VNITSW [DD] Page 40


return width;
}
public void setWidth(int width)
{
this.width=width;
}
public void paint(Graphics g)
{
double volume=(height*depth*width);
String msg="Volume="+volume;
g.drawString(msg,10,10);
}
}

5. Create a file Cycle14BeanInfo.java


package sunw.demo.Cycle14;
import java.beans.*;
public class Cycle14BeanInfo extends SimpleBeanInfo
{
public PropertyDescriptor[] getPropertyDescriptors()
{
try
{
PropertyDescriptor height = new PropertyDescriptor("height",Cycle14.class);
PropertyDescriptor width = new PropertyDescriptor("width",Cycle14.class);
PropertyDescriptor depth = new PropertyDescriptor("depth",Cycle14.class);
PropertyDescriptor pd[] = {height,width,depth};
return pd;
}
catch(Exception e)
{}
return null;
}
}

6. Goto start  run  cmd


7. In cmd, Goto C:\Beans\demo\sunw\demo\Cycle14

AJ Lab, Department of IT, VNITSW [DD] Page 41


8. Goto C:\Beans\demo
9. Create a manifest file with name Cycle14.mft

10. Goto start  run  cmd


11. In cmd, Goto C:\Beans\demo
12. Create jar file with name Cycle14.jar in C:\Beans\jars

13. Now Cycle14.jar created in C:\Beans\jar

AJ Lab, Department of IT, VNITSW [DD] Page 42


14. Goto MyComputer  C:\Beans\beanbox
15. Here we will find two kinds run files
16. Click on run(i.e. Windows Batch File)

17. In Tool Box(i.e. in left hand side ) we will find Cycle14


18. Drag Cycle14 from Tool Box to Bean Box – we will see Properties –
Cycle14(Right Hand Side)

AJ Lab, Department of IT, VNITSW [DD] Page 43


19. Update depth, height, width and check volume in bean box

15. Write a program for Java Bean with Indexed Property by using SimpleBeanInfo class.

Creation:

1. Goto C:\Beans\demo\sunw\demo
2. Create a Folder Name Cycle15
3. Goto Cycle15
4. Create a file Cycle15.java

package sunw.demo.Cycle15;
import java.awt.*;
import java.io.*;
public class Cycle15
{
private double data[];

public double getData(int index)


{
return data[index];
}

public void setData(int index, double value)

AJ Lab, Department of IT, VNITSW [DD] Page 44


{
data[index] = value;
}
public double[] getData( )
{
return data;
}
public void setData(double[] values)
{
data = new double[values.length];
System.arraycopy(values, 0, data, 0, values.length);
}
}

5. Create a file Cycle15PieCBeanInfo.java


package sunw.demo.Cycle15;
import java.beans.*;
public class Cycle15PieCBeanInfo extends SimpleBeanInfo
{
public PropertyDescriptor[] getPropertyDescriptors()
{
try
{
PropertyDescriptor data= new PropertyDescriptor("data",Cycle15.class);
PropertyDescriptor p[] = {data};
return p;
}
catch(Exception e)
{}

return null;
}
}

6. Goto start  run  cmd


7. In cmd, Goto C:\Beans\demo\sunw\demo\Cycle15

AJ Lab, Department of IT, VNITSW [DD] Page 45


8. Goto C:\Beans\demo
9. Create a manifest file with name Cycle15.mft

10. Goto start  run  cmd


11. In cmd, Goto C:\Beans\demo
12. Create jar file with name Cycle15.jar in C:\Beans\jars

13. Now Cycle15.jar created in C:\Beans\jar

AJ Lab, Department of IT, VNITSW [DD] Page 46


14. Goto MyComputer  C:\Beans\beanbox
15. Here we will find two kinds run files
16. Click on run(i.e. Windows Batch File)

17. In Tool Box(i.e. in left hand side ) we will find Cycle15


18. Drag Cycle15 from Tool Box to Bean Box – we will see “Properties –
Cycle15”(Right Hand Side)

AJ Lab, Department of IT, VNITSW [DD] Page 47


16. Write a program to develop a Enterprise Java Bean of "Session Bean" type.

AJ Lab, Department of IT, VNITSW [DD] Page 48

Vous aimerez peut-être aussi