0% found this document useful (0 votes)
18 views48 pages

Lab Manual - AJ

The document outlines a series of Java programming exercises focusing on different concepts such as multithreading, interfaces, packages, RMI, and GUI applications. Each exercise includes an aim, algorithm, coding examples, and results verification. The exercises demonstrate practical implementations of Java features through various programs, including a calculator and mathematical functions.

Uploaded by

Sree RK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views48 pages

Lab Manual - AJ

The document outlines a series of Java programming exercises focusing on different concepts such as multithreading, interfaces, packages, RMI, and GUI applications. Each exercise includes an aim, algorithm, coding examples, and results verification. The exercises demonstrate practical implementations of Java features through various programs, including a calculator and mathematical functions.

Uploaded by

Sree RK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

EX NO: 1 Create a program to implement the multithreading concept

DATE: 22-2-23
Aim:
To write a Java Program for creating multithreads
Algorithm:
Step 1: Start the program with header files.
Step 2: Create two classes for calculating Fibonacci number and prime number
Step 3: Create two threads for two classes
Step 4: Assign two classes to two threads
Step 5: Execute both thread with sleep method
Step 6: Analysis the results

Coding:
import java.io.*;
class fibthread implements Runnable
{int n;
public void run()
{ n=1;
long f1=0;
long f2=1;
long f3;
System.out.println("*************************");
System.out.println("FIBONACCI NUMBER");
System.out.println("*************************");
System.out.println(f1);
System.out.println(f2);

while (n!=50)
{
f3=f1+f2;
f1=f2;
f2=f3;
System.out.println(f3);

1
n++;
try{
if ((n==10))
{ Thread.sleep(50); System.out.println("FIBINACCICS NUMBER");}

if (n==20)
{ Thread.sleep(250); System.out.println("FIBINACCICS NUMBER");}

if (n==30)
{ Thread.sleep(450); System.out.println("FIBINACCICS NUMBER");}
}
catch(InterruptedException e){;}

}
}
}

class primethread implements Runnable


{
public void run()
{ int i,n=1,j,f=1,m=1;
try{
if(m==1)
{ Thread.sleep(10); // System.out.println("PRIME NUMBER");
}}
catch(InterruptedException e){;}
System.out.println("*************************");
System.out.println("PRIME NUMBER");
System.out.println("*************************");

do{
if(n==1)
{ System.out.println(n);}
for( i=2;i<n;i++)
2
{ if(n%i==0)
{f=1;break;}
else{f=0;}
try{
if(m==10||m==20)
{ Thread.sleep(5);
}
}
catch(InterruptedException e){;}
}
if(f!=1){System.out.println(n);m++;}
n++;
}while(m!=25);
}
}

class threadcal
{
public static void main (String args[])//throws IOException
{
fibthread r=new fibthread();
Thread t1=new Thread(r,"thread1");
t1.setPriority(Thread.NORM_PRIORITY+3);
t1.start();
primethread s=new primethread();
Thread t2=new Thread(s,"thread2");
t2.setPriority(Thread.NORM_PRIORITY);
t2.start();

}
}

3
OUTPUT:

Results:
Program results are verified for multithread concept

4
EX NO: 2 Create a program to implement the interface concept
DATE: 01-3-23
DATE:
Aim:
To write a Java Program for creating interface
Algorithm:
Step 1: Start the program with header files.
Step 2: Create two classes for calculating Fibonacci number and prime number
Step 3: Create two interface named as process
Step 4: Execute both class with interface
Step 5: Analysis the results

Coding:
import java.io.*;
public interface number
{int process(int x,int y);}

import java.io.*;
import java.util.*;
class avg implements number
{
int average;
public int process(int x,int y)
{average=x/y;
return average;}
}

import java.io.*;
import java.util.*;
class sum implements number
{
int tot;
public int process(int x,int y)
{ tot=x+y;
return tot;}
}

import java.io.*;
import java.util.*;
class interfacemain
{
public static void main(String args[])throws IOException
{Scanner s=new Scanner(System.in);
5
sum s1=new sum();
avg a1=new avg();
System.out.println("enter the first number:");
int n1=s.nextInt();
System.out.println("enter the second number:");
int n2=s.nextInt();
int tot=s1.process(n1,n2);
System.out.println("sum of two number:"+tot);
int avg=a1.process(tot,2);
System.out.println("avg of two number:"+avg);
}
}
OUTPUT:

Results:
Program results are verified for interface concept

6
EX NO: 3 Create a program to implement the package concept
DATE: 08-03-23
DATE:
Aim:
To write a Java Program for creating package
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a class for mathematical formulas – sin, cos and exponential
Step 3: Create a class created under the package
Step 4: import the packages into main class
Step 5: Execute the program
Step 6: Analysis the results

Coding:
package mathfun;
import java.io.*;
import java.util.*;
import java.lang.*;
public class math
{
public void sin1(double x1)
{
int i=1,j=3;
double sum1=0.0,sum2=0.0;
while(i<=20)
{
double d=0.0,f=0.0;
d=fact(i);
f=Math.pow(x1,i);
f=f/d;
sum1=sum1+f;
i=i+4;
}
while(j<=20)
{
double d1=0.0;
d1=fact(j);
double f=0.0;
f=Math.pow(x1,j);
f=f/d1;
sum2=sum2+f;
j=j+4;
}
7
System.out.println("MATH SIN X :"+Math.sin(x1));
System.out.println();
System.out.println("SIN X :"+(sum1-sum2));
System.out.println();
System.out.println("RELATIVE ERROR:"+(Math.sin(x1)-(sum1-sum2)));
System.out.println();
}

public void cos1(double x1)


{
int i=4,j=2;
double sum1=0.0,sum2=0.0;
while(i<20)
{
double d=0.0,f=0.0;
d=fact(i);
f=Math.pow(x1,i);
f=f/d;
sum1=sum1+f;
i=i+4;
}
while(j<=20)
{
double d1=0.0;
d1=fact(j);
double f=0.0;
f=Math.pow(x1,j);
f=f/d1;
sum2=sum2+f;
j=j+4;
}
System.out.println("MATH COS X :"+Math.cos(x1));
System.out.println();
System.out.println("COS X :"+(1+sum1-sum2));
System.out.println();
System.out.println("RELATIVE ERROR :"+(Math.cos(x1)-(1+sum1-sum2)));
System.out.println();
}

public void ex1(double x1)


{
int i=1;
double sum1=0.0;
while(i<10)
{
double d=0.0,f=0.0;
d=fact(i);
f=Math.pow(x1,i);
f=f/d;
sum1=sum1+f;

8
i=i+1;
}
double f1=Math.pow(Math.E,x1);
double f2=(1+sum1);
System.out.println("MATH E POWER X :"+f1);
System.out.println();
System.out.println("MY E POWER X :"+f2);
System.out.println();
System.out.println("RELATIVE ERROR :"+(f1-f2));
System.out.println();
}

public int fact(int g)


{
if(g<=1){ return 1;}
else { return g*fact(g-1);}
}
}

import mathfun.math;
import java.io.*;
class mfunmain
{ public static void main(String args[])throws IOException
{
math m=new math();
double x=0.0,x2=0.0;
DataInputStream in=new DataInputStream(System.in);
System.out.println("ENTER THE VALUE IN DEGREE:");
x=Double.parseDouble(in.readLine());
x2=(double)(x*22)/(180*7);
System.out.println("******************************");
System.out.println(" SIN X:");
System.out.println("******************************");
m.sin1(x2);
System.out.println("******************************");
System.out.println(" COS X:");
System.out.println("******************************");
m.cos1(x2);
System.out.println("******************************");
System.out.println(" E POWER X:");
System.out.println("******************************");
m.ex1(x2);
}
}

OUTPUT:

9
Results:
Program results are verified for package concept

10
EX NO: 4 Create a program to implement the RMI concept
DATE: 15-03-23
DATE:
Aim:
To write a Java Program for RMI concept
Algorithm:
Step 1: Start the program with RMI header files.
Step 2: Create an interface for establish a remote connection
Step 3: Create a class for implementing the interface method
Step 4: Test server status
Step 5: Execute the program
Step 6: Analysis the results

Coding:
import java.rmi.Remote;
import java.rmi.RemoteException;

// Creating Remote interface for our application


public interface Hello extends Remote {
void printMsg() throws RemoteException;
}

public class ImplExample implements Hello {

// Implementing the interface method


public void printMsg() {
System.out.println("This is an example RMI program");
}
}

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;

11
import java.rmi.server.UnicastRemoteObject;

public class Server extends ImplExample {


public Server() {}
public static void main(String args[]) {
try {
// Instantiating the implementation class
ImplExample obj = new ImplExample();

// Exporting the object of implementation class


// (here we are exporting the remote object to the stub)
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);

// Binding the remote object (stub) in the registry


Registry registry = LocateRegistry.getRegistry();

registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client {


private Client() {}
public static void main(String[] args) {
try {
// Getting the registry
Registry registry = LocateRegistry.getRegistry(null);

12
// Looking up the registry for the remote object
Hello stub = (Hello) registry.lookup("Hello");

// Calling the remote method using the obtained object


stub.printMsg();

// System.out.println("Remote method invoked");


} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}

Output:

13
Results:
Program results are verified for RMI concept

14
EX NO: 5 Write a program to design a calculator using Java components
and handle various events related activities
DATE: 29-03-23

Aim:
To write a Java Program for Calculator
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a classes for calculator
Step 3: Create action listener on Jframe
Step 4: Design the calculator with buttons
Step 5: Execute the code
Step 6: Analysis the results

Coding:
// Java program to create a simple calculator
// with basic +, -, /, * using java swing elements

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
class calculator extends JFrame implements ActionListener

{
// create a frame
static JFrame f;

// create a textfield
static JTextField l;

// store oprerator and operands


String s0, s1, s2;

15
// default constrcutor
calculator()
{
s0 = s1 = s2 = "";
}

// main function
public static void main(String args[])
{
// create a frame
f = new JFrame("calculator");

try {
// set look and feel
UIManager.setLookAndFeel

(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
System.err.println(e.getMessage

());
}

// create a object of class


calculator c = new calculator();

// create a textfield
l = new JTextField(16);

// set the textfield to non editable


l.setEditable(false);

16
// create number buttons and some

operators
JButton b0, b1, b2, b3, b4, b5, b6, b7,

b8, b9, ba, bs, bd, bm, be, beq, beq1;

// create number buttons


b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");

// equals button
beq1 = new JButton("=");

// create operator buttons


ba = new JButton("+");
bs = new JButton("-");
bd = new JButton("/");
bm = new JButton("*");
beq = new JButton("C");

// create . button
be = new JButton(".");

// create a panel
JPanel p = new JPanel();
17
// add action listeners
bm.addActionListener(c);
bd.addActionListener(c);
bs.addActionListener(c);
ba.addActionListener(c);
b9.addActionListener(c);
b8.addActionListener(c);
b7.addActionListener(c);
b6.addActionListener(c);
b5.addActionListener(c);
b4.addActionListener(c);
b3.addActionListener(c);
b2.addActionListener(c);
b1.addActionListener(c);
b0.addActionListener(c);
be.addActionListener(c);
beq.addActionListener(c);
beq1.addActionListener(c);

// add elements to panel


p.add(l);
p.add(ba);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(bs);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(bm);
p.add(b7);
p.add(b8);
p.add(b9);
18
p.add(bd);
p.add(be);
p.add(b0);
p.add(beq);
p.add(beq1);

// set Background of panel


p.setBackground(Color.blue);

// add panel to frame


f.add(p);

f.setSize(200, 220);
f.show();
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();

// if the value is a number


if ((s.charAt(0) >= '0' && s.charAt(0) <= '9') || s.charAt(0) == '.') {
// if operand is present then add to second no
if (!s1.equals(""))
s2 = s2 + s;
else
s0 = s0 + s;

// set the value of text


l.setText(s0 + s1 + s2);
}
else if (s.charAt(0) == 'C') {
// clear the one letter
s0 = s1 = s2 = "";

19
// set the value of text
l.setText(s0 + s1 + s2);
}
else if (s.charAt(0) == '=') {

double te;

// store the value in 1st


if (s1.equals("+"))
te = (Double.parseDouble(s0) + Double.parseDouble(s2));
else if (s1.equals("-"))
te = (Double.parseDouble(s0) - Double.parseDouble(s2));
else if (s1.equals("/"))
te = (Double.parseDouble(s0) / Double.parseDouble(s2));
else
te = (Double.parseDouble(s0) * Double.parseDouble(s2));

// set the value of text


l.setText(s0 + s1 + s2 + "=" + te);

// convert it to string
s0 = Double.toString(te);

s1 = s2 = "";
}
else {
// if there was no operand
if (s1.equals("") || s2.equals

(""))
s1 = s;
// else evaluate
else {
double te;
20
// store the value in 1st
if (s1.equals("+"))
te = (Double.parseDouble(s0) + Double.parseDouble(s2));
else if (s1.equals("-"))
te = (Double.parseDouble(s0) - Double.parseDouble(s2));
else if (s1.equals("/"))
te = (Double.parseDouble(s0) / Double.parseDouble(s2));
else
te = (Double.parseDouble(s0) * Double.parseDouble(s2));

// convert it to string
s0 = Double.toString(te);

// place the operator


s1 = s;

// make the operand blank


s2 = "";
}

// set the value of text


l.setText(s0 + s1 + s2);
}
}
}
Output:

21
Results:
Program results are verified for Swing concept with calculator

22
EX NO: 6 Write a program to demonstrate use of Grid Layout
DATE: 01-04-23
DATE:
Aim:
To write a Java Program for creating Grid Layout
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a classes for Grid Layout
Step 3: Create frame for window
Step 4: Design the grid layout on frame with buttons
Step 5: Execute the code
Step 6: Analysis the results

Coding:
import java.awt.*;
import javax.swing.*;

public class MyGridLayout{


JFrame f;
MyGridLayout(){
f=new JFrame();

JButton b1=new JButton("1");


JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");

f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);

23
f.add(b6);f.add(b7);f.add(b8);f.add(b9);

f.setLayout(new GridLayout(3,3));
//setting grid layout of 3 rows and 3 columns

f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new MyGridLayout();
}
}

Output:

Results:
Program results are verified for grid layout concept

24
EX NO: 7 Write a program to demonstrate use of Flow Layout
DATE: 05-04-23
Aim:
To write a Java Program for creating Flow Layout
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a classes for Flow Layout
Step 3: Create frame for window
Step 4: Design the Flow layout on frame with buttons
Step 5: Execute the code
Step 6: Analysis the results

Coding:

import java.awt.*;
import javax.swing.*;

public class MyFlowLayout{


JFrame f;
MyFlowLayout(){
f=new JFrame();

JButton b1=new JButton("1");


JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");

f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);

f.setLayout(new FlowLayout(FlowLayout.RIGHT));
//setting flow layout of right alignment

25
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new MyFlowLayout();
}
}
Output:

Results:
Program results are verified for Flow Layout

26
EX NO: 8 Write a program to demonstrate use of Card Layout
DATE: 19-04-23

Aim:
To write a Java Program for creating Card Layout
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a classes for Card Layout
Step 3: Create frame for window
Step 4: Design the Card layout on frame with buttons
Step 5: Execute the code
Step 6: Analysis the results

Coding:
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class CardLayoutExample extends JFrame implements

ActionListener{
CardLayout card;
JButton b1,b2,b3;
Container c;
CardLayoutExample(){

c=getContentPane();
card=new CardLayout(40,30);
//create CardLayout object with 40 hor space and 30 ver

space
c.setLayout(card);

27
b1=new JButton("Apple");
b2=new JButton("Boy");
b3=new JButton("Cat");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

c.add("a",b1);c.add("b",b2);c.add("c",b3);

}
public void actionPerformed(ActionEvent e) {
card.next(c);
}

public static void main(String[] args) {


CardLayoutExample cl=new CardLayoutExample();
cl.setSize(400,400);
cl.setVisible(true);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Output:

Results:
Program results are verified for Card concept
28
EX NO: 9 Write a program to demonstrate use of Border Layout
DATE: 26-04-23

Aim:
To write a Java Program for creating Border Layout
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a classes for Border Layout
Step 3: Create frame for window
Step 4: Design the Border layout on frame with buttons
Step 5: Execute the code
Step 6: Analysis the results

Coding:
import java.awt.*;
import javax.swing.*;

public class Border {


JFrame f;
Border(){
f=new JFrame();

JButton b1=new JButton("NORTH");;


JButton b2=new JButton("SOUTH");;
JButton b3=new JButton("EAST");;
JButton b4=new JButton("WEST");;
JButton b5=new JButton("CENTER");;

f.add(b1,BorderLayout.NORTH);
f.add(b2,BorderLayout.SOUTH);
f.add(b3,BorderLayout.EAST);
f.add(b4,BorderLayout.WEST);

29
f.add(b5,BorderLayout.CENTER);

f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new Border();
}
}
Output:

Results:
Program results are verified for Border Layout concept

30
EX NO: 10 Write a program to design a form using basic swing components
DATE: 03-05-23

Aim:
To write a Java Program for Form on Swing
Algorithm:
Step 1: Start the program with header files.
Step 2: Create two classes for design a login form
Step 3: Design the form with swing components
Step 4: Add the action listener on the components
Step 5: Execute the code
Step 6: Analysis the results

Coding:
import java.awt.Color;
import java.awt.Font;
import java.util.*;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginForm extends JFrame implements ActionListener{


JLabel l1, l2, l3;
JTextField tf1;
JButton btn1;
JPasswordField p1;
LoginForm() {
JFrame frame = new JFrame("Login Form");
l1 = new JLabel("Login Form");
l1.setForeground(Color.blue);

31
l1.setFont(new Font("Serif", Font.BOLD, 20));

l2 = new JLabel("Username");
l3 = new JLabel("Password");
tf1 = new JTextField();
p1 = new JPasswordField();
btn1 = new JButton("Login");

l1.setBounds(100, 30, 400, 30);


l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
tf1.setBounds(300, 70, 200, 30);
p1.setBounds(300, 110, 200, 30);
btn1.setBounds(150, 160, 100, 30);

frame.add(l1);
frame.add(l2);
frame.add(tf1);
frame.add(l3);
frame.add(p1);
frame.add(btn1);

frame.setSize(400, 400);
frame.setLayout(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
String uname = t1.getText();
String pass = p1.getText();
if(uname.equals("sudhir@oodlesTech") && pass.equals("abc@123"))
{
Welcome wel = new Welcome();
wel.setVisible(true);
32
JLabel label = new JLabel("Welcome:"+uname);
wel.getContentPane().add(label);
}
else
{
JOptionPane.showMessageDialog(this,"Incorrect login or password",
"Error",JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
new LoginForm();
}
}

Welcome.java

//package com.sudhir.oodlesTech;
import javax.swing.*;
import java.awt.*;
class Welcome extends JFrame
{
Welcome()
{
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Welcome");
setSize(400, 200);
}
}

Output:
33
Results:
Program results are verified for Swing Form concept

34
EX NO: 11 Write a program to demonstrate the use of scroll panes in Swing
DATE: 10-05-23
Aim:
To write a Java Program for Scroll panel
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a classes for adding horizontal and vertical scroll panel
Step 3: Design the GUI with swing components
Step 4: Execute the program
Step 5: Analysis the results

Coding:
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.*;

public class JScrollPaneExample {


private static final long serialVersionUID = 1L;

private static void createAndShowGUI() {

// Create and set up the window.


final JFrame frame = new JFrame("Scroll Pane Example");

// Display the window.


frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// set flow layout for the frame


frame.getContentPane().setLayout(new FlowLayout());
35
JTextArea textArea = new JTextArea(20, 20);
JScrollPane scrollableTextArea = new JScrollPane(textArea);

scrollableTextArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBA
R_ALWAYS);

scrollableTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AL
WAYS);

frame.getContentPane().add(scrollableTextArea);
}
public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {


createAndShowGUI();
}
});
}
}
Output:

36
Results:
Program results are verified for Scroll panel

37
EX NO: 12 Write a servlet to demonstrate the Http Servlet class using do Get ()
DATE: 16-05-23
Aim:
To write a Java Program for Servlet using doGet()
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a classes for calculating http servlet using doGet()
Step 3: Establish the connection between server and client
Step 4: Execute the program
Step 5: Analysis the results

Coding:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class


public class HelloWorld extends HttpServlet {

private String message;

public void init() throws ServletException {


// Do required initialization
message = "Hello World";
}

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

// Set response content type


response.setContentType("text/html");

38
// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}

public void destroy() {


// do nothing.
}
}

Output:

Results:
Program results are verified for Http Servlet concept

39
EX NO: 13 Write a servlet to demonstrate the Http Servlet class using do Post ()
DATE: 23-05-23
Aim:
To write a Java Program for Servlet using doPost()
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a classes for calculating http servlet using doPost()
Step 3: Establish the connection between server and client
Step 4: Execute the program
Step 5: Analysis the results

Coding:
package com.edu4java.servlets;
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 LoginServlet extends HttpServlet {


@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String user = req.getParameter("user");
String pass = req.getParameter("password");
if ("edu4java".equals(user) && "eli4java".equals(pass)) {
response(resp, "login ok");
} else {
response(resp, "invalid login");
}
}

40
private void response(HttpServletResponse resp, String msg)
throws IOException {
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<t1>" + msg + "</t1>");
out.println("</body>");
out.println("</html>");
}
}
Output:

Results:
Program results are verified for doPost () Servlet concept

41
EX NO: 14 Write a servlet to demonstrate the cookie
DATE: 30-05-23
Aim:
To write a Java Program for cookie
Algorithm:
Step 1: Start the program with header files.
Step 2: Create html and xml for cookie program
Step 3: Create two programs for httprequest and response
Step 4: Execute the program
Step 5: Analysis the results

Coding:
index.html
<form action="servlet1" method="post">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="go"/>
</form>
FirstServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class FirstServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response){


try{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String n=request.getParameter("userName");
out.print("Welcome "+n);

42
Cookie ck=new Cookie("uname",n);//creating cookie object
response.addCookie(ck);//adding cookie in the response

//creating submit button


out.print("<form action='servlet2'>");
out.print("<input type='submit' value='go'>");
out.print("</form>");

out.close();

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

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

public class SecondServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response){


try{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

Cookie ck[]=request.getCookies();
out.print("Hello "+ck[0].getValue());

out.close();

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

}
web.xml
<web-app>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
</web-app>
Output:

44
Results:
Program results are verified for Cookie concept

45
EX NO: 15 Write an Application program/Applet to send queries
through JDBC bridge & handle result.
DATE: 06-06-23
Aim:
To write a Java Program for JDBC SQL connection
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a class for accessing SQL through JDBC – ODBC Bridge
Step 3: Create the connection and statement
Step 4: Execute the program
Step 5: Analysis the results

Coding:
import java.sql.*;

import java.io.*;

class DBQuery6

public static void main (String args[]) throws SQLException,

IOException

try{

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

catch(ClassNotFoundException e){};

Connection cnn;

CallableStatement cs;

ResultSet rs;

String open="jdbc:odbc:JavaTest";

46
cnn=DriverManager.getConnection(open,"Admin","");

cs=cnn.prepareCall("{call SuppProd (?)}");

int inp;

BufferedReader d= new BufferedReader(new

InputStreamReader(System.in));

System.out.println("Enter the product code");

inp=Integer.parseInt(d.readLine());

cs.setInt(1,inp);

rs= cs.executeQuery();

System.out.println(" "+"Sup Code" + " " +"Sup Name" +"

" + "Sup Address ");

System.out.println("--------------------------------------------------------------------------------");

String name, add;

int code;

while (rs.next())

code= rs.getInt("SupplierCode");

name= rs.getString("SupplierName");

add= rs.getString("SupplierAddress");

System.out.println(" " +code + " " + name +" " +add);

Output:

Enter the product code : 3

47
Sup Code Sup Name Sup Address

-----------------------------------------------------------------------

2 Mela Ram 1 Subji Mandi New Delhi

Results:
Program results are verified for JDBC Bride concept

48

You might also like