AJava_File[1]ss
AJava_File[1]ss
of
Advance Java
Roll No. : 06
May 2025
Index
S. No Experiment
Write a Java program of the InetAddress class in Java for retrieving the host name and IP
1
address of a specified host.
Write a Java program of the URL class in Java for extracting components such as protocol,
2
host, port, file path, and full URL from a web address
Write a Java programto implement a simple client-server chat application in Java using
3
socket programming, where the server and client can exchange text messages in real-time.
4 To develop a basic Java servlet that displays a welcome message in a web browser
To write a Java program using the UCanAccess JDBC driver to connect to a Microsoft Access
5
database and create a table with appropriate fields and constraints.
To develop a Java servlet that collects member details including an image filename from an
6
HTML form, and stores the information in a MySQL database using JDBC.
To demonstrate the use of Cookies in Servlets by creating a FirstServlet that stores the user's
7
name in a cookie and passes it to the next Secondservlet.
To demonstrate basic JSP scripting elements and expressions including string manipulation
9
and displaying the current date using JSP.
To create a Java RMI-based application that performs basic arithmetic operations remotely
11
using client-server architecture
Write a Java program to implement the Singleton Design Pattern for managing database
12 operations, ensuring that only one instance of the DatabaseOperations class exists throughout
the application.
Program No 1.
Aim:- To demonstrate the use of the InetAddress class in Java for retrieving the host name and IP address
of a specified host.
import java.net.*;
class URLDemo {
public static void main(String args[]) throws MalformedURLException {
@SuppressWarnings("deprecation")
URL hp = new URL("https://www.tpointtech.com/java-tutorial");
System.out.println("Protocol: " + hp.getProtocol());
System.out.println("Port: " + hp.getPort());
System.out.println("Host: " + hp.getHost());
System.out.println("File: " + hp.getFile());
System.out.println("Ext: " + hp.toExternalForm());
}
}
Program No 3.
Aim:- To implement a simple client-server chat application in Java using socket programming, where
the server and client can exchange text messages in real-time.
import java.io.*;
import java.net.*;
// Chat loop
while (true) {
String clientMsg = in.readLine();
if (clientMsg == null || clientMsg.equalsIgnoreCase("bye"))
break;
System.out.println("Client: " + clientMsg);
System.out.print("Server: ");
message = userInput.readLine();
out.println(message);
if (message.equalsIgnoreCase("bye")) break;
}
socket.close();
serverSocket.close();
System.out.println("Chat ended.");
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
import java.io.*;
import java.net.*;
socket.close();
System.out.println("Chat ended.");
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
Program No 4.
Aim:- To develop a basic Java servlet that displays a welcome message in a web browser.
package in.sp.backend;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
</servlet-mapping>
<servlet-mapping>
<servlet-name>ml</servlet-name>
<url-pattern>/mylogin</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ml</servlet-name>
<servlet-class>in.sp.backend.Login</servlet-class>
</servlet>
</web-app>
Program No 5.
Aim:- To write a Java program using the UCanAccess JDBC driver to connect to a Microsoft Access
database and create a table with appropriate fields and constraints.
import java.sql.*;
package in.sp.backend;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part;
@MultipartConfig
public class MyServlet extends HttpServlet {
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/parveendb";
private static final String JDBC_USER = "root";
private static final String JDBC_PASSWORD = "Pky@1234";
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// Validate inputs
if (mname == null || mname.trim().isEmpty() ||
phone == null || phone.trim().isEmpty() ||
email == null || email.trim().isEmpty() ||
file == null || file.getSize() == 0) {
out.println("<h2 style='color:red;'>Please fill all fields!</h2>");
return;
}
// HTML response
out.println("<html><head><title>Latest Member Details</title></head><body>");
out.println("<h1>Latest Member Details</h1>");
if (rs.next()) {
String mname2 = rs.getString("mname");
String phone2 = rs.getString("phone");
String email2 = rs.getString("email");
String imageFileName2 = rs.getString("imageFileName");
out.println("</table>");
out.println("<html><head>");
out.println("<script>");
out.println("setTimeout(function() { window.location.href = 'index1.html'; }, 5000);"); // Delay of
5 seconds
out.println("</script>");
out.println("<title>Success</title></head><body>");
out.println("<h2 style='color:green;'>Data inserted successfully! Redirecting to home page in 5
seconds...</h2>");
out.println("</body></html>");
} else {
out.println("<p>No recent members found!</p>");
}
out.println("</body></html>");
// Close resources
rs.close();
stmt2.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
out.println("<p style='color:red;'>Error: MySQL JDBC Driver Not Found!</p>");
e.printStackTrace(out);
} catch (SQLException e) {
out.println("<p style='color:red;'>SQL Error: " + e.getMessage() + "</p>");
e.printStackTrace(out);
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home Page</title>
</head>
<body>
<h1>Welcome to Parveen Servlet Application for Image Uploading</h1>
<h2>Welcome</h2>
<label for="email">Email:</label>
<input type="text" id="email" name="email" required>
<br>
<button type="submit">Submit</button>
</form>
</body>
</html>
Web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WebxmlDemo11</param-value>
</context-param>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>in.sp.backend.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SServlet</servlet-name>
<servlet-class>in.sp.backend.SearchServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SServlet</servlet-name>
<url-pattern>/SearchServlet</url-pattern>
</servlet-mapping>
</web-app>
Program No 7.
.
Aim:- To demonstrate the use of Cookies in Servlets by creating a FirstServlet that stores the user's name in a
cookie and passes it to the next Secondservlet.
package com.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
String n = request.getParameter("userName");
out.print("<h1>Welcome " + n + "</h1>");
package com.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
out.close();
}
}
Output:-
Program No 8.
To demonstrate HTTP session tracking using HttpSession in Servlets.
package com.servlets;
// Import statements
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
out.close();
}
}
package com.servlets;
//Import statements
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.*;
if (session != null) {
// Retrieving the stored name
String name = (String) session.getAttribute("uname");
// Printing the name and message
out.print(name + ", you have reached the second page.");
} else {
out.print("No active session found. Please go back and enter your
name.");
}
out.close();
}
}
Program No 9.
Aim:- To demonstrate basic JSP scripting elements and expressions including string manipulation and displaying the
current date using JSP.
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Welcome to JSP File</h1>
<% String name="Parveen Kumar";
int leng=name.length();%>
<h3>Length of String <%=name %> is = <%=leng %><br></h1>
<%= new Date().toString() %>
</body>
</html>
package com.servlets;
//Import statements
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
@WebServlet(urlPatterns="/Login",name="LoginServlet")
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Setting content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.close();
}
}
Output:-
Program No 10.
Aim :- To demonstrate basic Hibernate CRUD operation (Create) using annotation-based
configuration.
Student.java
package com.example;
import jakarta.persistence.*;
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/parveendb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">Pky@1234</property>
<property
name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping class="com.example.Student"/>
</session-factory>
</hibernate-configuration>
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
// Open session
Session session = factory.openSession();
session.beginTransaction();
// Commit transaction
session.getTransaction().commit();
session.close();
Maven Depandency
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.15.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
Output:-
Program No 11.
Aim:- To create a Java RMI-based application that performs basic arithmetic operations remotely using
client-server architecture.
1. Calculator.java
import java.rmi.Remote;
import java.rmi.RemoteException;
CalculatorImpl.java
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
2. CalculatorServer
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
CalculatorClient.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;
int result = 0;
if (operation.equalsIgnoreCase("add")) {
result = calculator.add(num1, num2);
} else if (operation.equalsIgnoreCase("subtract")) {
result = calculator.subtract(num1, num2);
} else {
System.out.println("Invalid operation.");
scanner.close();
return;
}
package creational.singleton;
if (obj1 != obj2) {
throw new IllegalStateException("single pattern failed");
}
System.out.println("object 1 ==> " + obj1);
System.out.println("object 2 ==> " + obj2);
}
}
package creational.singleton;