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

Advanced Java Lab

The document is a lab file submitted by Shreyash K Gupta for the course Advanced Java Programming. It contains details of 3 programming assignments - writing a program to find the second largest number, writing a program to transfer files, and writing a program to implement RMI. The file also includes sample code and output for each assignment.

Uploaded by

Siddharth Jain
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)
466 views

Advanced Java Lab

The document is a lab file submitted by Shreyash K Gupta for the course Advanced Java Programming. It contains details of 3 programming assignments - writing a program to find the second largest number, writing a program to transfer files, and writing a program to implement RMI. The file also includes sample code and output for each assignment.

Uploaded by

Siddharth Jain
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/ 19

Lab File

Advanced Java Programming


[IT404]

DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING

Submitted to: Submitted by:


Dr. Shilpi Sharma Shreyash K Gupta
Associate Professor A2305216554
CSE Department, ASET B.Tech (CSE)
6CSE-8Y

AMITY SCHOOL OF ENGINEERING AND TECHNOLOGY


AMITY UNIVERSITY UTTAR PRADESH
NOIDA-201301
IT404 ADVANCED JAV A PR OGRAMMING

Assignment Name of Date of Date of Max. Marks Faculty


Exp.No Code
Category Experiment Allotment Evaluation Marks obtained Sign

Write a
program to find
the second 19 Dec
1. 1
largest number 2018
in a series of
numbers.
1. Write a
program to
2 Jan
2. transfer the 1
2018
contents of one
file to another.
Write a
program to
LR implement RMI 23 Jan
3. (10) 1
with client and 2018
server on same
system.
4. 1
5. 1
6. 1
7. Mandator 1
8. y 1
Experime
9. nt 1

10. 1

Viva
11. Viva 5
(5)

SHREYASH K GUPTA 0
IT404 ADVANCED JAV A PR OGRAMMING

1. Objective: Write a program to find the second largest number in a series of


numbers.

Software Used: Netbeans IDE

Code:
package skgsecondmax;
import java.util.*;
public class SKGSecondMax {
static int smax(int[] arr) {
int second = 0;
for(int i=0;i<5;i++) {
for(int j=i+1;j<5;j++) {
if(arr[j]>arr[i]) {
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
for(int i=1;i<5;i++) {
if(arr[0]!=arr[i]) {
second = arr[i];
break;
}
else if(i==4) {
second = arr[4];
}
}
return second;
}
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
System.out.println("Second max of 5 numbers");
System.out.println("Enter array of number: ");
int [] a= new int[5];
for(int i=0;i<5;i++) {
System.out.print("Enter element " + (i+1) +": ");
a[i] = inp.nextInt();
}
System.out.println("Second max number = " + smax(a));
}

SHREYASH K GUPTA 1
IT404 ADVANCED JAV A PR OGRAMMING

Output:

SHREYASH K GUPTA 2
IT404 ADVANCED JAV A PR OGRAMMING

2. Objective: Write a program to transfer the contents of one file to another.


Software Used: Netbeans IDE

Code:
package skgfiletransfer;
import java.util.*;
import java.io.*;
public class SkgFileTransfer {
public static void main(String[] args) {
File fileOne = new File("fileOne.txt");
File fileTwo = new File("fileTwo.txt");
try{
FileInputStream fis = new FileInputStream(fileOne);
FileOutputStream fos = new FileOutputStream(fileTwo);
int ch;
while((ch = fis.read())!=-1) {
fos.write(ch);
}
System.out.println("File transfer complete.");
fis.close();
fos.close();
}
catch(Exception e) {
System.out.println("Error in file I/O");
}
}
}

Output:

SHREYASH K GUPTA 3
IT404 ADVANCED JAV A PR OGRAMMING

3. Objective: Write a program to implement RMI with client and server on same
system.

Software Used: Notepad/TextEdit and Command Prompt/Terminal

Code:
1. //Interface
import java.rmi.*;
public interface Shreyash extends Remote {
public int getSquare(int num) throws RemoteException;
}

2. //Server
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
public class SKGServer extends UnicastRemoteObject implements Shreyash
{
public SKGServer() throws RemoteException {
super();
System.out.println("Server object created");
}
public int getSquare(int num) {
return num*num;
}
public static void main(String args[]) {
try {
SKGServer server = new SKGServer();
Naming.rebind("myServer",server);
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}

3. //Client
import java.rmi.*;
import java.rmi.registry.*;
public class SKGClient {
public static void main(String args[]) {
try {
Shreyash skg = (Shreyash)Naming.lookup("myServer");

SHREYASH K GUPTA 4
IT404 ADVANCED JAV A PR OGRAMMING

for(int i=1;i<=5;i++) {
System.out.println("Square of " + i + " = " +
skg.getSquare(i));
}
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}

Output:

SHREYASH K GUPTA 5
IT404 ADVANCED JAV A PR OGRAMMING

4. Objective: Write a program to implement JDBC

Software Used: Netbeans

Code:
package jdbc;
import java.sql.*;
public class JDBC {
public static void main(String[] args) {
try {
//Step 1: Load the driver
Class.forName("org.apache.derby.jdbc.ClientDriver");

//Step 2: Create a connection object


Connection connect =
DriverManager.getConnection("jdbc:derby://localhost:1527/skgDatabase");

//Step 3: Create statement object


Statement statement = connect.createStatement();

//Step 4: Execute query


ResultSet result;
System.out.println("Displaying Table");
result = statement.executeQuery("SELECT * FROM SKGTable");
while(result.next()) {
System.out.println(result.getInt(1) + ", " + result.getString(2) + ", " +
result.getInt(3) + ", " + result.getString(4));
}
System.out.println();

System.out.println("Inserting a Value");
statement.executeUpdate("INSERT INTO SKGTable VALUES (43,
'Shaurya Kumar', 5, 'ME')");
result = statement.executeQuery("SELECT * FROM SKGTable");
while(result.next()) {
System.out.println(result.getInt(1) + ", " + result.getString(2) + ", " +
result.getInt(3) + ", " + result.getString(4));
}
System.out.println();

System.out.println("Deleting a Value");
statement.executeUpdate("DELETE FROM SKGTable WHERE
RollNumber = 43");

SHREYASH K GUPTA 6
IT404 ADVANCED JAV A PR OGRAMMING

result = statement.executeQuery("SELECT * FROM SKGTable");


while(result.next()) {
System.out.println(result.getInt(1) + ", " + result.getString(2) + ", " +
result.getInt(3) + ", " + result.getString(4));
}
System.out.println();
//Step 5: Close the connection
connect.close();
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}

Output:

SHREYASH K GUPTA 7
IT404 ADVANCED JAV A PR OGRAMMING

5. Objective: WAP to create a servlet.

Software used: Netbeans

Code:
a. Java code:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class NewServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html><body>");
pw.println("Welcome to Servlet");
pw.println("</body></html>");
pw.close();
}
}

b. HTML code:

<html>
<head>
<title></title>
</head>
<body>
<h4> Click here to go to <a href="NewServlet">My Servlet
Page</a></h4>
</body>
</html>

c. XML code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jc
p.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>NewServlet</servlet-name>
<servlet-class>NewServlet</servlet-class>

SHREYASH K GUPTA 8
IT404 ADVANCED JAV A PR OGRAMMING

</servlet>
<servlet-mapping>
<servlet-name>NewServlet</servlet-name>
<url-pattern>/NewServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>SKGHtml.html</welcome-file>
</welcome-file-list>
</web-app>

Output:

SHREYASH K GUPTA 9
IT404 ADVANCED JAV A PR OGRAMMING

6. Objective: WAP to create a servlet implementing session tracking.

Software used: Netbeans

Code:
a. Java code:

//FirstServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FirstServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try (PrintWriter out = response.getWriter()) {
response.setContentType("text/html");
String n = request.getParameter("userName");
out.print("Welcome "+n);
out.print("<a href='servlet2?uname="+n+"'>visit</a>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet FirstServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet FirstServlet at " +
request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
out.close();
}
catch(Exception e){
System.out.println(e);
}
}

//SecondServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SecondServlet extends HttpServlet {

SHREYASH K GUPTA 10
IT404 ADVANCED JAV A PR OGRAMMING

protected void processRequest(HttpServletRequest request,


HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try (PrintWriter out = response.getWriter()) {
response.setContentType("text/html");
String n = request.getParameter("userName");
out.print("Hello "+ n);
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet SecondServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet SecondServlet at " +
request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
out.close();
}
catch(Exception e){
System.out.println(e);
}
}

b. HTML code:

<html>
<head>
<title>SKGCookie</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
</head>
<body>
<form action=”servlet1”>
Name: <input type=”text” name=”username”/><br/>
<input type=”submit” value=”go”/>
</form>
</body>
</html>

c. XML code:

SHREYASH K GUPTA 11
IT404 ADVANCED JAV A PR OGRAMMING

<?xml version="1.0" encoding="UTF-8"?>


<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>

Output:

SHREYASH K GUPTA 12
IT404 ADVANCED JAV A PR OGRAMMING

SHREYASH K GUPTA 13
IT404 ADVANCED JAV A PR OGRAMMING

7. Objective: WAP to create a servlet implementing login servlets.

Software used: Netbeans

Code:
a. Java Code:
//First Servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SKGFirstServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String name = request.getParameter("name");
String email = request.getParameter("email");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet SKGFirstServlet</title>");
out.println("</head>");
out.println("<body>");
//out.println("<h1>Servlet SKGFirstServlet at " +
request.getContextPath() + "</h1>");
out.println("<h2>Welcome to SKGLoginPage</h2></br>");
out.println("<a href = SKGSecondServlet?name=" + name +
"&email=" + email + ">Click here to view your login information</a>");
out.println("</body>");
out.println("</html>");
}
}

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

SHREYASH K GUPTA 14
IT404 ADVANCED JAV A PR OGRAMMING

out.println("<html>");
out.println("<head>");
out.println("<title>Servlet SKGSecondServlet</title>");
out.println("</head>");
out.println("<body>");
//out.println("<h1>Servlet SKGSecondServlet at " +
request.getContextPath() + "</h1></br>");
out.println("Welcome, " + name);
out.println("E-mail: " + email);
out.println("</body>");
out.println("</html>");
}
}

b. HTML Code:
<html>
<head>
<title>SKG Login Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
</head>
<body>
<div>Login Form</div>
<form action ="SKGFirstServlet">
Name: <input type = "text" name = "name"></br>
E-mail: <input type ="password" name ="email"></br>
<input type = "submit" value = "Login"/></br>
</form>
</body>
</html>

c. XML Code:
?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jc
p.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>SKGFirstServlet</servlet-name>
<servlet-class>SKGFirstServlet</servlet-class>
</servlet>
<servlet>

SHREYASH K GUPTA 15
IT404 ADVANCED JAV A PR OGRAMMING

<servlet-name>SKGSecondServlet</servlet-name>
<servlet-class>SKGSecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SKGFirstServlet</servlet-name>
<url-pattern>/SKGFirstServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SKGSecondServlet</servlet-name>
<url-pattern>/SKGSecondServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

Output:

SHREYASH K GUPTA 16
IT404 ADVANCED JAV A PR OGRAMMING

SHREYASH K GUPTA 17

You might also like