0% found this document useful (0 votes)
35 views18 pages

AJLabEx1to6 Without Border

The document describes how to add two numbers using RMI concepts in Java. It provides the code for the interface, remote object implementation, server and client classes. It also lists the steps to compile and run the code to demonstrate adding two numbers remotely.

Uploaded by

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

AJLabEx1to6 Without Border

The document describes how to add two numbers using RMI concepts in Java. It provides the code for the interface, remote object implementation, server and client classes. It also lists the steps to compile and run the code to demonstrate adding two numbers remotely.

Uploaded by

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

EX.

NO: 1
DATE:
ADDITION OF TWO NUMBERS USING RMI
Aim:
To add two Numbers using RMI concepts

Procedure:
1.Open four notepad and type the following code with the respective name as given in program. 2.Save all
the four files inside the folder rmi.
3.Open command prompt, go to rmi folder and compile all the four files using javac command. 4.Compile
the implementation program (AdderRemote.java) using the rmic command. 5.Open the rmi registry using
the command “start rmiregistry”. Minimize the rmi registry window. 6.Start the server using the command
“java”.
7.Open a new command prompt window, go to rmi folder and start the client using command “java”.
8.Exit from all the command prompt window.

Program :

1. Adder.java

import java.rmi.*;
public interface Adder extends Remote
{
public int add(int x,int y)throws RemoteException;
}

2. AdderRemote.java

import java.rmi.*; import java.rmi.server.*;


public class AdderRemote extends UnicastRemoteObject implements Adder{ AdderRemote()throws
RemoteException{
super();
}
public int add(int x,int y)
{
return x+y;}
}

3. MyServer.java

import java.rmi.*;
import java.rmi.registry.*;
public class MyServer{
public static void main(String args[]){
try{
Adder stub=new AdderRemote();
Naming.rebind("myobj",stub); }
catch(Exception e){
System.out.println(e);
}
}
}
4. MyClient.java

import java.rmi.*;
public class MyClient{
public static void main(String args[]){try{
Adder stub=(Adder)Naming.lookup("myobj");
System.out.println(stub.add(34,4));
}
catch(Exception e){}
}}

Steps to run this rmi example:

1. Compile all the java files


javac *.java

2. Create stub and skeleton object by rmic tool


rmic AdderRemote

3. Start rmi registry in one command prompt


start rmiregistry

4. Start the server in another command prompt


java MyServer

5. Start the client application in another command prompt


java MyClient

OUTPUT:
RESULT:
Thusthe program has been executed successfully and output verified.
EX.NO: 2
DATE:
CREATE APPLICATIONS USING JDBC
Aim:
To retrieve the records from employee table using Mysql Database and JDBC.

Procedure:
1.Open mysql command line and create a database, table and insert two rows of data into the table. a.

mysql> create database user;

b. mysql> use user;

c. mysql> show tables;

d. mysql> create table employee(id int(3), name varchar(20));

e. mysql> insert into employee values(101,"Ram");

f. mysql> insert into employee values(102,"Raju");

g. mysql> select * from employee;


2.Start Netbeans IDE , Go to file menu and select new project, The new project dialog box shown as
below opens. From the Categories select the option java and from projects select java Application and
click Next.

3.Type the name of the project as “myfirstdatabase” and select a location to save the project and click
Finish.
4.In the project tab
right click the library folder of your application and select the option “Add JAR/Folder” and go to the
location where we have saved our mysql connector JAR file. Select the JAR file and it will be added to
your project.
5.Type the following code in the main method.
6.Run the code by clicking the run tool or by selecting run option from run menu.
Program

package myfirstdatabase;
import java.sql.*;
public class Myfirstdatabase
{
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","mysql"); Statement
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from employee");
while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output:

RESULT:
Thusthe program has been executed successfully and output verified.
EX.NO: 3
DATE:
CREATE STUDENT APPLICATIONS USING JDBC
Aim:
To create a student application using MySQL Database and JDBC.

Procedure :
1. Open mysql command line and create a database and table as given below:

a. mysql> create database bsc;

b. mysql> use bsc;

c. mysql> create table student(sid int(3),sname varchar(20),age int(2));;

d. mysql> select * from student;


2. Start Netbeans IDE , Go to file menu and select new project, The new project dialog box shown as
below opens. From the Categories select the option java and from projects select java Application and
click Next.
3.Type the name of the project as “studentJDBC” and select a location to save the project and click
Finish.
4.In the project tab right click the library folder of your application and select the option “Add
JAR/Folder” and go to the location where we have saved our mysql connector JAR file. Select the JAR
file and it will be added to your project.
5.Type the following code in the main method.
6.Run the code by clicking the run tool or by selecting run option from run menu.
Program
import java.sql.*;
import java.io.*;
public classstudentJDBC
{
public static void main(String args[])
{
int choice; String n,ch;int id,age; int rows;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in)); try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bsc","root","mysql");
Statement s=con.createStatement();
do
{
System.out.println("choice");
System.out.println("1.Insertion\t 2.Deletion \t 3.Select contents from
table"); choice=Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
{
String ss="insert into student(sid,sname,age) values(?,?,?)";
PreparedStatement ps=con.prepareStatement(ss);
System.out.println("enter student id");
id=Integer.parseInt(br.readLine());
System.out.println("enter student name");
n=br.readLine();
System.out.println("enter student age");
age=Integer.parseInt(br.readLine());
ps.setInt(1,id);
ps.setString(2,n);
ps.setInt(3,age);
rows=ps.executeUpdate();
System.out.println(rows+"rows inserted");
break;
}
case 2:
{
String ss="delete from student where id=?";
PreparedStatement ps=con.prepareStatement(ss);
System.out.println("enter student id to delete");
id=Integer.parseInt(br.readLine());
ps.setInt(1,id);
rows=ps.executeUpdate();
System.out.println(rows+"rows deleted");
break;
}
case 3:
{
ResultSet rs=s.executeQuery("select * from student");
while(rs.next())
{
System.out.println("details from student table");
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getInt(3));
}
break;
}
default:
System.out.println("choose between 1 to 3");
}
System.out.println("Want to continue (y/n)");
ch=br.readLine();
}
while(ch.equals("y"));
s.close();
con.close();
}
catch(Exception e)
{
System.out.println("Exception caught"+e);
}}}

Output:
1. Inserting 2 rows :

2. Displaying all rows:


3. Deleting 1 row:
RESULT:
Thusthe program has been executed successfully and output verified.
EX.NO: 4
DATE:
DEVELOP WEB APPLICATIONS USING SERVLET

Aim:
Develop simple Login Application using Servlet.

Procedure:
1.Start Netbeans IDE, Go to file menu and select new project, the new project dialog box shown as
below opens. From the Categories select the option “java web” and from projects select “web
Application” and click Next.
2.Type the name of the project as “ServApp” and select a location to save the project and click Next.
3.In the server select default server “GlassFish Server” and click “Next”. Click “Finish”. 4.Type the
index.html code in the respective index .html file.
5.Right click the source packages folder from the workspace and select option “new” and select
“servlet” and type the name of the servlet as “LoginServlet” click “Next”. Select the check box “Add
information to deployment descriptor” and then click “Finish”.
6.Run the index.html by right clicking the file and select run.

Program :
Index.html
<html>
<head>
<title>Login Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="LoginServlet">
Email ID:<input type="text" name="email" ><br/>
Password:<input type="password" name="pass" /><br/>
<input type="submit" value="login" />
</form>

LoginServlet.java
import java.io.*;
importjavax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String user = request.getParameter("email");
String pass = request.getParameter("pass");

if(user.equals("srm")&& pass.equals("123"))
{
out.println("Welcome to oursite" + user);
}
else
{
out.println("Username or Password incorrect!! Try Again");
}}

OUTPUT:
RESULT:
Thusthe programhas been executed successfully and output verified.
EX.NO: 5
DATE:
TO CREATE USER LOGIN FORM USING SERVLET
Aim:
To create the user Login page and to Read the Form Data using Servlet

Procedure:

1.Start Netbeans IDE, Go to file menu and select new project, the new project dialog box shown as
below opens. From the Categories select the option “java web” and from projects select “web
Application” and click Next.
2.Type the name of the project as “LoginServlet” and select a location to save the project and click
Next.
3.In the server select default server “GlassFish Server” and click “Next”. Click
“Finish”. 4.Type the index.html code in the respective index .html file.
5.Right click the source packages folder from the workspace and select option “new” and select
“servlet” and type the name of the servlet as “GetForm” click “Next”. Select the check box “Add
information to deployment descriptor” and then click “Finish”.
6.Run the index.html by right clicking the file and select run.

Program:
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>
<formaction = "RegisterForm" method = "GET">
First Name: <input type = "text" name = "first_name"><br />
Last Name: <input type = "text" name = "last_name" /><br />
Address: <input type = "text" name = "address"/><br />
Email: <input type = "text" name = "email"/><br />
<input type = "submit" value = "Submit" />
</form>
</body>
</html>

GetForm.java

import java.io.*;
importjavax.servlet.*;
import javax.servlet.http.*;
public class GetForm extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String title = "Using GET Method to Read FormData";
StringdocType = "<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n"; out.println(docType +"<html>\n"
+ "<head><title>" + title + "</title></head>\n" +"<body bgcolor = \"pink\">\n" +
"<h1 align = \"center\">" + title + "</h1>\n" +"<ul>\n" +" <li><b>First Name</b>: "
+ request.getParameter("first_name") + "\n" + "<li><b>Last Name</b>: " +
request.getParameter("last_name") + "\n" + "<li><b>Address</b>: "
+ request.getParameter("address") + "\n" + "<li><b>Email</b>: "
+ request.getParameter("email") + "\n" + "</ul>\n" +"</body>"
+"</html>"); }}

OUTPUT
RESULT: Thusthe programhas been executed successfully and output verified.
EX.NO: 6
DATE:
SESSION MANAGEMENT USING SERVLET

AIM:
To perform session management using servlet.

PROCEDURE:
1.Start Netbeans IDE, Go to file menu and select new project, the new project dialog box shown as
below opens. From the Categories select the option “java web” and from projects select “web
Application” and click Next.
2.Type the name of the project as “Sess” and select a location to save the project and click Next.
3.In the server select default server “GlassFish Server” and click “Next”. Click “Finish”. 4.Type
the index.html code in the respective index .html file.
5.Right click the source packages folder from the workspace and select option “new” and select
“servlet” and type the name of the servlet as “FirstServlet” click “Next”. Select the check box
“Add information to deployment descriptor” and then click “Finish”. Type its code. 6.Repeat
step 5 and create another servlet with the name “SecondServlet” and type its code. 7.Run the
index.html by right clicking the file and select run.

PROGRAM:
FirstServlet.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 FirstServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse
response) throwsServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
out.print("Welcome "+n);
out.print("<a href='SecondServlet?uname="+n+"'>visit</a>");
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
{
processRequest(request, response); }
Second Servlet.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 SecondServlet extends HttpServlet{
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("uname");
out.print("Hello "+n);
out.close();
}
protected void doPost (HttpServletRequest request, HttpServletResponse
response)throws ServletException, IOException {
processRequest(request, response); }}

Index.html

<html>
<body>
<form action="FirstServlet" method="post">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="go"/>
</form>
</body>
</html>

OUTPUT:

RESULT:
Thusthe programhas been executed successfully and output verified.

You might also like