0% found this document useful (0 votes)
23 views10 pages

Expt 10 Java Manual

javamanual

Uploaded by

20harish08
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)
23 views10 pages

Expt 10 Java Manual

javamanual

Uploaded by

20harish08
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/ 10

AIM :

To implement client and server using datagram socket/stream socket.


SOFTWARE REQUIRED :

S.No Name of the Device Quantity

1 Working PC 1

2 Java JDK 1

ALGORITHM :
Server Side:
1. Create a DatagramSocket instance to listen for incoming datagrams on a specific port.
2. Create a byte array to hold the incoming data.
3. Create a DatagramPacket to receive the incoming data into the byte array.
4. Use the DatagramSocket's receive method to receive the datagram packet.
5. Process the received data.
6. Optionally, create a DatagramPacket to send a response to the client.
7. Use the DatagramSocket's send method to send the response datagram packet.
8. Close the DatagramSocket when done.
Client Side:
1. Create a DatagramSocket instance for sending datagrams.
2. Create a byte array to hold the data to be sent.
3. Create a DatagramPacket with the data and destination address/port.
4. Use the DatagramSocket's send method to send the datagram packet.
5. Optionally, create a DatagramPacket to receive a response from the server.
6. Use the DatagramSocket's receive method to receive the response datagram packet.
7. Process the received response.
8. Close the DatagramSocket when done.
PROGRAM :
DatagramSocketServer.java
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class DatagramSocketServer {
public static void main(String[] args) {
try {
// Create socket to listen on port 1234
DatagramSocket serverSocket = new DatagramSocket(1234);
System.out.println("Server started. Waiting for client message...");
byte[] receiveBuffer = new byte[1024];
// Receive data from client
DatagramPacket receivePacket = new DatagramPacket(receiveBuffer,
receiveBuffer.length);
serverSocket.receive(receivePacket);
String clientMessage = new String(receivePacket.getData(), 0,
receivePacket.getLength());
System.out.println("Client says: " + clientMessage);
// Send response to client
String response = "Hello Client, I received your message!";
byte[] sendBuffer = response.getBytes();
DatagramPacket sendPacket = new DatagramPacket(
sendBuffer,
sendBuffer.length,
receivePacket.getAddress(),
receivePacket.getPort()
);
serverSocket.send(sendPacket);
System.out.println("Response sent to client.");
// Close socket
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

DatagramSocketClient.java
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class DatagramSocketClient {
public static void main(String[] args) {
try {
// Create socket
DatagramSocket clientSocket = new DatagramSocket();
// Send message to server
String message = "Hello Server, this is Client!";
byte[] sendBuffer = message.getBytes();
InetAddress serverAddress = InetAddress.getByName("localhost");
DatagramPacket sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length,
serverAddress, 1234);
clientSocket.send(sendPacket);
System.out.println("Message sent to server: " + message);
// Receive response from server
byte[] receiveBuffer = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveBuffer,
receiveBuffer.length);
clientSocket.receive(receivePacket);
String serverResponse = new String(receivePacket.getData(), 0,
receivePacket.getLength());
System.out.println("Server replied: " + serverResponse);
// Close socket
clientSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

SAMPLE OUTPUT :
Server Side Execution:
Server started. Waiting for client message...
Client says: Hello Server, this is Client!
Response sent to client.
Client Side Execution:
Message sent to server: Hello Server, this is Client!
Server replied: Hello Client, I received your message!

RESULT :
Thus, the client and server communication was successfully implemented using Datagram
sockets in Java.
The client was able to send a message to the server, and the server processed the message and
sent back a response, which was received correctly by the client.
AIM
To implement JDBC Connectivity using Servlet, JSP, and MySQL database.

PROCEDURE
Step 1: Setup the directories
1. Create a folder named exp10 inside C:\xampp\tomcat\webapps (main directory).
2. Inside exp10, create two sub-directories: src and WEB-INF.
3. Save the Java Servlet file JdbcServlet.java inside src.
4. Inside WEB-INF, create two more sub-directories: classes and lib.
o Save web.xml inside WEB-INF.

5. Inside the lib folder, add the required JAR files:


o mysql-connector-j-8.0.32.jar

o servlet-api-7.0.6.jar

Step 2: Create the required files


 HTML files: home.html, register.html
 JSP file: register.jsp
 Servlet file: JdbcServlet.java
 Deployment descriptor: web.xml
Step 3: Execute the files
1. Start XAMPP and run Tomcat.
2. Open browser and go to:
3. http://localhost:8080/exp10/register.html
PROGRAM FILES :
register.html
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<h2>Registration Form</h2>
<form method="post" action="register.jsp">
<label>Username: </label><input type="text" name="username" required><br><br>
<label>Password: </label><input type="password" name="password" required><br><br>
<label>Full Name: </label><input type="text" name="fullname" required><br><br>
<label>Email: </label><input type="email" name="email" required><br><br>
<input type="submit" value="Register">
</form>
</body>
</html>

register.jsp
<%@ page import="java.sql.*" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String fullname = request.getParameter("fullname");
String email = request.getParameter("email");

try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","password");
PreparedStatement pst = con.prepareStatement("INSERT INTO
users(username,password,fullname,email) VALUES(?,?,?,?)");
pst.setString(1, username);
pst.setString(2, password);
pst.setString(3, fullname);
pst.setString(4, email);
int i = pst.executeUpdate();
if(i > 0) {
out.println("You are successfully registered!");
} else {
out.println("Registration failed!");
}
con.close();
} catch(Exception e) {
out.println("Error: " + e.getMessage());
}
%>

JdbcServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class JdbcServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
String fullname = request.getParameter("fullname");
String email = request.getParameter("email");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","password");
PreparedStatement pst = con.prepareStatement("INSERT INTO
users(username,password,fullname,email) VALUES(?,?,?,?)");
pst.setString(1, username);
pst.setString(2, password);
pst.setString(3, fullname);
pst.setString(4, email);
int i = pst.executeUpdate();
if(i > 0) {
out.println("<h3>Registration Successful!</h3>");
} else {
out.println("<h3>Registration Failed!</h3>");
}
con.close();
} catch(Exception e) {
out.println("Error: " + e.getMessage());
}
}
}

web.xml (inside WEB-INF)


<web-app>
<servlet>
<servlet-name>JdbcServlet</servlet-name>
<servlet-class>JdbcServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JdbcServlet</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
</web-app>

DATABASE SETUP
CREATE DATABASE mydb;
USE mydb;
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50),
password VARCHAR(50),
fullname VARCHAR(50),
email VARCHAR(50)
);

SAMPLE OUTPUT :
When you open http://localhost:8080/exp10/register.html and fill details:
Input (form):
Username : john123
Password : john@123
Full Name: John Doe
Email : [email protected]
Browser Output:
You are successfully registered!
Database Output (MySQL):

RESULT :
Thus, JDBC connectivity was successfully implemented using Servlet, JSP, and MySQL
database to perform user registration and store details in the database.

You might also like