Final_AJP_LAB
Final_AJP_LAB
import java.util.*;
int m = sc.nextInt();
int n = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
if (n != x) {
return;
// Input matrix A
A[i][j] = sc.nextInt();
// Input matrix B
B[i][j] = sc.nextInt();
// Print matrix A
System.out.println("Matrix A:");
System.out.println();
System.out.println("Matrix B:");
System.out.println();
}
System.out.println();
}
OUTPUT:
10 20 30 40 50 60
11 22 33 44 55 66
Matrix A:
10 20 30
40 50 60
Matrix B:
11 22
33 44
55 66
2420 3080
5390 7040
2. An educational institution wishes to maintain a database of its
employees. The database is divided into no of classes whose
hierarchical relationships are shown in fig. Write a Java program
to specify all the classes and define methods to read and display
data of all the classes
Department
dname
Staff
sname, experience,salary
Teaching Non-Teaching
subject position
import java.util.Scanner;
class Department {
String dname;
dname = sc.nextLine();
System.out.print(dname + "\t\t");
String sname;
int experience;
double salary;
super.readDepartment(sc);
sname = sc.nextLine();
experience = sc.nextInt();
salary = sc.nextDouble();
sc.nextLine();
super.displayDepartment();
String subject;
super.readStaff(sc);
subject = sc.nextLine();
super.displayStaff();
System.out.println(subject);
String position;
super.readStaff(sc);
position = sc.nextLine();
super.displayStaff();
System.out.println(position);
sc.nextLine();
sc.nextLine();
nonTeachingStaffArray[i].readNonTeaching(sc);
System.out.println("Department\tStaff Name\tExperience\tSalary\t\tSubject");
System.out.println("--------------------------------------------------------------------------");
teachingStaff.displayTeaching();
System.out.println("Department\tStaff Name\tExperience\tSalary\t\tPosition");
System.out.println("--------------------------------------------------------------------------");
nonTeachingStaff.displayNonTeaching();
}
OUTPUT
--------------------------------------------------------------------------
--------------------------------------------------------------------------
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
TextField t1,t2,t3;
Label l1,l2,l3;
Button b1,b2,b3,b4;
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(20);
l3=new Label("Result:");
b1=new Button("Add");
b2=new Button("Subtract");
b3=new Button("Multiply");
b4=new Button("Divide");
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(b2);
add(b3);
add(b4);
add(l3);
add(t3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
float a,b,res=0;
a=Float.parseFloat(t1.getText());
b=Float.parseFloat(t2.getText());
String cmd=ae.getActionCommand();
if(cmd.equals("Add"))
res=a+b;
else if(cmd.equals("Subtract"))
res=a-b;
else if(cmd.equals("Multiply"))
{
res=a*b;
else
res=a/b;
String msg=String.valueOf(res);
t3.setText(msg);
repaint();
}
OUTPUT:
4. Develop a Java applet which handles mouse events by displaying
the respective event names and co-ordinates.
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
addMouseListener(this);
addMouseMotionListener(this);
mouseX=10;
mouseY=20;
msg="Mouse is clicked";
repaint();
mouseX=10;
mouseY=20;
msg="Mouse entered";
repaint();
mouseX=10;
mouseY=20;
msg="Mouse exited";
repaint();
mouseX=me.getX();
mouseY=me.getY();
msg="Down";
repaint();
mouseX=me.getX();
mouseY=me.getY();
msg="Up";
repaint();
mouseX=me.getX();
mouseY=me.getY();
repaint();
mouseX=me.getX();
mouseY=me.getY();
msg="*";
repaint();
g.drawString(msg,mouseX,mouseY);
}
OUTPUT:
5. Develop a Java servlet to accept user details entered through a
web form such as name, gender, password, feedback (through a
text area) and job category (through drop down list). Display the
same with appropriate messages.
User.html
<html>
<head>
<title></title>
</head>
<body>
Gender:
Job Category:
<option value="Science">Science</option>
<option value="Maths">Maths</option>
<option value="English">English</option>
<option value="Social">Social</option>
</select><br><br>
</form>
</body>
</html>
UserDetails.java
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;
response.setContentType("text/html;charset=UTF-8");
String n = request.getParameter("name");
String p = request.getParameter("pwd");
String g = request.getParameter("gender");
String f = request.getParameter("feedback");
String[] j = request.getParameterValues("jobcat");
try {
out.println("<html>");
out.println("<body>");
out.println(j[i]);
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
OUTPUT:
6. Develop a Java servlet to read and write cookies.
Cookies.html
<html>
<head><title>Cookies Handling</title></head>
<body>
</form>
</body>
</html>
Cookie1.java
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;
String S = request.getParameter("name");
String V = request.getParameter("pwd");
response.addCookie(myCookie);
out.println("<html><body>");
if (myCookie1 == null) {
out.println("No Cookies");
} else {
int i;
out.println("</body></html>");
}
OUTPUT:
7. Write a Java program to create a database table, insert records in it, and display
them.
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Override
response.setContentType("text/html;charset=UTF-8");
Connection db = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.exit(0);
} catch (SQLException e) {
System.exit(0);
try {
// Create table
Statement st = db.createStatement();
st.executeUpdate(query);
out.println("Table created<br>");
// Insert data
st.executeUpdate(query1);
ResultSet rs = st.executeQuery(query2);
if (!rs.next()) {
out.println("No Records found");
} else {
out.println("Name\tID<br>");
do {
int id = rs.getInt("ID");
} while (rs.next());
} catch (SQLException e) {
OUTPUT:
Table created
Name ID
xyz 1234567891
8. Write a JSP program to print in table format multiplication table
of a number entered by user.
mult.jsp:
<!DOCTYPE html>
<html>
<head>
<title>multiplication table</title>
</head>
<table border="1">
<body>
int n=Integer.parseInt(s);%>
{%>
<tr>
<th>
<%out.println(n+"x"+i);%>
</th>
<th>
<%out.println("=");%>
</th>
<th>
<%out.println(n*i);%>
</th>
</tr>
<%}%>
</table>
</body>
</html>
mult.html
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<input type="submit">
</form>
</body>
</html>
OUTPUT:
9. Write a JSP program to print Fibonacci series for a number
entered by the user.
Fibonacci.jsp
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
int n=Integer.parseInt(s);
int i=1,f1=0,f2=1,f3;
while(i<=n)
out.println(f1);
f3=f1+f2;
f1=f2;
f2=f3;
i++;
}%>
</body>
</html>
FiboSeries.html
<html>
<head>
<title>Fibo Series</title>
<meta charset="UTF-8">
</head>
<body>
<input type="submit">
</form>
</body>
</html>
OUTPUT:
10.Write a JSP program to:
ErrorDemo.html
<html>
<head>
<title>error handling</title>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
Calculation.jsp
<!DOCTYPE html>
<html>
<head>
<title>Calculation Page</title>
<meta charset="UTF-8">
<body>
%>
</body>
</html>
ErrorHandling.jsp
<html>
</head>
<body>
<%=exception%>
</body>
</html>
</html>
OUTPUT:
b) Display HTTP request headers in table form on a web page.
<!DOCTYPE html>
<html>
<head>
<title>request header</title>
</head>
<body>
<table border="1">
<%@page import="java.util.*"%>
<%Enumeration e=request.getHeaderNames();
<tr>
<th>Header Name</th>
<th>Header Value</th>
</tr>
while(e.hasMoreElements())
String Name=(String)e.nextElement();
String value=request.getHeader(Name);%>
<tr>
<td>
<%out.println(Name);%>
</td>
<td>
<%out.println(value);%>
</td>
</tr>
<%}%>
</table>
</body>
</html>
OUTPUT: