Java Questions and Answers

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 40

IMPORTANT JAVA QUESTIONS AND SOLUTIONS

Question#01
Q. Write a Java program to read the details from the user (Name, City, Contact No., Mail ID etc.) and
display it on Console.
Solution :
Program Name : Read_Stud_info.java

import java.io.*;

public class Read_Stud_info


{
public static void main(String args[])
{
InputStreamReader in = null;
BufferedReader bin = null;

try
{
in = new InputStreamReader(System.in);
bin = new BufferedReader(in);

System.out.println("\nEnter your Name ::");


String name = bin.readLine();

System.out.println("\nEnter your City ::");


String city = bin.readLine();

System.out.println("\nEnter your Contact No. ::");


String contact_no = bin.readLine();

System.out.println("\nEnter your Email ID ::");


String email = bin.readLine();

System.out.println("\n\n################ STUDENT INFORMATION


##############");
System.out.println("\n NAME \t"+name);
System.out.println("\n CITY \t"+city);
System.out.println("\n CONTACT_NO. \t"+contact_no);
System.out.println("\n EMAILID \t"+email);
}catch(IOException e)
{
System.out.println(e);
}
}
}

Question#02
Q. Write a Java Program to read the content from file and display it on the console line by line.
Solution :
Program Name : Read_File.java
import java.io.*;
public class Read_File
{
public static void main(String args[])
{
FileReader in = null;
BufferedReader bin = null;
String str;

try
{
in = new FileReader("abc.txt");
bin = new BufferedReader(in);
while((str= bin.readLine()) != null)
{
System.out.println(str);

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

Question#03
Q. Write a Java Program to swap the content of one file into another, read the filenames from user.
Solution :
Program Name : File_Swap.java
import java.io.*;

public class File_Swap


{
public static void main(String args[])
{
InputStreamReader in = null;
FileReader fin1,fin2,fin3;
BufferedReader bin,bin1,bin2,bin3;
FileWriter fout1,fout2,fout3;
PrintWriter bout1,bout2,bout3;
String str1,str2,str3;

try
{
in = new InputStreamReader(System.in);
bin = new BufferedReader(in);

System.out.println("\n Enter File name#1 ::");


String file1 = bin.readLine();
System.out.println("\n Enter File name#2 ::");
String file2 = bin.readLine();

fin1 = new FileReader(file1);


fout1 = new FileWriter("temp.txt");
bin1 = new BufferedReader(fin1);
bout1 = new PrintWriter(fout1);

while((str1 = bin1.readLine()) != null)


{
bout1.println(str1);
}

bout1.flush();

fin1.close();
fout1.close();
bin1.close();
bout1.close();

fin2 = new FileReader(file2);


fout2 = new FileWriter(file1);
bin2 = new BufferedReader(fin2);
bout2 = new PrintWriter(fout2);

while((str2 = bin2.readLine()) != null)


{
bout2.println(str2);
}

bout2.flush();
fin2.close();
fout2.close();
bin2.close();
bout2.close();
fin3 = new FileReader("temp.txt");
fout3 = new FileWriter(file2);
bin3 = new BufferedReader(fin3);
bout3 = new PrintWriter(fout3);

while((str3 = bin3.readLine()) != null)


{
bout3.println(str3);
}
bout3.flush();
fin3.close();
fout3.close();
bin3.close();
bout3.close();

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

Question#04
Q. Write a Java Program to count the number of characters, words and lines from the file.
Solution :
Program Name : Count_File_line.java
import java.io.*;

public class Count_File_line


{
public static void main(String args[])
{
FileReader in = null;
BufferedReader bin = null;
int c = 0;
char ch;
int words=0,ch1=0,line=0;

try
{
in = new FileReader("abc.txt");

while((c = in.read()) != -1)


{
ch = (char) c;
if(ch == '\n')
{
line++;
}
else if(ch ==' ')
{
words++;
}
else
{0
ch1++;
}
}
System.out.println(" No. of Lines :: "+ line);
System.out.println(" No. of Words :: "+ words);
System.out.println(" No. of Characters:: "+ ch1);
}catch(IOException e)
{
System.out.println(e);
}
}
}
Question#05
Q. Write a Java Program to copy the content of two files into one file, read the file name from users.
Solution :
Program Name : RandomAccessFile.java
import java.io.*;

public class RandomAccessFile


{
public static void main(String arsg[])
{
RandomAccessFile fin1,fin2,fin3;
BufferedReader in = null;
String file1, file2, file3;
int ch=0;

try
{
in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n Enter File Name#1");
file1 = in.readLine();

System.out.println("\n Enter File Name#2");


file2 = in.readLine();
System.out.println("\n Enter File Name#3");
file3 = in.readLine();

fin1 = new RandomAccessFile(file1,"r");


fin2 = new RandomAccessFile(file2,"r");
fin3 = new RandomAccessFile(file3,"rw");

Long len = fin1.length();

while((ch = fin1.read()) != -1)


{
fin3.write(ch);
}

fin3.seek(len+2);

while((ch = fin2.read()) != -1)


{
fin3.write(ch);
}

}catch(IOException e)
{
System.out.println(e);
}
}
}
Question#06
Q. Write a Java Program to read the content from file and copy the odd number of lines in odd.txt and
even number of lines in even.txt.
Solution :
Program Name : ODD_EVEN_File.java
import java.io.*;

public class ODD_EVEN_File


{
public static void main(String args[])
{
FileReader in = null;
BufferedReader bin = null;
FileWriter fout1, fout2;
PrintWriter bout1, bout2;
String str;
int cnt = 0;

try
{
in = new FileReader("abc.txt");
bin = new BufferedReader(in);
fout1 = new FileWriter("even.txt");
fout2 = new FileWriter("odd.txt");
bout1 = new PrintWriter(fout1);
bout2 = new PrintWriter(fout2);

while((str= bin.readLine()) != null)


{
cnt++;
if(cnt % 2 == 0)
{
bout1.println(str);
}
else
{
bout2.println(str);
}

bout1.close();
bout2.close();

}catch(IOException e)
{
System.out.println(e);
}
}
}
Question#07
Q. Write a Java Program which creates the connection between the Client and Server and ones it establish
then server will send the welcome message to the client.
Solution :
1. Program Name : ClientSide.java
import java.io.*;
import java.net.*;
public class ClientSide
{
public static void main(String args[])
{
Socket soc = null;
BufferedReader sin = null;
PrintWriter out = null;

try
{
soc = new Socket("127.0.0.1",7676);

sin = new BufferedReader(new InputStreamReader(soc.getInputStream()));


out = new PrintWriter(new OutputStreamWriter(soc.getOutputStream()));

String str = sin.readLine();


System.out.println(str);

out.println("Data Received");
out.flush();

}catch(Exception e)
{
System.out.println(e);
}
}
}
2. Program Name : ServerSide.java
import java.io.*;
import java.net.*;

public class ServerSide


{
public static void main(String args[])
{
ServerSocket ssoc = null;
Socket soc = null;
BufferedReader cin = null;
PrintWriter out = null;

try
{
ssoc = new ServerSocket(7676);
System.out.println("\n Server has started .....");

while(true)
{
soc = ssoc.accept();
if(soc != null)
{
System.out.println("\n Client has called ..."+ soc);
}

out = new PrintWriter(new OutputStreamWriter(soc.getOutputStream()));


out.println("Welcome to Java Networking");
out.flush();

cin = new BufferedReader(new InputStreamReader(soc.getInputStream()));


String str = cin.readLine();
System.out.println(str);

}
}catch(Exception e)
{
System.out.println(e);
}
}
}
Question#08
Q. Write a Serverside & Clientside Java Program to implement the Echo Server.
Solution :
1. Program Name : EchoClient.java
import java.io.*;
import java.net.*;
public class EchoClient
{
public static void main(String args[])
{
Socket soc = null;
BufferedReader bin = null;
BufferedReader sin = null;
PrintWriter out = null;

try
{
soc = new Socket("127.0.0.1",7676);

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


sin = new BufferedReader(new InputStreamReader(soc.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(soc.getOutputStream()));

System.out.println("\n Enter the Message :");


String str = bin.readLine();

out.println(str);
out.flush();
String str1 = sin.readLine();
System.out.println(str1);

}catch(Exception e)
{
System.out.println(e);
}
}
}
2. Program Name : EchoServer.java
import java.io.*;
import java.net.*;

public class EchoServer


{
public static void main(String args[])
{
ServerSocket ssoc = null;
Socket soc = null;
BufferedReader cin = null;
PrintWriter out = null;

try
{
ssoc = new ServerSocket(7676);
System.out.println(“\n Server has started …..”);

while(true)
{
soc = ssoc.accept();
if(soc != null)
{
System.out.println(“\n Client has called …”+ soc);
}

cin = new BufferedReader(new InputStreamReader(soc.getInputStream()));


String str = cin.readLine();
System.out.println(str);

out = new PrintWriter(new OutputStreamWriter(soc.getOutputStream()));


out.println(str);
out.flush();

}
}catch(Exception e)
{
System.out.println(e);
}
}
}
Question#09
Q. Write a Serverside & Clientside Java Program implement the Chat Server.
Solution :
1. Program Name : ChatClient.java
import java.io.*;
import java.net.*;
public class ChatClient
{
public static void main(String args[])
{
Socket soc = null;
BufferedReader bin = null;
BufferedReader sin = null;
PrintWriter out = null;

try
{
soc = new Socket("127.0.0.1",7676);

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


sin = new BufferedReader(new InputStreamReader(soc.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(soc.getOutputStream()));

System.out.println("\n Enter the Message :");


String str= bin.readLine();

out.println(str);
out.flush();

String str1 = sin.readLine();


System.out.println(str1);

}catch(Exception e)
{
System.out.println(e);
}
}
}
2. Program Name : ChatServer.java
import java.io.*;
import java.net.*;

public class ChatServer


{
public static void main(String args[])
{
ServerSocket ssoc = null;
Socket soc = null;
BufferedReader cin = null;
BufferedReader bin = null;
PrintWriter out = null;

try
{
ssoc = new ServerSocket(7676);
System.out.println("\n Server has started .....");

while(true)
{
soc = ssoc.accept();
if(soc != null)
{
System.out.println("\n Client has called ..."+ soc);
}

cin = new BufferedReader(new InputStreamReader(soc.getInputStream()));


String str = cin.readLine();
System.out.println(str);

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


System.out.println("\n Enter the Message ::");
String str1 = bin.readLine();

out = new PrintWriter(new OutputStreamWriter(soc.getOutputStream()));


out.println(str1);
out.flush();

}
}catch(Exception e)
{
System.out.println(e);
}
}
}
Question#10
Q. Write a Serverside & Clientside Java Program implement the File Server.
Solution :
1. Program Name : FileClient.java
import java.io.*;
import java.net.*;
public class FileClient
{
public static void main(String args[])
{
Socket soc = null;
BufferedReader bin = null;
PrintWriter out = null;
try
{
soc = new Socket("127.0.0.1",7676);

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


out = new PrintWriter(new OutputStreamWriter(soc.getOutputStream()));

System.out.println("\n Enter the FileName :");


String fname = bin.readLine();

out.println(fname);
out.flush();

}catch(Exception e)
{
System.out.println(e);
}
}
}
2. Program Name : FileServer.java
import java.io.*;
import java.net.*;

public class FileServer


{
public static void main(String args[])
{
ServerSocket ssoc = null;
Socket soc = null;
BufferedReader cin = null;
BufferedReader fin = null;
String str;
try
{
ssoc = new ServerSocket(7676);
System.out.println("\n Server has started .....");

while(true)
{
soc = ssoc.accept();
if(soc != null)
{
System.out.println("\n Client has called ..."+ soc);
}

cin = new BufferedReader(new InputStreamReader(soc.getInputStream()));


String fname = cin.readLine();

fin = new BufferedReader(new FileReader(fname));

while((str = fin.readLine()) != null)


{
System.out.println(str);
}

}
}catch(Exception e)
{
System.out.println(e);
}
}
}
Question#11
Q. Write Java Program to read the content from the table and display it on Console
Solution :
Program Name : DisplayData.java
import java.sql.*;

public class DisplayData


{
public static void main(String args[])
{
Connection con = null;
Statement stmt = null;
ResultSet rs = null;

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDrriver");
con = DriverManager.getConnection("jdbc.odbc.STUD");
stmt = con.createStatement();
rs = stmt.executeQuery("Select * from Student");
System.out.println("\n RollNo.\t Name \t Marks");
while(rs.next())
{
System.out.println("\n "+ rs.getInt("rno")+"\t"+ rs.getString("name")+"\t"+
rs.getInt("Marks"));
}
}catch(ClassNotFoundException e)
{
System.out.println(e);
}catch(SQLException e)
{
System.out.println(e);
}
}
}
Question#12
Q. Write a Java Program to read the values from the user and update the same in database.
Solution :
Program Name : UpdateData.java
import java.sql.*;
import java.io.*;

public class UpdateData


{
public static void main(String args[])
{
Connection con = null;
Statement stmt = null;
InputStreamReader in = null;
BufferedReader bin = null;
String name;
int num;

try
{
in = new InputStreamReader(System.in);
bin = new BufferedReader(in);
System.out.println("\n Enter The Roll No. of Student");
num = Integer.parseInt(bin.readLine());

System.out.println("\n Enter the Name of Student");


name = (bin.readLine());

Class.forName("sun.jdbc.odbc.JdbcOdbcDrriver");
con = DriverManager.getConnection("jdbc.odbc.STUD");
stmt = con.createStatement();
stmt.executeUpdate("Update Student set name = "+name+" where rno ="+ num);
System.out.println("\n Record Updated");

}catch(IOException e)
{
System.out.println(e);
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}catch(SQLException e)
{
System.out.println(e);
}
}
}
Question#13
Q. Write a Java Program to read the value from the command line argument and delete the same record
from the database.
Solution :
Program Name : DeleteData.java
import java.sql.*;
import java.io.*;
public class DeleteData
{
public static void main(String args[])
{
Connection con = null;
Statement stmt = null;
int n;

try
{
n = Integer.parseInt(args[0]);
Class.forName("sun.jdbc.odbc.JdbcOdbcDrriver");
con = DriverManager.getConnection("jdbc.odbc.EMP");
stmt = con.createStatement();
stmt.executeUpdate("delete Employee where eno ="+ n);
System.out.println("\n Record Deleted");

}catch(ClassNotFoundException e)
{
System.out.println(e);
}catch(SQLException e)
{
System.out.println(e);
}
}
}
Question#14
Q. Write a Java Program to insert the data into database by using prepared statement.
Solution :
Program Name : InsertData.java
import java.sql.*;
import java.io.*;

public class InsertData


{
public static void main(String args[])
{
Connection con = null;
PreparedStatement pstmt = null;

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDrriver");
con = DriverManager.getConnection("jdbc.odbc.EMP");
pstmt = con.prepareStatement("Insert into Employee values(?,?,?)");
pstmt.setInt(1,102);
pstmt.setString(2,"Ramesh");
pstmt.setInt(3,15000);
pstmt.executeUpdate();
System.out.println("\n Record Inserted");

}catch(ClassNotFoundException e)
{
System.out.println(e);
}catch(SQLException e)
{
System.out.println(e);
}
}
}
Question#15
Q. Write Java Program to build RMI application which sends welcome message to the user.
Solution :
1. Program Name : MsgRemote.java
import java.rmi.*;
import java.rmi.server.*;

public interface MsgRemote extends Remote


{
public String getMsg() throws RemoteException
}
2. Program Name : MsgRO.java
import java.rmi.*;
import java.rmi.server.*;

public class MsgRO extends UnicastRemoteObject implements MsgRemote


{
MsgRO()throws RemoteException
{}

public String getMsg() throws RemoteException


{
return("Hello Bunty");
}
}
3. Program Name : MsgServer.java
import java.rmi.*;
import java.rmi.server.*;

public class MsgServer


{
public static void main(String args[])
{
try
{
MsgRO obj = new MsgRO();
Naming.rebind("/msg",obj);
System.out.println("\n Object is bind to /msg");
}catch(Exception e)
{
System.out.println(e);
}
}
}
4. Program Name : MsgClient.java
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.RMISecurityManager;

public class MsgClient


{
public static void main(String args[])
{

try
{
MsgRemote obj = (MsgRemote)Naming.lookup("/msg");
System.out.println(obj.getMsg());
}catch(Exception e)
{
System.out.println(e);
}
}
}

Question#16
Q. Write Java Program to build RMI application which having remote method for the calculation of sum
of two integers.
Solution :
1. Program Name : ADDRemote.java
import java.rmi.*;
import java.rmi.server.*;

public interface ADDRemote extends Remote


{
public int getSum(int n1, int n2) throws RemoteException;
}
2. Program Name : ADDRO.java
import java.rmi.*;
import java.rmi.server.*;

public class ADDRO extends UnicastRemoteObject implements ADDRemote


{
ADDRO()throws RemoteException
{}

public int getSum(int n1, int n2) throws RemoteException


{
return(n1+n2);
}
}
3. Program Name : ADDServer.java
import java.rmi.*;
import java.rmi.server.*;
public class ADDServer
{
public static void main(String args[])
{
try
{
ADDRO obj = new ADDRO();
Naming.rebind("/add",obj);
System.out.println("\n Object is bind to /add");
}catch(Exception e)
{
System.out.println(e);
}
}
}
4. Program Name : ADDClient.java
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.RMISecurityManager;

public class ADDClient


{
public static void main(String args[])
{

try
{
ADDRemote obj = (ADDRemote)Naming.lookup("/add");
System.out.println(obj.getSum(100,54));
}catch(Exception e)
{
System.out.println(e);
}
}
}

Question#17
Q. Write a Java Program to design a Java bean for the employee.
Solution :
Program Name : EMPLOYEE.java

public class EMPLOYEE


{
int eno, esal;
String ename;

EMPLOYEE(int no,String name,int sal)


{
this.eno = no;
this.ename = name;
this.esal = sal;
}

public void setENO(int n)


{
this.eno = n;
}

public int getENO()


{
return eno;
}

public void setENAME(String str)


{
this.ename = str;
}

public String getENAME()


{
return ename;
}

public void setSALARY(int amt)


{
this.esal = amt;
}

public int getSALARY()


{
return esal;
}

}
Question#18
Q. Write a simple Java Servlet Program to welcome to the user.
Solution :
Program Name : GreetUser.java
import java.io.*;
import java.servlet.*;
import java.servlet.http.*;
public class GreetUser extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,
IOException
{
res.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>Hello World</HEAD>");
out.println("<BODY>");
out.println("Weclcome to Java Servlet");
out.println("</BODY>");
out.println("</HTML>");
}
}
Question#19
Q. Write a Java Servlet Program to read the data from database and display in tabular form.
Solution :
Program Name : DisplayData.java
import java.io.*;
import java.sql.*;
import java.servlet.*;
import java.servlet.http.*;

public class DisplayData extends HttpServlet


{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,
IOException
{
res.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection con = null;
Statement stmt = null;
resultSet rs = null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:EMP");
stmt = con.createStatement();
rs = stmt.executeQuery("Select * from Employee");

out.println("<HTML>");
out.println("<HEAD>Hello World</HEAD>");
out.println("<BODY>");
out.println("<Table>");

out.println("<TR><TH>EMPNO</TH><TH>EMPNAME</TH><TH>SALARY</TH></TR>");
while(rs.next())
{
out.println("<TR><TD>");
out.println(rs.getInt("eno"));
out.println("</TD><TD>");
out.println(rs.getString("ename"));
out.println("</TD><TD>");
out.println(rs.getInt("sal"));
out.println("</TD><TR>");
}
out.println("</TABLE>");
out.println("</BODY>");
out.println("</HTML>");
}catch(ClassNotFoundException e)
{
System.out.println(e);
}catch(SQLException e)
{
System.out.println(e);
}
}
}
Question#20
Q. Write a Java Servlet Program to authenticate the login page and display appropriate message if login
fails.
Solution :
1. Program Name : login.html
<HTML>
<HEAD>
<TITLE>Insert Data Demo </TITLE>
</HEAD>
<BODY>
<H2> LOGIN FORM</H2>
<Form method="Get" action="Http://localhost:8080/Servlet/Validate.class">
<br>
<br>
User Name : <Input Type ="text" name="txtUser" size = "10">
<br>
Password : <Input Type ="text" name="txtPass" size = "10">
<br>
<Input Type="submit" name="Button" value="SUBMIT">
<Input Type="reset" name="Button" value="RESET">
</Form>
</Body>
</Html>
2. Program Name : Validate.java
import java.io.*;
import java.sql.*;
import java.servlet.*;
import java.servlet.http.*;

public class Validate extends HttpServlet


{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,
IOException
{
res.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String user = req.getParameter("txtUser"));
String pwd= req.getParameter("txtPass");
String id, pass;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:EMP");
stmt = con.createStatement();
rs = stmt.executeQuery("Select * from Employee");

while(rs.next())
{
id = rs.getString("user");
pass = rs.getString("pass");

if((user.equals(id)) && (pwd.equals(pass))


{
out.println("\n Valid User");
}
else
{
out.println("\n Invalid User");
}
}
}catch(ClassNotFoundException e)
{
System.out.println(e);
}catch(SQLException e)
{
System.out.println(e);
}
}
}
Question#21
Q. Write a Java Servlet Program to insert data into table, read the details from the user.
Solution :
1. Program Name : InsertData.html
<HTML>
</HEAD>
<TITLE>Insert Data Demo </TITLE>
</HEAD>
<BODY>
<H2> STUDENT INFORMATION</H2>
<Form method="Get" action="Http://localhost:8080/Servlet/InsertData.class">
<br>
<br>
Employee No. <Input Type ="text" name="txteno" size = "10">
<br>
Employee Name <Input Type ="text" name="txtename" size = "10">
<br>
Salary <Input Type ="text" name= "txtsalary" size = "10">
<br>
<Input Type="submit" name="Button" value="Insert">
</Form>
</Body>
</Html>
2. Program Name : InsertData.java
import java.io.*;
import java.sql.*;
import java.servlet.*;
import java.servlet.http.*;

public class InsertData extends HttpServlet


{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,
IOException
{
res.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection con = null;
Statement stmt = null;
resultSet rs = null;
int eno = Integer.parseInt(req.getParameter("eno"));
String ename = req.getParameter("ename");
int sal = Integer.parseInt(req.getParameter("sal"));
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:EMP");
stmt = con.createStatement();
stmt.executeUpdate("insert into Employee values("+ eno +","+ename+","+sal+")");
out.println("\n Data Inserted");
}catch(ClassNotFoundException e)
{
System.out.println(e);
}catch(SQLException e)
{
System.out.println(e);
}
}
}
Question#22
Q. Write a Java Servlet Program to demonstrate the use of Hidden form Filed in Session Management.
Solution :

1. Program Name : Session.html

<HTML>
<HEAD>
<TITLE>Session Management Demo</TITLE>
</HEAD>
<BODY>
<H2> USER FORM</H2>
<Form method="Get" action="Http://localhost:8080/Servlet/Intermediate.class">
<br>
<br>
User Name : <Input Type ="text" name="txtUser" size = "10">
<br>
Password : <Input Type ="text" name="txtPass" size = "10">
<br>
<Input Type="submit" name="Button" value="SUBMIT">
<Input Type="reset" name="Button" value="RESET">
</Form>
</Body>
</Html>
2. Program Name : Intermediate.java

import java.io.*;
import java.sql.*;
import java.servlet.*;
import java.servlet.http.*;

public class Intermediate extends HttpServlet


{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,
IOException
{
res.setContentType("text/html");
PrintWriter out = response.getWriter();
String user = req.getParameter("txtUser"));

out.println("<HTML>");
out.println("<HEAD>Session Management</HEAD>");
out.println("<BODY>");
out.println("Weclcome to Java Servlet");
out.println("</BODY>");
out.println("<Form method="Get" action="Http://localhost:8080/Servlet/Final.class">"):
out.println("<Input Type ="password" name="txtUser" size = "10" value="+user+">");
out.println("<Input Type="submit" name="Button" value="NEXT">"):
out.println("</Form>");
out.println("</HTML>");
}
}

3. Program Name : Final.java


import java.io.*;
import java.sql.*;
import java.servlet.*;
import java.servlet.http.*;

public class Final extends HttpServlet


{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,
IOException
{
res.setContentType("text/html");
PrintWriter out = response.getWriter();
String user = req.getParameter("txtUser"));
out.println("\n Welcome "+ user +"session management");
}}

You might also like