Uploaded by ISTE JCE

FINAL AJP RECORD-OCTOBER 2019

advertisement
FACULTY OF SCIENCE AND HUMANITIES
DEPARTMENT OF COMPUTER SCIENCE
KATTANKULATHUR – 603 203
LAB RECORD
NAME
:
REGISTER NUMBER
:
CLASS
:
III Year B. Sc Computer Science
DEPARTMENT
:
Computer Science
SUBJECT CODE & NAME
:
UCS15504-ADVANCED JAVA PROGRAMMING
LABORATORY
OCTOBER 2019
1
FACULTY OF SCIENCE AND HUMANITIES
DEPARTMENT OF COMPUTER SCIENCE
CERTIFICATE
This is to certify that this is a bonafide record of practical work done by
NAME
:
REGISTER NUMBER
:
CLASS
:
III Year B. Sc Computer Science
DEPARTMENT
:
Computer Science
SEMESTER, MONTH & YEAR
:
V Semester – Oct 2019
SUBJECT CODE & NAME
:
UCS15504 – ADVANCED JAVA
PROGRAMMING LABORATORY
During the Academic Year 2019 - 2020 & Submitted for B.SC Degree practical
examination held on
.
LECTURER INCHARGE
INTERNAL EXAMINER
H.O.D
EXTERNAL EXAMINER
2
INDEX
Ex.
No.
Date
Contents
Page
No.
1 25/06/2019 Displaying records From a DB Table
6
2 28/06/2019
9
3 05/07/2019
4 09/07/2019
5 12/07/2019
6 16/07/2019
7 19/07/2019
Inserting records to a database table
Updating records of a database table
Deleting records from a database table
Retrieving Metadata
Exploring InetAddress Class
TCP Date Time Server
13
16
19
24
27
8 23/07/2019 TCP Hello Server
31
9 26/07/2019
36
10 30/07/2019
11 02/08/2019
12 09/08/2019
RMI – Sum of Two Numbers
Factorial with RMI
Current Date And Time
Generating Multiplication Table
40
45
48
13 14/08/2019 HitCount of A WebPage
52
14 20/08/2019
55
15 27/08/2019
Data Insertion Using Prepared Statement
Generating prime numbers
Signature
59
3
16 04/09/2019 Message Applet
64
17 12/09/2019 Shape Applet
66
18 19/09/2019 Keyboard Events Applet
69
19 30/09/2019 Face Applet
72
4
JDBC
5
Ex.No: 1
Displaying records From a DB Table
Date: 25-06-19
Aim:To write a java program to connect and retrieve records from a database table using
a select query with the help of JDBC API.
Procedure:Step-1: Create a database using Apache’s Java DB , create a table and add a
few records.
Step-2: Start the DB Server.
Step-3: .Create a java application in net beans. Create a java class in the
source package (SRC).
Step-4: Import the SQL package and load the EmbeddedDriver class of Apache.
Create a connection between the database and the program.
Step-5: Create the Statement object; pass the select query as the parameter.
Step-6: Execute the Query and get the result into the ResultSet object.
Step-7: Traverse through the ResultSet.
Steps-8: Compile& Run the program.
6
CODING:import java.sql.*;
public class Main {
public static void main(String[] args) {
String n;
int r;
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver ");
Connection con= DriverManager.getConnection("jdbc:derby://localhost:1527/Student",
"nbuser","nbuser" );
Statement st=con.createStatement();
ResultSet rs=con.executeQuery("select * from student");
while(rs.next()){
n=rs.getString("student name");
r=rs.getInt("regno");
System.out.println("Name "+n+"\t "+"regno "+r);
}
con.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
7
OUTPUT:-
Result:Thus the result is displayed.
8
Ex.No: 2
Inserting records to a database table
Date: 28-06-19
Aim:To write a java program to connect and insert records in database table with the
help of JDBC API.
Procedure:Step-1: Create a database using Apache’s Java DB , create a table and add a
few records.
Step-2: Start the DB Server.
Step-3: Create a java application in net beans. Create a java class in the
source package (SRC).
Step-4: Import the SQL package and load the EmbeddedDriver.
Create a connection between the database and the program.
Step-5: Create the Prepared Statement object; pass the insert query as
the parameter.
Step-6: Execute the executeUpdate() method.
Steps-7: Compile& Run the program.
9
CODING:import java.sql.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
String studname,gender,dept;
int regno;
Scanner in=new Scanner(System.in);
System.out.println("Enter student name:");
studname=in.nextLine();
System.out.println("Enter department:");
dept=in.nextLine();
System.out.println("Enter gender:");
gender=in.nextLine();
System.out.println("Enter reg number:");
regno=in.nextInt();
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection con=DriverManager.getConnection(
"jdbc:derby://localhost:1527/Student","nbuser","nbuser");
PreparedStatement st=con.prepareStatement("Insert into
student (studname,regno,dept,gender) values(?,?,?,?)");
st.setString(1,studname);
st.setInt(2,regno);
st.setString(3,dept);
st.setString(4,gender);
st.executeUpdate();
10
System.out.println("updated");
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
11
OUTPUT:-
Result:Thus the result is displayed.
12
Ex.No:3
Updating records of a database table
Date: 05-07-19
Aim:To write a java program to connect and update records of a database table with
the help of JDBC API.
Procedure:Step-1: Create a database using Apache’s Java DB, create a table and add a few
records.
Step-2: Start the DB Server.
Step-3: Create a java application in net beans. Create a java class in the
source package (SRC).
Step-4: Import the SQL package and load the EmbeddedDriver. Create a connection
between the database and the program.
Step-5: Create the Prepared Statement object; pass the update query as
the parameter.
Step-6: Execute the executeUpdate() method.
Steps-7: Compile& Run the program.
13
CODING:import java.sql.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
int rno, newaddr;
Scanner in=new Scanner(System.in);
System.out.println("Enter the reg number");
rno=in.nextInt();
System.out.println("Enter the contact number");
newaddr=in.nextInt();
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver ");
Connection
con=DriverManager.getConnection("jdbc:derby://localhost:1527/Student","nbuser","nb
user");
PreparedStatement pst=con.prepareStatement("Update student set contact=?
where regno=? ");
pst.setInt(1,newaddr);
pst.setInt(2,rno);
pst.executeUpdate();
System.out.println("updated");
con.close();
} catch(Exception e){
e.printStackTrace();
}
}
}
14
OUTPUT:-
Result:Thus the result is displayed.
15
Ex.No: 4
Deleting records from a database table
Date: 09 -07-19
Aim:To write a java program to connect and delete records from a database with the
help of JDBC API.
Procedure:Step-1: Create a database using Apache’s Java DB, create a table and add a few
records.
Step-2: Start the DB Server.
Step-3: Create a java application in net beans. Create a java class in the
source package (SRC).
Step-4: Import the SQL package and load the EmbededDriver. Create a connection
between the database and the program.
Step-5: Create the Prepared Statement object; pass the update query as
the parameter.
Step-6: Execute the executeUpdate() method.
Steps-7: Compile& Run the program.
16
CODING:import java.sql.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
int rno;
Scanner in=new Scanner(System.in);
System.out.println("enter reg no:");
rno=in.nextInt();
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver ");
Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/Student",
"nbuser","nbuser ");
PreparedStatement st=con.prepareStatement("Delete from student
where regno=?");
st.setInt(1,rno);
st.executeUpdate();
System.out.println("deleted");
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
17
OUTPUT:-
Result:Thus the result is displayed.
18
Ex.No: 5
Retrieving Metadata
Date: 12-07-19
Aim:To write a java program to connect and retrieve Metadata from a database using
a select query with the help of JDBC API.
Procedure:Step-1: Create a database using Apache’s Java DB,create a table and add a few
records.
Step-2: Configure the DSN.
Step-3: .Create a java application in net beans. Create a java class in the
source package (SRC).
Step-4: Import the SQL package and load the EmbeddedDriver. And create a
connection between the database and the program.
Step-5: Create the DatabaseMetaData object and use its built in methods to
display the product name, product Name and major version respectively.
Step-6: Get result into the ResultSets object.
Step-7: Traverse through the ResultSets.
Steps-8: Compile& Run the program.
19
CODING:import java.sql.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
String catalog=null;
String schema=null;
String mytable=null;
String[] types=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(
"jdbc:derby://localhost:1527/Student","nbuser","nbuser ");
DatabaseMetaData dm = con.getMetaData();
System.out.println("Product name:"+ dm.getDatabaseProductName());
System.out.println("Product version:"+ dm.getDatabaseProductVersion());
System.out.println("Driver version:"+ dm.getDriverMajorVersion());
ResultSet rs= dm.getTables(catalog,schema,mytable,types);
while(rs.next())
{
System.out.println(rs.getString(3));
}
Scanner in=new Scanner(System.in);
System.out.println("Any specific table?");
mytable=in.nextLine();
ResultSet rs2=su.getTables(catalog,schema,mytable,types);
20
while(rs2.next())
{
System.out.println(rs2.getString(4));
System.out.println(rs2.getInt(5));
}
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
21
OUTPUT:-
Result:Thus the result is displayed.
22
NETWORKING
23
Ex.No: 6
Exploring InetAddress Class
Date: 16-07-19
Aim:To write a java program to IP of a few given sites using InetAddress class.
Procedure:Step-1: Create a java application in net beans. Create a java class in the
source package (SRC).
Step-2: Import the net package.
Step-3: Create the InetAddress objects and use getLocalHost (), getAllByName()
and getByName () to retrieve and display the ip addresses.
Step-4: Traverse through the maddr[] array for getAllByName().
Steps-5: Compile& Run the program.
24
CODING:import java.net.*;
public class Main {
public static void main(String[] args) {
try
{
InetAddress addr=InetAddress.getLocalHost();
System.out.println("local"+addr);
addr=InetAddress.getByName("www.srmuniv.ac.in");
System.out.println(addr);
InetAddress maddr[]=InetAddress.getAllByName("www.gmail.com");
for(int i=0;i<maddr.length;i++)
System.out.println(maddr[i]);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
25
OUTPUT:-
Result:Thus the result is displayed.
26
Ex.No:7
TCP Date Time Server
Date: 19-07-19
Aim:To write a TCP server application that will give the current date and time when
it receives a client request.
Procedure:Step-1: Create a java application in net beans. Create a java class in the
source package (SRC).
Step-2:Import the net, util and io package.
Step-3: Write the server program in which create a server socket such that
takes the request and returns the current date and time.
Step-4: Create a java application in net beans. Create a java class in the
source package (SRC).
Step-5:Import the net and io package.
Step-6: Write the client program in which create socket such that takes the
request from the local host and displays it.
Step-7: Compile both the server and client program.
Step-8: Run the server program first and then the client program.
27
CODING:SERVER CODE:import java.io.*;
import java.net.*;
import java.util.*;
public class Main {
public static void main(String[] args)throws IOException
{ ServerSocket server=new ServerSocket(8888);
while(true)
{
System.out.println("Waiting for client to
connect.."); Socket socket=server.accept();
PrintWriter out=new
PrintWriter(socket.getOutputStream(),true);
out.println("Hello"+new Date()); }
}
}
28
CLIENT CODE:import java.io.*;
import java.net.*;
public class Main {
public static void main(String[] args) {
try {
Socket socket=new Socket("localhost",8888);
BufferedReader in=new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String msg=in.readLine();
System.out.println("Server"+msg);
}
catch(IOException ioe)
{ioe.printStackTrace();}
}
}
29
OUTPUT:SERVER:-
CLIENT:-
Result:Thus the result is displayed.
30
Ex.No: 8
TCP Hello Server
Date: 23-07-`19
Aim:To write a TCP server that display a welcome message to a client
Procedure:Step-1: Create a java application in net beans. Create a java class in the
source package (SRC).
Step-2:Import the net, util and io package.
Step-3: Write the server program in which create a server socket such that
takes the request and returns the current date and time.
Step-4: Create a java application in net beans. Create a java class in the
source package (SRC).
Step-5:Import the net and io package.
Step-6: Write the client program in which create socket such that takes the
request from the local host and displays it.
Step-7: Compile both the server and client program.
Step-8: Run the server program first and then the client program.
31
CODING:SERVER CODE:-
package HelloSocket;
import java.io.*;
import java.net.*;
import java.util.*;
public class HelloServer {
public static void main(String[] args) throws Exception {
ServerSocket serv = new ServerSocket(8888);
while(true)
{
System.out.println("Waiting for Server");
Socket s = serv.accept();
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
pw.println("Hello World");
}}
}
32
CLIENT CODE:-
package HelloSocket;
import java.io.*;
import java.net.*;
public class HelloClient {
public static void main(String[] args)throws Exception {
Socket s = new Socket("LocalHost", 8888);
BufferedReader in = new BufferedReader(new
InputStreamReader(s.getInputStream()));
String msg = in.readLine();
System.out.println("from Server:" + msg);
}
}
\\
33
OUTPUT:-
Result:Thus the result is displayed.
34
RMI
-REMOTE METHOD INVOCATION
35
Ex.No: 9
RMI – Sum of Two Numbers
Date: 26-07-19
Aim:To write a java programs in which a server and client communicate with each
other through RMI and calculate the sum of the 2 given numbers.
Procedure:Step-1: Create a java application in net beans. Create a java interface in the
source package (SRC).
Step-2: Write the interface which extends the Remote of the rmi package.
Step-3: Create a java class in the source package (SRC) and Import the
rmi, rmi.registry and rmi.server package.
Step-4: Write the server program and create the registry in which implements
the interface and extends UnicatRemoteObject.
Step-5: Create a java class in the source package (SRC) and import the rmi
and util package.
Step-6: Write the client program in which creates the object of the interface
and call the function with the object.
Step-7: Compile the interface, server and client program.
Step-8: Run the server program first and then the client program.
36
CODING:REMOTE INTERFACE:import java.rmi.*;
public interface serinterface extends Remote
{
public int add(int x,int y)throws RemoteException;
}
SERVER CODE:import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class ImplAdd extends UnicastRemoteObject implements serinterface
{ public ImplAdd() throws RemoteException
{
super();
}
public int add(int x,int y)throws RemoteException
{
return x+y;
}
public static void main(String[] args)throws RemoteException
{ ImplAdd obj=new ImplAdd();
Registry r=java.rmi.registry.LocateRegistry.createRegistry(1099);
r.rebind("server",obj);
System.out.println("Ready .....");
}
}
37
CLIENT CODE:import java.rmi.*;
import java.util.*;
import java.rmi.Naming;
public class rmiclient {
public rmiclient() {
}
public static void main(String[] args){
try
{
serinterface
obj=(serinterface)Naming.lookup("server"); int a,b;
Scanner in=new Scanner(System.in);
System.out.println("Enter 2 number:");
a=in.nextInt();
b=in.nextInt();
int sum=obj.add(a,b);
System.out.println("Sum="+sum);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
38
OUTPUT:SERVER :-
CLIENT:-
Result:Thus the result is displayed.
39
Ex.No: 10
Factorial with RMI
Date: 30-07-19
Aim:To write a java programs in which a server and client communicate with each
other through RMI and calculate the factorial.
Procedure:Step-1: Create a java application in net beans. Create a java interface in the
source package (SRC).
Step-2: Write the interface which extends the Remote of the rmi package.
Step-3: Create a java class in the source package (SRC) and Import the
rmi, rmi.registry and rmi.server package.
Step-4: Write the server program and create the registry in which implements
the interface and extends UnicatRemoteObject.
Step-5: Create a java class in the source package (SRC) and import the rmi
and util package.
Step-6: Write the client program in which creates the object of the interface
and call the function with the object.
Step-7: Compile the interface, server and client program.
Step-8: Run the server program first and then the client program.
40
CODING:INTERFACE CODE:import java.rmi.*;
public interface ser extends Remote {
public int fact(int x)throws RemoteException;
}
SERVER CODE:import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class serf extends UnicastRemoteObject implements
ser{ public serf()throws RemoteException { super();
}
public int fact(int x)throws RemoteException
{
if(x==1)
return x;
else
{
int f=1;
for(int i=1;x>i;i++)
{
f*=i;
}
41
return f;
}
}
public static void main(String[] args)throws Exception
{
serf obj=new serf();
Registry r=java.rmi.registry.LocateRegistry.createRegistry(1099);
r.rebind("server",obj);
System.out.println("Ready...");
}
}
CLIENT CODE:import java.rmi.*;
import java.util.*;
import java.rmi.Naming;
public class rmiclient {
public static void main(String[] args) throws
Exception{ ser obj=(ser)Naming.lookup("server");
int a;
Scanner in=new Scanner(System.in);
System.out.println("Enter one number:");
a=in.nextInt();
int fac=obj.fact(a);
System.out.println("Factroial="+fac);
}
public rmiclient() {
}
42
}
OUTPUT:SERVER :-
CLIENT:-
Result:Thus the result is displayed.
43
SERVLETS
44
Ex.No: 11
Current Date And Time
Date: 02-08-19
Aim:To write a Servlet that will display the current date and time.
Procedure:Steps-1. Create a new servlet.
Steps-2. Import the HTTPServlet, util and text packages.
Steps-3. In the processRequest() method declare a object of Date class
and DateFormat class.
Steps-4. Using the format() function print the date.
Steps-5. Compile& Run the program..Current Date will be displayed
on the browser.
45
CODING:import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class tservlet extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF8"); PrintWriter out = response.getWriter();
out.println("Date:"+new Date());
out.close();
}
}
46
OUTPUT:-
Result:Thus the result is displayed.
47
Ex.No: 12
Generating Multiplication Table
Date: 09-08-19
Aim:To write a servlet that displays multiplication table for the number given by user.
Algorithm:Step-1: Create a HTML file that displays a single text box and a Submit button.
Step-2: Create a new servlet.
Step-3: Run the servlet and get its URL, put the URL of the servlet in the
forms action attribute.
Step-4: In the processRequest method of servlet get the value entered in
the HTML form by the user using getParameter() method.
Step-5: Convert the value into integer value and generate multiplication table.
Steps-6: Compile the Servlet.
Step-7: Run the HTM file enter any value and submit, multiplication table for
the value will be printed on browser.
48
CODING:JAVA CODE:import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class mulservlet extends HttpServlet {
public void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF8"); PrintWriter out = response.getWriter();
int n=Integer.parseInt(request.getParameter("txt1"));
for(int i=1;i<=12;i++)
{
out.println(n+"*"+i+"="+n*i);
out.println("<br>");
}
}}
49
HTML CODE:<html>
<head>
<title>test</title>
</head>
<body>
enter any number:
<form method="get" action="http://localhost:8084/mulservlet/mulservlet">
<input type="text" name="txt1" />
<br>
<input type="submit" name="submit"
value="submit"/> </form>
</body>
</html>
50
OUTPUT:-
Result:Thus the result is displayed.
51
Ex.No: 13
HitCount of A WebPage
Date: 14-08-19
Aim:To Write a Servlet that counts the no of times a web page has
been refreshed/reloaded
Procedure:Step-1: Create a new servlet.
Step-2: Create an object of the HttpSession class and get the session.
Step-3: Declare a new integer variable called hit and read/store the
session attribute “hitcount”.
Step-4: If hit is null make its value as 1 and put it back to the session
else increment hit and put it back to the session.
Steps-5: Print the value of hit and the session id.
Step-6 : Compile & run the program out put will be printed on browser and
every time the browser is refreshed a new hit value will be displayed.
52
CODING:-
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class hitman extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF8"); PrintWriter out = response.getWriter();
HttpSession ses=request.getSession(true);
Integer cnt=(Integer)ses.getValue("hit");
if(cnt==null)
{cnt=new Integer(1);}
else
cnt=new Integer(cnt.intValue()+1);
ses.putValue("hit",cnt);
out.println("session id:"+ses.getId()+"<br>");
out.println("page view:"+cnt+"times");
out.close();
}
}
53
OUTPUT:-
Result:Thus the result is displayed.
54
Ex.No: 14
Data Insertion Using Prepared Statement
Date: 20-08-19
Aim:To write a servlet program to insert data into database with the help of JDBC
and tabulate the information.
Procedure:Step-1: Create a HTML form and add controls for getting information from
the user.
Step-2: Give the URL of the servlet that has to process the information as
the parameter for the forms action tag.
Step-3: Create a new Servlet. Import the HttpServlet and the java.sql package.
Step-4: Load the sun.jdbc.odbc.JdbcOdbcDriver.
Step-5: Create a connection to the DB, with the help of the DSN created.
Step-6: Create the Statement object.
Step-7: Based on the action requested by the user execute the Insert or Delete
or Select Query with the help of the Statement object created.
Step-8: If the query executed was a Select query then get the result into
the ResultSet object.
Step-9: Traverse through the ResultSet.
Step-10: Print the records one by one in the table.
55
CODING:JAVA CODE:import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class inser extends HttpServlet {
public void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF8"); PrintWriter out = response.getWriter();
int
n=Integer.parseInt(request.getParameter("txt2"));
String na=request.getParameter("txt1"); try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection nu=DriverManager.getConnection("jdbc:odbc:fuckservlet");
PreparedStatement bi=nu.prepareStatement("Insert
into student(studname,regno) values(?,?);");
bi.setString(1,na);
bi.setInt(2,n);
bi.executeUpdate();
nu.close();
}
56
catch(Exception e)
{
e.printStackTrace();
}
out.close();
}
}
HTML CODE:<html>
<head>
<title>Insert</title>
</head>
<body>
<form method="get" action="http://localhost:8084/insertservlet/inser">
Enter name:
<input type="text" name="txt1" />
<br>
Enter regno:
<input type="text" name="txt2" />
<br>
<input type="submit" name="submit"
value="submit"/> </form>
</body>
</html>
57
OUTPUT:-
Result:Thus the result is displayed.
58
Ex.No: 15
Generating prime numbers
Date:27-08-19
Aim:To write a servlet that displays prime for the number given by user.
Algorithm:Step-1: Create a HTML file that displays a single text box and a Submit button.
Step-2: Create a new servlet.
Step-3: Run the servlet and get its URL, put the URL of the servlet in the
forms action attribute.
Step-4: In the processRequest method of servlet get the value entered in
the HTML form by the user using getParameter() method.
Step-5: Convert the value into integer value and generate multiplication table.
Steps-6: Compile the Servlet.
Step-7: Run the HTM file enter any value and submit, multiplication table for
the value will be printed on browser.
59
CODING:JAVA CODE:import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class pser extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int p=0;
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
int n=Integer.parseInt(request.getParameter("txt1"));
for(int i=1;i<n;i++) {
for(int j=2;j<i;j++) {
p=i%j;
if(p==0){
break;
}
}
if(p==1){
out.println(i + " ");
}
}
out.close();
}
}
60
HTML CODE:<html>
<head>
<title>test</title>
</head>
<body>
enter any number:
<form method="post" action="http://localhost:8084/primeservlet/pser">
<input type="text" name="txt1" />
<br>
<input type="submit" name="submit"
value="submit"/> </form>
</body>
</html>
61
OUTPUT:-
Result:Thus the result is displayed.
62
APPLET
63
Ex.No:16
Message Applet
Date:04-09-19
Aim:-
To write an Applet that will display a simple message
Algorithm:-
Step -1: Create a class called SimpleApplet which extends the Applet class.
Step -2: Import the java.applet, java.awt packages.
Step -3: In the init() method set the required background and foreground color.
Step -4: Inside the paint() method of the applet use the drawstring() method to
display string of your choice.
Step -5: Compile the class and use the appletviewer to view the output.
64
CODING:-
package applettest;
import java.awt.*;
import java.applet.Applet;
public class Simple extends Applet {
String str="Welcome to SRM University.";
public void init() {
setBackground(Color.orange);
setForeground(Color.blue);
}
public void paint(Graphics g){
g.drawString(str,10,100);
}
}
OUTPUT:-
RESULT :The applet is displayed with the welcome message
65
Ex.No : 17
Date : 12-09-19
Shape Applet
AIM:
To write an Applet that will display a different shapes and lines.
PROCEDURE:
Step 1: Create a class called ShapeApplet which extends the Applet class.
Step 2: Import the java.applet, java.awt packages.
Step 3: Inside the paint() method of the applet use the various built in methods of
the Graphics class to display Shapes.
Step 4: Compile the class and use the appletviewer to view the output.
66
CODE:package applettest;
import java.applet.Applet;
import java.awt.*;
public class ShapeApplet extends Applet {
public void paint(Graphics g)
{
g.drawLine(10,10,50,50);
g.drawRect(10,60,40,30);
g.setColor(Color.blue);
g.fillRect(60,10,30,80);
g.setColor(Color.green);
g.drawRoundRect(10,100,80,50,10,10);
g.setColor(Color.yellow);
g.fillRoundRect(20,110,60,30,5,5);
g.setColor(Color.red);
g.drawLine(100,10,230,140);
g.drawLine(100,140,230,10);
g.drawString("Line Rectangles Demo",65,180);
g.drawOval(230,10,200,150);
g.setColor(Color.blue);
g.fillOval(245,25,100,100);
}
}
67
OUTPUT:-
RESULT :
The applet is displayed with different geometrical shapes
68
Ex.No : 18
Date : 19-09-19
Keyboard Events Applet
AIM:To write an Applet that will respond to all the key press events.
PROCEDURE:Step 1: Create a class which extends the Applet class and implements the
KeyBoard listener Interface.
Step 2: Import the java.applet, java.awt packages.
Step 3: Inside the init() method of the applet add the listeners to capture all
keyboard events.
Step 4: Override all the KeyBoard Listener methods.
Step 5: Inside these methods required information can be displayed in the status
bar of the applet using the showStatus() method and also paint method
can be invoked using the repaint() method.
Step 6: Compile the class and use the appletviewer to view the output.
69
CODE :-
public class EventApplet extends Applet implements KeyListener{
int X=20,Y=30;
String msg="";
public void init()
{
addKeyListener(this);
requestFocus();
setBackground(Color.green);
setForeground(Color.blue);
}
public void keyPressed(KeyEvent k)
{
showStatus("KeyDown");
}
public void keyReleased(KeyEvent k)
{
showStatus("Key Up");
}
public void keyTyped(KeyEvent k)
{
msg+=k.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}
}
70
OUTPUT :-
RESULT :The applet that handles keyboard events is successfully executed.
71
Ex.No : 19
Date : 30-09-19
Face Applet
AIM:To write an Applet that will display a face.
PROCEDURE:Step 1: Create a class which extends the Applet class.
Step 2: Import the java.applet, java.awt packages.
Step 3: Inside the paint() method of the applet use the various built in methods of
the Graphics class to display the required image.
Step 4: Compile the class and use the appletviewer to view the output.
72
CODE :import java.applet.Applet;
import java.awt.Graphics;
public class FaceAppl extends Applet {
public void paint(Graphics g){
g.drawOval(40,40,120,150); //Head
g.drawOval(57,75,30,20); //Left eye
g.drawOval(110,75,30,20); //Right eye
g.fillOval(68,81,10,10); //Pupil (left)
g.fillOval(121,81,10,10); //Pupil (right)
g.drawOval(85,100,30,30); //Nose
g.fillArc(60,125,80,40,180,180); //Mouth
g.drawOval(25,92,15,30); //Left ear
g.drawOval(160,92,15,30); //Right ear
}
}
OUTPUT :-
RESULT :The applet is successfully executed.
73
74
Download