58
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

5997.Solutioncsof lab manual3011.doc

Embed Size (px)

Citation preview

Page 1: 5997.Solutioncsof lab manual3011.doc

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

Page 2: 5997.Solutioncsof lab manual3011.doc

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.

33-34

2

Page 3: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

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

Page 4: 5997.Solutioncsof lab manual3011.doc

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

Page 5: 5997.Solutioncsof lab manual3011.doc

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

Page 6: 5997.Solutioncsof lab manual3011.doc

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

Page 7: 5997.Solutioncsof lab manual3011.doc

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

Page 8: 5997.Solutioncsof lab manual3011.doc

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

Page 9: 5997.Solutioncsof lab manual3011.doc

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

Page 10: 5997.Solutioncsof lab manual3011.doc

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

Page 11: 5997.Solutioncsof lab manual3011.doc

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

Page 12: 5997.Solutioncsof lab manual3011.doc

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

Page 13: 5997.Solutioncsof lab manual3011.doc

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

Page 14: 5997.Solutioncsof lab manual3011.doc

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()));

14

Page 15: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

}

catch(UnknownHostException ue)

{

System.out.println("Trouble: " + ue.getMessage());

}

catch(IOException ioe)

{

System.out.println("Trouble: " + ioe.getMessage());

}

catch(SecurityException se)

{

System.out.println("Trouble: " + se.getMessage());

}

catch(Exception e)

{

System.out.println("Trouble: " + e.getMessage());

}

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

boolean finished = false;

String userInput;

try

{

15

Page 16: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

System.out.println("Received : " + in.readLine());

do

{

System.out.print("Send : ");

userInput = stdIn.readLine();

if(userInput.equalsIgnoreCase("quit"))

finished = true;

out.println(userInput);

System.out.println("Received : " + in.readLine());

}while(!finished);

out.close();

in.close();

stdIn.close();

echoSocket.close();

}catch(Exception e)

{

System.out.println("Trouble: " + e.getMessage());

}

}

}

16

Page 17: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

//EchoServer.java

import java.net.*;

import java.io.*;

public class ChatServer

{

public static void main(String args[])

{

Socket clientSocket;

ServerSocket serverSocket = null;

int port = 4444;

try

{

//Creating ServerSocket instance

serverSocket = new ServerSocket(port);

}

catch(SecurityException se)

{

System.out.println("Trouble : " + se.getMessage());

}

catch(IOException ioe)

{

System.out.println("Trouble : " + ioe.getMessage());

}

17

Page 18: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

try

{

//Server is accepting connection

clientSocket = serverSocket.accept();

// Initializing I/O streams

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),true);

BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

//Server is ready to start conversation

//Initiate conversation with Client

String inputLine, userInput;

boolean finished = false;

out.println("Server is listening on port " + port);

do

{

inputLine = in.readLine();

System.out.println("Received : " + inputLine);

if(inputLine.equalsIgnoreCase("quit"))

finished = true;

System.out.print("Send : ");

userInput = stdIn.readLine();

18

Page 19: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

out.println(userInput);

out.flush();

}while(!finished);

//Close all IO streams and sockets

in.close();

out.close();

clientSocket.close();

serverSocket.close();

} catch(SecurityException se)

{

System.out.println("Trouble : " + se.getMessage());

}

catch(IOException ioe)

{

19

Page 20: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

System.out.println("Trouble : " + ioe.getMessage());

}

}

}

20

Page 21: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

Experiment – 4

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

{

Connection conn = null;

Statement stmt = null;

try

{

21

Page 22: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

conn = DriverManager.getConnection("Jdbc:Odbc:db1");

if (conn != null)

{

stmt = conn.createStatement();

stmt.executeUpdate("CREATE TABLE employee(Emp_code varchar(50),Emp_name varchar(50),Address varchar(50),Phone int,Designation varchar(50),Department varchar(50),Basic_salary int);");

System.out.println("employee Table created successfully");

}

}

catch (Exception e)

{

System.err.println ("Cannot connect to database server"+e);

}

conn.close();

}

}

22

Page 23: 5997.Solutioncsof lab manual3011.doc

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

{

Connection conn = null;

Statement stmt = null;

try

{

23

Page 24: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

conn = DriverManager.getConnection("Jdbc:Odbc:db1");

stmt = conn.createStatement();

int x;

System.out.println("welcome please enter your choice");

System.out.println("1 create a table");

System.out.println("2 to insert a record in table");

System.out.println("3 to modify an existing record");

System.out.println("4 to delete an existing record");

System.out.println("5 to view all records");

System.out.println("6 exit");

BufferedReader b=new BufferedReader(new InputStreamReader(System.in));

x=Integer.parseInt(b.readLine());

switch(x)

{

case 1:

{

24

Page 25: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

stmt.executeUpdate("CREATE TABLE BookMaster3(BookName varchar(50),AuthorName varchar(50),PublisherName varchar(40),BookSynopsis varchar(100));");

System.out.println("Table created");

break;

}

case 2:

{

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

Page 26: 5997.Solutioncsof lab manual3011.doc

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:");

ResultSet rs = stmt.executeQuery(query);

while (rs.next())

{

String BookName = rs.getString("BookName");

String AuthorName = rs.getString("AuthorName");

String PublisherName = rs.getString("PublisherName");

String Synopsis = rs.getString("BookSynopsis");

System.out.println("Name Of The Book: " +BookName);

System.out.println("Name Of The Author:" +AuthorName);

26

Page 27: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

System.out.println("Name Of The Publisher: " +PublisherName);

System.out.println("Synopsis: " + Synopsis);

}

break;

}

case 6:

{

System.exit(0);

break;

}

default:

{

System.out.println("please enter valid choice");

}

}

}

catch (Exception e)

{

System.err.println ("Cannot connect to database server"+e);

}

conn.close();

}}

27

Page 28: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

Experiment – 6

Write a JDBC program that inserts five records in employee table using PreparedStatement object.

SOLUTION:

import java.sql.*;

import java.io.*;

import java.net.*;

public class Ji

{

public static void main(String args[])throws Exception

{

Connection conn;

PrintWriter out=null;

Statement stmt=null;

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

conn=DriverManager.getConnection("Jdbc:Odbc:db2");

stmt=conn.createStatement();

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("connection established");

System.out.println("Enter id");

28

Page 29: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

int c=Integer.parseInt(br.readLine());

PreparedStatement updateSales;

String updateString = "update COFFEES " + "set SALES = ? where COF_NAME like ?";

updateSales = con.prepareStatement(updateString);

int [] salesForWeek = {175, 150, 60, 155, 90};

String [] coffees = {"Colombian", "French_Roast", "Espresso", "Colombian_Decaf", "French_Roast_Decaf"};

int len = coffees.length;

for(int i = 0; i < len; i++) {

updateSales.setInt(1, salesForWeek[i]);

updateSales.setString(2, coffees[i]);

updateSales.executeUpdate();

}

me.close();

conn.close();

}

}

29

Page 30: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

Experiment – 7

Write a servlet program that displays “Hello World” in a browser window.

SOLUTION:

import java.io.*;import java.text.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) 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("hello world");

out.println("</body>"); out.println("</html>"); }}

30

Page 31: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

Experiment – 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

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

III. Word StarIV. MS Word

Spread SheetsIII. Lotus 1-2-3IV. MS Excel

SOLUTION:

import java.io.*;import java.text.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

31

Page 32: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

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

V. Word StarVI. MS Word

Spread SheetsV. Lotus 1-2-3

VI. MS Excel

");

out.println("</body>"); out.println("</html>"); }}

32

Page 33: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

Experiment – 9

Create a servlet program that reads the data of employee table from the access database and displays the records in an HTML table.

SOLUTION: import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import java.sql.*;import javax.sql.*;

public class UpdateMySQLTable extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

Connection conn = null;Statement stmt = null;

out.println("<HTML><HEAD><TITLE>Welcome To The World Of JDBC</TITLE></HEAD>");

out.println("<BODY>");

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

conn = DriverManager.getConnection("jdbc:odbc:db");

33

Page 34: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

if (conn != null){

stmt=conn.createStatement();

String query="select * from emp'"; ResultSet rs = stmt.executeQuery(query);

out.println("The details of the employee:<BR><BR>");

while (rs.next()){

String empname = rs.getString("Name");String empAddress = rs.getString("Address");String empID = rs.getString("empid");

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

Page 35: 5997.Solutioncsof lab manual3011.doc

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

Page 36: 5997.Solutioncsof lab manual3011.doc

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)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

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

36

Page 37: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

String pass = request.getParameter("password");

Cookie c=new Cookie(name,pass);

response.addCookie(c);

Cookie[] cookies = request.getCookies();

if (cookies != null)

{

for(int i=0; i < cookies.length; i++)

{

Cookie thisCookie = cookies[i];

//String p=thisCookie.getValue();

if(thisCookie.getValue().equals(pass))

{

RequestDispatcher rd=request.getRequestDispatcher("/newservlet");

rd.forward(request,response);

}

else{

out.println("bye");

37

Page 38: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

}

}}

}}

<html>

<head>

<title>New Page 1</title>

</head>

<body>

<h2>Login</h2>

<p>Please enter your username and password</p>

<form method="GET" action="/prog10/run">

<p> Username <input type="text" name="username" size="20"></p>

<p> Password <input type="text" name="password" size="20"></p>

<p><input type="submit" value="Submit" name="B1"></p>

</form>

<p>&nbsp;</p>

</body>

38

Page 39: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

</html>

Experiment – 11

Write a JSP program to display “Welcome to Chitkara University” in the browser window

SOLUTION:

<html>

<body>

Out.println(“Welcome to chitkara University”);

</body>

</html>

39

Page 40: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

Experiment – 12

Create a jsp to calculate the square of number.

Solution:

<html>

<head>

<title>

Example jsp page</title>

</head>

<body bgcolor='white'>

<b>Table of number squred:</b>

<table border='1' cellspacing='0' cellpadding='5'>

<tr><th>number</th><th>squared</th></tr>

<% for(int i=0;i<10;i++){ %>

<tr><td><%=i%></td><td><%=(i*i)%></td></tr>

<%}%>

</table>

</body>

40

Page 41: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

</html>

Experiment – 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.

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

Page 42: 5997.Solutioncsof lab manual3011.doc

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

Page 43: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

Statement stmt = null;

out.println("<HTML><HEAD><TITLE>Welcome To The World Of JDBC</TITLE></HEAD>");

out.println("<BODY><H2>");

try

{

<b><%String name=request.getParameter("name")%></b><br><br>

<b><%String pass=request.getParameter("password")%></b><br><br>

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

conn = DriverManager.getConnection("Jdbc:Odbc:db2");

if (conn != null)

{

stmt=conn.createStatement();

String query="select * from emp where empName =name&&password=pass'"; ResultSet rs = stmt.executeQuery(query);

if (rs.next()){

out.println(“welcome”);

43

Page 44: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

}

conn.close();

}

}

catch (Exception e)

{

out.println ("Cannot connect to database server");

}

out.println("</H2></BODY></HTML>");

out.close();

%>

44

Page 45: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

Experiment – 14

Create an RMI application to convert given temperature from Celsius to Fahrenheit.

SOLUTION:

import java.rmi.*;

public interface MyRemote extends Remote

{

public float conversion()throws RemoteException;

}

import java.rmi.*;

import java.rmi.server.*;

import java.net.*;

public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote

{

45

Page 46: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

public float conversion()

{

float temp_f,temp_c;

temp_c=34;

temp_f = (1.8 * temp_c) + 32;

return temp_f;

}

public MyRemoteImpl()throws RemoteException

{

super();

}

public static void main(String arg[])

{

try

{

MyRemote service=new MyRemoteImpl();

Naming.rebind("hello",service);

}

catch(Exception e)

46

Page 47: 5997.Solutioncsof lab manual3011.doc

Rapid Application Development (CS3011)-Lab Manual

{

e.printStackTrace();

}

}

}

import java.rmi.*;

public class MyRemoteClient

{

public static void main(String arg[])

{

new MyRemoteClient().go();

}

public void go(){

try{

MyRemote service=(MyRemote) Naming.lookup("rmi://127.0.0.1/hello");

String s=service.sayHello();

System.out.println(s);

}catch(Exception e)

{

e.printStackTrace();

}

}

}

47