Professional Documents
Culture Documents
Ajp-16 11 2022
Ajp-16 11 2022
Register No:
Subject code: P22IT1P1
2.
INDEX
S. No Date Content Page No Signature
Date: 27/08/2022 Create an applet that takes age of the user in a Text Field using a Scroll bar.
Aim:
To write a program an applet that takes age of the user as an input in a textfield using scrollbar.
Algorithm:
Step 1: Start the program
Step 2: Open the notepad
Step 3: Import the header file for applet program
import java. applet. *;
Step 4: Create a Label, TextField and Scrollbar controls.
Step 5: User age is getted by scrollbar.
Step 6: TextField control is used to display the value which is selected in a scrollbar.
Step 7: Save the file using java extension and run the java program in command prompt by
using the following coding:
javac Scroll.java
appletviewer Scroll.java
Step 8: End the program
Program Coding:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="Scroll" height="300" width="300">
</applet>
*/
public class Scroll extends Applet implements AdjustmentListener
{
Label u =new Label("select your age by using Scrollbar:")
TextField t =new TextField(10);
Scrollbar s = new Scrollbar(Scrollbar.VERTICAL, 50, 0, 0, 100);
public void init()
{
add(u);
add(t);
add(s);
s.addAdjustmentListener(this);
t.setEditable(false);
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
t.setText(s.getValue()+"");
}
}
Output:
Result:
Thus the program was executed successfully and the output was verified.
Ex no:2
Date:02/09/2022 Create an exception called Array out of bounds and throw the exception
Aim:
To Write a program to create an exception called Array Out of Bounds and Throw the Exception.
Algorithm:
Program Coding:
import java.util.Arrays;
import java.util.Scanner;
public class AIOBSampleHandled
{
public static void main(String args[])
{
int[] myArray = {897, 56, 78, 90, 12, 123, 75};
System.out.println("Elements in the array are: ");
System.out.println(Arrays.toString(myArray));
Scanner sc = new Scanner(System.in);
System.out.println("Enter the index of the required element :");
try
{
int element = sc.nextInt();
System.out.println("Element in the given index is : "+myArray[element]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("The index you have entered is invalid");
System.out.println("Please enter an index number between 0 and 6");
throw e;
}
}
}
Output:
Result:
Thus the program was executed successfully and the output was verified.
Ex no:3
Aim:
Algorithm:
import java.sql.*;
public class gfg
{
public static void main(String arg[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/register","root","");
Statement stmt;
stmt= con.createStatement();
ResultSet rs;
rs=stmt.executeQuery("select* from reg");
int id;
int age;
String first;
String second;
while(rs.next())
{
id=rs.getInt("id");
age=rs.getInt("age");
first=rs.getString("first").trim();
second=rs.getString("secound").trim();
System.out.println("id: "+id + "age: "+age+"first:" +first+ "second:" + second);
}
rs.close();
stmt.close();
con.close();
}
catch (Exception exception)
{
}
}
}
Output:
Result:
Thus the program was executed successfully and the output was verified.
Ex no:4
Aim:
Algorithm:
index.html
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="transact">
Enter amount:<input type="text" name="t1"/><br>
select transaction type:<br>
<input type="radio" name="transaction" value="deposit">deposit<br>
<input type="radio" name="transaction" value="withdraw">withdraw<br>
<input type="submit"/>
</form>
</body>
</html>
Transact.java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jbank.banktransactLocal;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
@Override
public String getServletInfo()
{
return "Short description";
}
private banktransactLocal lookupbanktransactLocal()
{
try
{
Context c = new InitialContext();
return (banktransactLocal) c.lookup("java:global/jbank/jbank-ejb/banktransact!
jbank.banktransactLocal");
}
catch (NamingException ne)
{
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
throw new RuntimeException(ne);
}
}
}
Banktransact.java
package jbank;
import javax.ejb.Stateful;
@Stateful
public class banktransact implements banktransactLocal
{
int balance=1000;
// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
@Override
public int deposit(int amount)
{
balance=balance+amount;
return balance;
}
@Override
public int withdraw(int amount)
{
balance=balance-amount;
return balance;
}
}
Output:
Result:
Thus the program was executed successfully and the output was verified.
Ex no:5
Aim:
Index.html
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="Transact">
Enter no of book:<input type="text" name="t1"/><br>
Select Operation:<br>
<input type="radio" name="transaction" value="Returnbook">Returnbook<br>
<input type="radio" name="transaction" value="Issuebook">Issuebook<br>
<input type="submit"/>
</form>
</body>
</html>
TRANSACT.JAVA
import banktransact.banktransactLocal;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
if (s.equals("Returnbook"))
{
out.println(noofbook+"Successfully added Available books are:"+libtransact.Retunbook(noofbook));
}
if (s.equals("Issuebook"))
{
out.println(noofbook+"Successfully removed Available books are:"+libtransact.delbook(noofbook));
}
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
@Override
public String getServletInfo()
{
return "Short description";
}// </editor-fold>
}
LIBTRANSACT.JAVA
package libtransact;
import javax.ejb.Stateful;
@Stateful
public class libtransact implements libtransactLocal
{
int total=10;
@Override
public int Issuebook(int noofbook) {
total=total-noofbook;
return total;
}
}
Output:
Result:
Thus the program was executed successfully and the output was verified.
Ex no:6
Aim:
Algorithm:
import javax.swing.*;
import java.awt.*;
public class Emp extends JFrame
{
JButton jButton1;
JTextField jt1;
JTextField jt2;
JTextField jt3;
JTextField jt4;
JTextField jt5;
public Emp()
{
JLabel jL2 = new JLabel("Enter Name");
jt1 = new JTextField(10);
JLabel jL3 = new JLabel("Enter Department");
jt2 = new JTextField(10);
JLabel jL4 = new JLabel("Enter Address");
jt3 = new JTextField(10);
JLabel jL5 = new JLabel("Enter Salary");
jt4 = new JTextField(10);
JLabel jL6 = new JLabel("Phone No.");
jt5 = new JTextField(12);
jButton1 = new JButton();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Save");
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton1ActionPerformed(evt);
}
});
Container cp = getContentPane();
JPanel p=new JPanel();
p.add(jL2);
p.add(jt1);
p.add(jL3);
p.add(jt2);
p.add(jL4);
p.add(jt3);
p.add(jL5);
p.add(jt4);
p.add(jL6);
p.add(jt5);
p.add(jButton1);
cp.add(p);
}
void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
String name1=jt1.getText();
String dept=jt2.getText();
String addr=jt3.getText();
int salr=Integer.parseInt(jt4.getText());
int phon=Integer.parseInt(jt5.getText());
System.out.println(name1);
System.out.println(dept);
System.out.println(addr);
System.out.println(salr);
System.out.println(phon);
}
public static void main(String args[])
{
Emp emp= new Emp();
emp.setVisible(true);
emp.setSize(260,260);
emp.setTitle("Employee Details");
emp.setResizable(false);
}
}
Output:
Result:
Thus the program was executed successfully and the output was verified
Ex no:7
Create a Servlet to display all the headers available from request
Date:06/10/2022
Aim:
WEB .XML:
<web-app>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>ShowHeaders</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/run</url-pattern>
</servlet-mapping>
</web-app>
Index.html:
<html>
<body>
<a href="run"> click Here </a>
</body>
</html>
ShowHeaders.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
Thus the program was executed successfully and the output was verified
Ex no:8
Aim:
To write a servlet which displays a message and also display how many times the message has been
displayed.
Algorithm:
web.xml
<web-app>
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class> counterservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/run</url-pattern>
</servlet-mapping>
</web-app>
Index.html
<html>
<body>
<a href="run">Click Here </a>
</body>
</html>
counterservlet.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;
public class counterservlet extends HttpServlet
{
private int c;
@Override
public void init() throws ServletException
{
c=0;
}
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response) throws
ServletException,IOException
{
PrintWriter out=response.getWriter();
out.println("<h3> Welcome to my website!!!</h3>");
out.println("You are vistor number: "+(++c));
}
@Override
public void doPost(HttpServletRequest request,HttpServletResponse response) throws
ServletException,IOException
{
doGet(request,response);
}
}
Output:
Result:
Thus the program was executed successfully and the output was verified
Ex no:9
Aim:
Algorithm:
Xml coding
<web-app>
<servlet>
<servlet-name>S1</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>S2</servlet-name>
<servlet-class>SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>S1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>S2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Html coding
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);
Cookie ck=new Cookie("uname",n);//creating cookie object
response.addCookie(ck);//adding cookie in the response
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
Thus the program was executed successfully and the output was verified.
Ex no:10
Date:15/10/22 Create a JSP page which uses the include directive to show its header and footer
Aim:
To Write a JSP page, which uses the include directive to show its header and footer.
Algorithm:
Step1: Open the NetBeans app.
Step2: Create a JSP and give name as main JSP.
Step3: Use <%@include> to include the files for displaying in header.
Step4: Create another JSP and that file the contents to display header and footer.
Step5: Save the application and build.
Step 6: After successful build run the application.
Program Coding:
Main.jsp:
header.jsp:
</head>
<body>
<center>
<h1>Hi Welcome</h1>
<h2> This is an directory Example<h2>
<p>This site has been visited<%= pageCount %> times.</p>
</center>
<br/><br/>
</body>
</html>
footer.jsp:
Thus the program was executed successfully and the output was verified
Ex no:11
Develop Remote Interface and implement Java/RMIServer
Date:20/10/2022
Aim:
Algorithm:
Hello.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote
{
void printMsg() throws RemoteException;
}
ImplExample.java
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;