0% found this document useful (0 votes)
69 views

Java Lab File Modify1

The document contains the index for 9 experiments conducted. Each experiment contains the objective, software used, procedure and output. The experiments cover topics like Remote Method Invocation in Java using interfaces, classes, servers and clients. Designing a calculator using grid layout, implementing mouse events, creating and inserting values in a database table using JDBC, updating table values and more.

Uploaded by

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

Java Lab File Modify1

The document contains the index for 9 experiments conducted. Each experiment contains the objective, software used, procedure and output. The experiments cover topics like Remote Method Invocation in Java using interfaces, classes, servers and clients. Designing a calculator using grid layout, implementing mouse events, creating and inserting values in a database table using JDBC, updating table values and more.

Uploaded by

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

INDEX

S. Name of Experiment Date of Marks Signature


N experiment obtained of Faculty
o
1. RMI Application using interface, class,
server and client.
2. WAP to design a calculator using grid
layout.
3. WAP to implement mouse events.

4. WAP to create a table and Insert values


and display them.
5. WAP to update table values and display
them.
6. JSP Program to make a Login and
password functionality using database.
7. WAP to implement servlets.
8. WAP using Servlet for persistent and
non-persistent cookies on client side.
9. WAP to print sever side information using
JSP that shows URL, IP, Context, Hit
count.

EXPERIMENT -1

OBJECTIVE: Write a program to implement Remote Method Invocation (RMI) in JAVA


creating an interface, a class to implement it, a server class and a client class.

SOFTWARE USED: JDK 1.7, Notepad, CMD

PROCEDURE:

//Interface Adder (Adder.java)


import java.rmi.*;
public interface Adder extends Remote
{
public int sum(int x,int y) throws RemoteException;
}
//Implementation Class (MyAdder.java)
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class MyAdder extends UnicastRemoteObject implements Adder
{ MyAdder() throws RemoteException
{
super();
}
public int sum(int x, int y) throws RemoteException
{
return (x+y);
}
}

//Server Class (RmiServer.java)


import java.rmi.*;
import java.rmi.registry.*;
public class RmiServer
{ public static void main(String s[])
{ try{ System.out.println("Server is started");
MyAdder a=new MyAdder();
System.out.println("\nBinding the remote object into registry...");
Naming.bind("myStub",a);
System.out.println("\nObject registered");
}
catch(Exception e) {e.printStackTrace();}
}
}
//Client Class (RmiClient.java)
import java.rmi.*;
class RmiClient
{ public static void main(String s[])
{ try{ System.out.println("Client started");
Adder a=(Adder)Naming.lookup("rmi://localhost:1099/myStub");
int res=a.sum(5,6);
System.out.println("\nStub obtained.\nResult is" + res);
}
catch(Exception e){
e.printStackTrace();
}
}
}

OUTPUT:
RESULT:
The RMI Application to add two numbers runs successfully.

EXPERIMENT -2

OBJECTIVE: Write a program to design a calculator using grid layout.

SOFTWARE USED: NetBeans, JDK 1.7, Google Chrome

PROCEDURE:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="Calculator1" width=300 height=300></applet>*/
public class Calculator1 extends Applet implements Calc
{
Text t;
Button b[]=new Button[15];
Button b1[]=new Button[6];
String op2[]={“+”,”-”,”*”,”%”,”=”,”C”};
String str1="";
int p=0,q=0;
String oper;
public void init()
{
setLayout(new GridLayout(5,4));
t=new Text(20);
setBackground(Color.pink);
setFont(new Font("Arial",Font.BOLD,20));
int k=0;
t.setEditable(false);
t.setBackground(Color.white);
t.setText("0");
for(int i=0;i<10;i++)
{
b[i]=new Button(""+k);
add(b[i]);
k++;
b[i].setBackground(Color.pink);
b[i].addCalc(this);
}
for(int i=0;i<6;i++)
{
b1[i]=new Button(""+op2[i]);
add(b1[i]);
b1[i].setBackground(Color.pink);
b1[i].addCalc(this);
}
add(t);
}
public void operation(oprtn op)
{
String str=op.getoperationcommand();
if(str.equals("+"))
{
p=Integer.parseInt(t.getText());
oper=str;
t.setText(str1="");
}
else if(str.equals("-"))
{
p=Integer.parseInt(t.getText());
oper=str;
t.setText(str1="");
}
else if(str.equals("*"))
{
p=Integer.parseInt(t.getText());
oper=str;
t.setText(str1="");
}
else if(str.equals("%"))
{
p=Integer.parseInt(t.getText());
oper=str;
t.setText(str1="");
}
else if(str.equals("="))
{ str1="";
if(oper.equals("+")) {
q=Integer.parseInt(t.getText());
t.setText(String.valueOf((p+q)));}
else if(oper.equals("-")) {
q=Integer.parseInt(t.getText());
t.setText(String.valueOf((p-q))); }

else if(oper.equals("*")){
q=Integer.parseInt(t.getText());
t.setText(String.valueOf((p*q))); }

else if(oper.equals("%")){
q=Integer.parseInt(t.getText());
t.setText(String.valueOf((p%q))); }
}
else if(str.equals("C")){ p=0;q=0;
t.setText("");
str1="";
t.setText("0");
}
else{ t.setText(str1.concat(str));
str1=t.getText();
}
}
}

Output:
RESULT: Calculator runs successfully.

EXPERIMENT -3

OBJECTIVE: Write a program to implement mouse events.

SOFTWARE USED: NetBeans.

PROCEDURE:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class GFG extends Applet implements MouseListener
{
private int x,y;
private String str = " ";
public void init()
{
this.addMouseListener (this); 
}
public void paint(Graphics g)
{
g.drawString(str,x,y);
}
public void update(Graphics g)
{
paint(g);
}
public void mouseClicked(MouseEvent m)
{
  x = m.getX();
  y = m.getY();
  str = "x =" +x+",y = "+y;
repaint();
}
 public void mouseEntered(MouseEvent m)
{
}
 public void mouseExited(MouseEvent m)
{
}
 public void mousePressed(MouseEvent m)
{
}
 public void mouseReleased(MouseEvent m)
 {     
 }

Output:
EXPERIMENT -4
OBJECTIVE: Write a program to create a table and Insert values and display them using
JDBC.

SOFTWARE USED: NetBeans, JDK 1.7, Firefox

PROCEDURE:

//TO INSERT IN THE DATABASE

package jdbc_program;
import java.sql.*;

public class serv extends HttpServlet {

public static void main(String[] args) {


String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost/S7093";

Connection conn = null;


Statement stmt = null;
try{
Class.forName(driver);
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(url,"root","");

System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM saurabh";
try (ResultSet rs = stmt.executeQuery(sql)) {
while(rs.next()){

int no = rs.getInt("A");
String A = rs.getString("A");

System.out.print("mobile: " + no);


System.out.print("Name: " + A);

}
}
stmt.close();
conn.close();
}
catch(SQLException | ClassNotFoundException se){
}
finally{

try{
if(stmt!=null)
stmt.close();
}
catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
}
}
System.out.println("Goodbye!");
}

RESULT:
Records in database are successfully inserted using JDBC program.

EXPERIMENT -5
OBJECTIVE: Write a program update table values and display them.

SOFTWARE USED: NetBeans, JDK 1.7, Firefox

PROCEDURE:

//To update the database


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class Update extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection dbconnection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/s7093","root","s7093");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Update</title>");
out.println("</head>");
out.println("<body>");
PreparedStatement pstmt = dbconnection.prepareStatement("UPDATE student SET
name='"+request.getParameter("name")+"' where rollno='"+request.getParameter("rollno")
+"'");
pstmt.executeUpdate();
out.println("<h1>Servlet Updated Record </h1>");
out.println("</body>");
out.println("</html>");
out.close();
}
catch(Exception sqle)
{ System.err.println("Connection error: " + sqle); }
}
}

//to display all the records


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class Display extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try { Class.forName("com.mysql.jdbc.Driver");
Connection dbconnection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/s7093","root","s7093");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Display</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Data Displayed from Student Database. </h1>");
Statement statement = dbconnection.createStatement();
String sqlString = "SELECT * FROM saurabh";
ResultSet resultset=statement.executeQuery(sqlString);
out.println("NAME &nbsp; &nbsp; ROLLNO<br>");
while(resultset.next())
{
out.println(resultset.getString("name"));
out.println("&nbsp;&nbsp;&nbsp; &nbsp&nbsp&nbsp&nbsp");
out.println(resultset.getString("rollno"));
out.println("<br>");
}
out.println("</body>");
out.println("</html>");
out.close();
}
catch(Exception sqle)
{
System.err.println("Connection error: " + sqle);
}
}

RESULT:
Records in database are successfully manipulated using servlet and JSP programs.

EXPERIMENT -6
OBJECTIVE: Write a program in JSP to provide Login Password Functionality using
Database Driver.

SOFTWARE USED: NetBeans, JDK 1.7, Firefox

PROCEDURE:

//index.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index Page</title>
</head>
<body>
<FORM action="check.jsp" method="get">
<table align="center"> <br><br><br><br><br><br><br><br><br><br>
<tr>
<td>
<b>Admin Login:-<br><br>
Username:<input type="text" size=20 maxlength=20 name="name"><br>
Password:&nbsp<input type="password" size=20 maxlength=20 name="rollno"></b>
<input type="Submit" value="LOGIN"<center/></td>
</table>
</FORM>
</body>
</html>
//check.jsp

<%@page contentType="text/html"
import="java.sql.*,java.util.*,java.text.*,java.util.Date,java.net.*" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%! String rollno,name; %>
<%
Connection con1=null;
Statement stmt1=null;
ResultSet rs1=null;
try{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException cnfe){ }
try{ con1 =
DriverManager.getConnection("jdbc:mysql://localhost:3306/s7093","root","s7093");
stmt1 = con1.createStatement();rs1 = stmt1.executeQuery("select *FROM saurabh WHERE
rollno='"+request.getParameter("rollno")+"'AND name='"+request.getParameter("name")
+"'");
while(rs1.next()){
rollno = rs1.getString("rollno");
name= rs1.getString("name");
}
if(name.equals(request.getParameter("name")) &&
rollno.equals(request.getParameter("rollno")))
{
response.sendRedirect("correct.jsp?name="+request.getParameter("name")+"");
}
else
{
response.sendRedirect("incorrect.jsp");
}
}
catch(SQLException sqle){ }
%>
</body> </html>

//correct.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Menu Page</title>
</head>
<body>
<center><b><font size="10">Welcome <%out.println(request.getParameter("name"));
%></font></b>
</center>
</body>
</html>
//incorrect.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>incorrect</h1>
</body>
</html>

OUTPUT:
RESULT: Login page created successfully.

EXPERIMENT -7

OBJECTIVE: Write a program to implement servlet.

SOFTWARE USED: NetBeans, JDK 1.7

PROCEDURE:

//Servlet program to display current system date and time.

package ds;
import javax.servlet.*;
import java.io.*;
import java.util.*;
public class DateServ extends GenericServlet {
public DateServ() {
System.out.println("SERVLET LOADED...");
}
public void init()
{}
public void service(ServletRequest req, ServletResponse res) throws ServletException,
IOException {
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
Date d = new Date();
String s = d.toString();
pw.println("<h1> WELCOME TO SERVLETS <h1>");
pw.println("<h2> CURRENT DATE & TIME IS : " + s + "<h2>");
}
public void destroy()
}
};

EXPERIMENT -8

OBJECTIVE: Write a program using servlet to write persistent and non-persistent cookies on
client side.
SOFTWARE USED: NetBeans, JDK 1.7, Google Chrome

PROCEDURE:

//Cook.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.*;
public class Cook extends HttpServlet {
private static final long serialVersionUID = 1L;
public Cook()
{
super();
}
protected void processRequest(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet MyServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("Advanced Java Lab");
boolean newbie = true;
Cookie[] cookies = request.getCookies();
if (cookies != null)
{
for(int i=0; i<cookies.length; i++)
{
Cookie c = cookies[i];
if ((c.getName().equals("repeatVisitor")) && (c.getValue().equals("yes"))) {
newbie = false;
break;
}
String title;
if (newbie) {
Cookie returnVisitorCookie =new Cookie("repeatVisitor", "yes");
returnVisitorCookie.setMaxAge(60*60*24*365); // 1 year
response.addCookie(returnVisitorCookie);
title = "Welcome Aboard";
}
else {
title = "Welcome Back";
}
out.println("<H1>Setting and Reading Cookies</H1>");
out.println(title);
out.println("</body>");
out.println("</html>");
}
finally {
out.close(); }
}
}
OUTPUT:

RESULT: Program for persistent and non persistent cookies runs successfully.

EXPERIMENT -9

OBJECTIVE: Write a program to print server side information using JSP as Client IP Address ,
URL , Context Info, hit count.

SOFTWARE USED: NetBeans, JDK 1.7, Google Chrome


PROCEDURE:

//hit.jsp

<%@ page contentType="text/html" pageEncoding="UTF-8" import="java.io.*,java.util.*"


%>
<!DOCTYPE html>
<head>
<title>Experiment 6</title>
</head>
<body>
<% Integer hitsCount =
(Integer)application.getAttribute("hitCounter");
if( hitsCount ==null || hitsCount == 0 )
{
/* First visit */
out.println("Welcome to my website!");
hitsCount = 1;
}
else{ /* return visit */
out.println("Welcome back");
hitsCount += 1;
}
%><br><br><center><%
out.println("Context path is::"+request.getContextPath());%><br><br><%
out.println("Path info is::"+request.getServletPath());%><br><br><%
out.println("Context Info::"+request.getContextPath());%><br><br><%
out.print( "Client IP address::"+request.getRemoteAddr());%><br><br><br><br><br><br><
%
application.setAttribute("hitCounter", hitsCount);
%>
<p>Total number of visits: <%= hitsCount%></p>
</center>
</body>
</html>

OUTPUT:

RESULT: All information displayed successfully.

You might also like