Rapid Application Development (CS3011)-Lab Manual INDEX Exp. No. Experiment Pag e no 1. Write a Java application that implements following calculator using Applet. 4-8 2. Write a Java application that implements following calculator using Frame. 9- 13 3. Write a Server and Client program that implements Chat application using TCP sockets. 14- 20- 1 First Number: Second Number: Result: ADD SUBTRAC MULTIPL DIVIDE RESET First Number: Second Number: Result: ADD SUBTRAC MULTIPL DIVIDE RESET
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Rapid Application Development (CS3011)-Lab Manual
INDEX
Exp. No.
Experiment Page no
1. Write a Java application that implements following calculator using Applet.
4-8
2. Write a Java application that implements following calculator using Frame.
9-13
3. Write a Server and Client program that implements Chat application using TCP sockets.
14-20-
4. Create a table employee with following fields –Emp_codeEmp_nameAddressPhoneDesignationDepartmentBasic SalaryWrite JDBC program that sends the query to select all the fields of
21-22
1
First Number:
Second Number:
Result:
ADD SUBTRACT MULTIPLY
DIVIDE RESET
First Number:
Second Number:
Result:
ADD SUBTRACT MULTIPLY
DIVIDE RESET
Rapid Application Development (CS3011)-Lab Manual
employee table to MS Access RDBMS and display all records of the given table.
5. Write a menu driven program to implement following application as per the user’s choice –a. To insert records in the table b. To modify an existing recordc. To delete an existing recordd. To view all recordse. Exit
23-27
6 Write a JDBC program that inserts five records in employee table using PreparedStatement object.
28-29
7 Write a servlet program that displays “Hello World” in a browser window
30
8 Write a servlet program that displays following list in the browse windowSystem SoftwareSystem software, more commonly known as Operating system, is any computer software that provides the infrastructure over which programs can operate, ie it manages and controls computer hardware so that application software can perform.
Operating Systems1. MS DOS2. Windows3. UNIX4. Linux
Translators1. Assembler2. Interpreter3. Compiler
Application Software Application Software is any tool that functions and is operated by
the means of a computer, with the purpose of supporting or improving the software user’s work.
Word ProcessorsI. Word Star
II. MS Word Spread Sheets
I. Lotus 1-2-3II. MS Excel
31-32
9 Create a servlet program that reads the data of employee table from the access database and displays the records in an HTML table.
10 Create a login form with two text boxes login name and password and a submit button.Create a servlet program “Verify” that reads data submitted by the login form and create a cookie “uid” and assign value of the login name to it. Redirect the response to another servlet program “NewServlet”. Create “NewServlet” program that reads the cookie “uid” created by the “Verify” and displays following message –
Welcome! Mr. Login name. . . .
35-38
11 Write a JSP program to display “Welcome to Chitkara University” in the Browser window.
39
12 Write a jsp to calculate the square of number.\ 40
13 Create an HTML form that accepts user name and password. Create a JSP page that reads the user name and password submitted by form and checks the validity of user from the database. Based upon the validity of the user, display an appropriate message on new JSP page
41-44
14 Create an RMI application to convert given temperature from Celsius to Fahrenheit.
45-47
3
Rapid Application Development (CS3011)-Lab Manual
Experiment – 1Write a Java application that implements following calculator using Applet.
SOLUTION:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code=Demo1.class height=500 width=500>
</applet>*/
public class Demo1 extends Applet implements ActionListener
{
Public void init()
{
Label l1,l2,l3;
TextField t1,t2,t3;
4
First Number:
Second Number:
Result:
ADD SUBTRACT MULTIPLY
DIVIDE RESET
Rapid Application Development (CS3011)-Lab Manual
Button b1,b2,b3,b4,b5;
Panel p;
p=new Panel();
setLayout(null);
l1 = new Label("First Number");
l2 = new Label("Second Number");
l3 = new Label("Result");
t1 = new TextField(10);
t2 = new TextField(10);
t3 = new TextField(10);
b1 = new Button("ADD");
b2 = new Button("SUBTRACT");
b3=new Button("MULTIPLY");
b4=new Button("DIVIDE");
b5=new Button("RESET");
l1.setBounds(10,20,100,30);
l2.setBounds(10,70,100,30);
l3.setBounds(10,120,100,30);
5
Rapid Application Development (CS3011)-Lab Manual
t1.setBounds(250,20,100,30);
t2.setBounds(250,70,100,30);
t3.setBounds(250,120,100,30);
b1.setBounds(10,170,50,50);
b2.setBounds(100,170,100,50);
b3.setBounds(250,170,100,50);
b4.setBounds(90,250,100,50);
b5.setBounds(250,250,100,50);
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
p.add(l3);
p.add(t3);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
add(p);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
6
Rapid Application Development (CS3011)-Lab Manual
b5.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{ String msg = ae.getActionCommand();
if(msg.equals("ADD"))
{ int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int res = n1+n2;
t3.setText(String.valueOf(res));
}
if(msg.equals("SUBTRACT"))
{ int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int res = n1-n2;
t3.setText(String.valueOf(res));
}
if(msg.equals("MULTIPLY"))
7
Rapid Application Development (CS3011)-Lab Manual
{ int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int res = n1*n2;
t3.setText(String.valueOf(res));
}
if(msg.equals("DIVIDE"))
{ int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int res = n1/n2;
t3.setText(String.valueOf(res));
}
if(msg.equals("RESET"))
{
t1.setText("");
t2.setText("");
t3.setText("");
}
}
8
Rapid Application Development (CS3011)-Lab Manual
Experiment – 2
Write a Java application that implements following calculator using Frame.
SOLUTION:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class FrameDemo extends Frame implements ActionListener
{
Label l1,l2,l3;
TextField t1,t2,t3;
Button b1,b2,b3,b4,b5;
Panel f;
public FrameDemo()
{
9
First Number:
Second Number:
Result:
ADD SUBTRACT MULTIPLY
DIVIDE RESET
Rapid Application Development (CS3011)-Lab Manual
f=new Panel();
f.setLayout(null);
l1 = new Label("First Number");
l2 = new Label("Second Number");
l3 = new Label("Result");
t1 = new TextField(10);
t2 = new TextField(10);
t3 = new TextField(10);
b1 = new Button("ADD");
b2 = new Button("SUBTRACT");
b3=new Button("MULTIPLY");
b4=new Button("DIVIDE");
b5=new Button("RESET");
l1.setBounds(10,20,100,30);
l2.setBounds(10,70,100,30);
l3.setBounds(10,120,100,30);
t1.setBounds(250,20,100,30);
t2.setBounds(250,70,100,30);
t3.setBounds(250,120,100,30);
b1.setBounds(10,170,50,50);
10
Rapid Application Development (CS3011)-Lab Manual
b2.setBounds(100,170,100,50);
b3.setBounds(250,170,100,50);
b4.setBounds(90,250,100,50);
b5.setBounds(250,250,100,50);
f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(l3);
f.add(t3);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
add(f);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
11
Rapid Application Development (CS3011)-Lab Manual
{ String msg = ae.getActionCommand();
if(msg.equals("ADD"))
{ int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int res = n1+n2;
t3.setText(String.valueOf(res));
}
if(msg.equals("SUBTRACT"))
{ int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int res = n1-n2;
t3.setText(String.valueOf(res));
}
if(msg.equals("MULTIPLY"))
{ int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int res = n1*n2;
t3.setText(String.valueOf(res));
}
12
Rapid Application Development (CS3011)-Lab Manual
if(msg.equals("DIVIDE"))
{ int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int res = n1/n2;
t3.setText(String.valueOf(res));
}
if(msg.equals("RESET"))
{
t1.setText("");
t2.setText("");
t3.setText("");
}
}
public static void main(String args[])
{
FrameDemo fd = new FrameDemo();
fd.setTitle("My GUI Application Window");
fd.setSize(new Dimension(400,400));
fd.setVisible(true);
}
}
13
Rapid Application Development (CS3011)-Lab Manual
Experiment – 3
Write a Server and Client program that implements Chat application using TCP sockets.
SOLUTION:
import java.net.*;
import java.io.*;
public class ChatClient
{
public static void main(String[] args)
{
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try
{
echoSocket = new Socket("127.0.0.1",4444);
out = new PrintWriter(echoSocket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
Create a table employee with following fields –Emp_codeEmp_nameAddressPhoneDesignationDepartmentBasic SalaryWrite JDBC program that sends the query to select all the fields of employee table to MS Access RDBMS and display all records of the given table.
SOLUTION:
import java.sql.*;
public class CreateMySQLTable
{
public static void main(String ar[])throws SQLException
System.out.println("employee Table created successfully");
}
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server"+e);
}
conn.close();
}
}
22
Rapid Application Development (CS3011)-Lab Manual
Experiment – 5
Write a menu driven program to implement following application as per the user’s choice –f. To insert records in the table g. To modify an existing recordh. To delete an existing recordi. To view all recordsj. Exit
SOLUTION:
import java.sql.*;
import java.io.*;
public class Menudriven
{
public static void main(String ar[])throws SQLException
stmt.executeUpdate("INSERT INTO BookMaster2 VALUES('servlet introduction', 'Suresh', 'chitkara','This book provides genuine knowledge to programmers, who want to learn web based application.')");
System.out.println("Record is inserted");
break;
}
case 3:
{
stmt.executeUpdate("Update BookMaster2 SET AuthorName='Singh' WHERE BookName='servlet introduction'");
System.out.println("Record is updated");
break;
}
case 4:
{
25
Rapid Application Development (CS3011)-Lab Manual
stmt.executeUpdate("DELETE FROM BookMaster2");
System.out.println("Record deleted");
break;
}
case 5:
{
String query = "SELECT * FROM BookMaster2";
System.out.println("The details of the book updated is:");
Write a servlet program that displays following list in the browse windowSystem SoftwareSystem software, more commonly known as Operating system, is any computer software that provides the infrastructure over which programs can operate, ie it manages and controls computer hardware so that application software can perform.
Operating Systems1. MS DOS2. Windows3. UNIX4. Linux
Translators4. Assembler5. Interpreter6. Compiler
Application Software Application Software is any tool that functions and is operated by the means of a
computer, with the purpose of supporting or improving the software user’s work. Word Processors
throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter();
out.println("<html>"); out.println("<head>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");out.println("System SoftwareSystem software, more commonly known as Operating system, is any computer software that provides the infrastructure over which programs can operate, ie it manages and controls computer hardware so that application software can perform.
Operating Systems1. MS DOS2. Windows3. UNIX4. Linux
Translators7. Assembler8. Interpreter9. Compiler
Application Software Application Software is any tool that functions and is operated by the means of a
computer, with the purpose of supporting or improving the software user’s work. Word Processors
out.println("<B>Name Of The Employee:</B> " + empname);out.println("<BR><B>Address of the Emp:</B>
" +empAddress);out.println("<BR><B>ID of emp:</B> " +
empID);
}conn.close ();
}}catch (Exception e){
System.err.println ("Cannot connect to database server");}out.println("</BODY></HTML>");
out.close();}
}
34
Rapid Application Development (CS3011)-Lab Manual
Experiment – 10
Create a login form with two text boxes login name and password and a submit button.Create a servlet program “Verify” that reads data submitted by the login form and create a cookie “uid” and assign value of the login name to it. Redirect the response to another servlet program “NewServlet”. Create “NewServlet” program that reads the cookie “uid” created by the “Verify” and displays following message –
Welcome! Mr. Login name. . . .
SOLUTION:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class NewServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out;
response.setContentType("text/html");
35
Rapid Application Development (CS3011)-Lab Manual
out = response.getWriter();
out.println("<HTML><HEAD><TITLE>");
out.println("Cookie Using Servlets");
out.println("</TITLE></HEAD><BODY>");
out.println("<BR><B> Welcome !Mr.Login Name");
out.println("</BODY></HTML>");
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Verify extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
Create an HTML form that accepts user name and password.
Create a JSP page that reads the user name and password submitted by form and checks the validity of user from the database. Based upon the validity of the user, display an appropriate message on new JSP page.
SOLUTION:
<html>
<head>
<title>Welcome</title>
</head>
<body>
<%=new java.util.Date();%>
hello hio how ru?/
<h2>Welcome</h2>
<form action="showuser.jsp">
<table>
<tr>
41
Rapid Application Development (CS3011)-Lab Manual
<td colspan="2" align="center">
Enter User Information
</td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>City</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td colspan="2"><input
type="submit" value="Enter"></td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" import="java.sql.*" %>
<% Connection conn = null;
42
Rapid Application Development (CS3011)-Lab Manual
Statement stmt = null;
out.println("<HTML><HEAD><TITLE>Welcome To The World Of JDBC</TITLE></HEAD>");