Ajp Lab Manual
Ajp Lab Manual
Practical:1
Aim : Implement TCP Server for transferring files using Socket and
ServerSocket
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
int port = 12345; // Specify the port number you want to use
String savePath = "path/to/save/files"; // Specify the directory where you want to save
received files
while (true) {
receiveFile(clientSocket, savePath);
clientSocket.close();
} catch (Exception e) {
e.printStackTrace();
1
Advance Java Programming (3160707) Enrollment No:
try {
int bytesRead;
fileOutputStream.write(buffer, 0, bytesRead);
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
Output:
Server listening on port 12345
2
Advance Java Programming (3160707) Enrollment No:
Practical:2
Aim: Implement cookies to store firstname and lastname using Java server pages
3
Advance Java Programming (3160707) Enrollment No:
4
Advance Java Programming (3160707) Enrollment No:
response.addCookie(firstNameCookie);
response.addCookie(lastNameCookie);
// Redirect to the main page
response.sendRedirect("main.jsp");
%>
Output:
Welcome to the Cookie Example
Hello, John Doe!
5
Advance Java Programming (3160707) Enrollment No:
Practical:3
Aim: Implement the shopping cart for users for the online shopping. Apply the
concept of session.
index.jsp
6
Advance Java Programming (3160707) Enrollment No:
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/AddToCart")
public class AddToCartServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Get the product name from the request parameter
String product = request.getParameter("product");
// Get the user's session
HttpSession session = request.getSession();
// Get the shopping cart from the session
List<String> cart = (List<String>) session.getAttribute("cart");
// Add the selected product to the shopping cart
cart.add(product);
// Redirect back to the main page
response.sendRedirect("index.jsp");
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>AddToCartServlet</servlet-name>
<servlet-class>AddToCartServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddToCartServlet</servlet-name>
<url-pattern>/AddToCart</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
7
Advance Java Programming (3160707) Enrollment No:
Output:
Welcome to Online Shopping
Products:
- Product 1 - $10.00 [Add to Cart]
- Product 2 - $20.00 [Add to Cart]
- Product 3 - $30.00 [Add to Cart]
Your Shopping Cart:
- Product 1
And if the user then adds "Product 2" and "Product 3" to the cart, the output would be:
Welcome to Online Shopping
Products:
- Product 1 - $10.00 [Add to Cart]
- Product 2 - $20.00 [Add to Cart]
- Product 3 - $30.00 [Add to Cart]
Your Shopping Cart:
- Product 1
- Product 2
- Product 3
8
Advance Java Programming (3160707) Enrollment No:
Practical:4
Aim: Implement student registration form with enrollment number, first name,
last name,semester, contact number. Store the details in database. Also
implement search, delete and modify facility for student records.
Form.jsp
pageEncoding="ISO-8859-1"%>
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<form action="Insert.jsp">
<tr>
<td>ENROLLMENT_NUMBER: </td>
</tr>
<tr>
<td>FIRSTNAME: </td>
9
Advance Java Programming (3160707) Enrollment No:
</tr>
<tr>
<td>LASTNAME: </td>
</tr>
<tr>
<td>SEMESTER: </td>
</tr>
<tr>
<td>CONTACT_NUMBER: </td>
</tr>
<tr>
</tr>
</table>
</form>
<a href="Search.jsp">Search</a>
</body>
</html>
Insert.jsp:
pageEncoding="ISO-8859-1" import="java.sql.*"%>
10
Advance Java Programming (3160707) Enrollment No:
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<%
String en = request.getParameter("en");
String fn = request.getParameter("fn");
String ln = request.getParameter("ln");
String se = request.getParameter("se");
String no = request.getParameter("no");
Class.forName("com.mysql.jdbc.Driver");
Connection
c=DriverManager.getConnection("jdbc:mysql://localhost:3306/manual","root","root");
Statement s2 = c.createStatement();
values('"+en+"','"+fn+"','"+ln+"','"+se+"','"+no+"')");
s2.close();
c.close();
response.sendRedirect("Form.jsp");
%>
11
Advance Java Programming (3160707) Enrollment No:
</body>
</html>
Search.jsp:
pageEncoding="ISO-8859-1" import="java.sql.*"%>
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection
c=DriverManager.getConnection("jdbc:mysql://localhost:3306/manual","root","root");
Statement st = c.createStatement();
%>
<tr>
<th>ID</th>
<th>ENROLLMENT_NUMBER</th>
12
Advance Java Programming (3160707) Enrollment No:
<th>FIRSTNAME</th>
<th>LASTNAME</th>
<th>SEMESTER</th>
<th>CONTACT_NUMBER</th>
<th>Action</th>
</tr>
<%
while(rs.next())
int id = rs.getInt("id");
String en = rs.getString("Enrollment");
String fn = rs.getString("Firstname");
String ln = rs.getString("Lastname");
String se = rs.getString("Semester");
String no = rs.getString("Contactno");
%>
<tr>
<td><% out.println(id);%></td>
<td><% out.println(en);%></td>
<td><% out.println(fn);%></td>
<td><% out.println(ln);%></td>
<td><% out.println(se);%></td>
<td><% out.println(no);%></td>
<td><a href="Delete.jsp?x=<%=id%>">Delete</a></td>
13
Advance Java Programming (3160707) Enrollment No:
<td><a href="Edit.jsp?y=<%=id%>">Edit</a></td>
</tr>
<%
%>
</body>
</html>
Edit.jsp:
pageEncoding="ISO-8859-1" import="java.sql.*"%>
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<%
int id = Integer.parseInt(request.getParameter("y"));
int i=0;
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mysql://localhost/manual","root","root");
Statement st = c.createStatement();
14
Advance Java Programming (3160707) Enrollment No:
%>
<%
while(rs.next())
String en = rs.getString("Enrollment");
String fn = rs.getString("Firstname");
String ln = rs.getString("Lastname");
String se = rs.getString("Semester");
String no = rs.getString("Contactno");
%>
<form action="Update.jsp">
<tr>
</tr>
<tr>
<td>ENROLLMENT_NUMBER: </td>
</tr>
<tr>
<td>FIRSTNAME: </td>
15
Advance Java Programming (3160707) Enrollment No:
</tr>
<tr>
<td>LASTNAME: </td>
</tr>
<tr>
<td>SEMESTER: </td>
</tr>
<tr>
<td>CONTACT_NUMBER: </td>
</tr>
<tr>
</tr>
</table>
</form>
<%
c.close();
st.close();
%>
</body>
16
Advance Java Programming (3160707) Enrollment No:
</html>
Update.jsp:
pageEncoding="ISO-8859-1" import="java.sql.*"%>
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<%
String en = request.getParameter("en");
String fn = request.getParameter("fn");
String ln = request.getParameter("ln");
String se = request.getParameter("se");
String no = request.getParameter("no");
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mysql://localhost/manual","root","root");
Statement st =c.createStatement();
Lastname='"+ln+"',
17
Advance Java Programming (3160707) Enrollment No:
st.close();
c.close();
response.sendRedirect("Search.jsp");
%>
</body>
</html>
Delete.jsp:
pageEncoding="ISO-8859-1" import="java.sql.*"%>
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<%
int id = Integer.parseInt(request.getParameter("x"));
Class.forName("com.mysql.jdbc.Driver");
Connection
c=DriverManager.getConnection("jdbc:mysql://localhost:3306/manual","root","root");
Statement st = c.createStatement();
18
Advance Java Programming (3160707) Enrollment No:
st.close();
c.close();
response.sendRedirect("Search.jsp");
%>
</body>
</html>
19
Advance Java Programming (3160707) Enrollment No:
20
Advance Java Programming (3160707) Enrollment No:
21
Advance Java Programming (3160707) Enrollment No:
Practical:5
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/DateTimeServlet")
public class DateTimeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
IOException {
// Set content type
response.setContentType("text/html");
// Get the current date and time
Date currentDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = dateFormat.format(currentDate);
// Prepare the HTML response
PrintWriter out = response.getWriter();
out.println("<html><head><title>System Date and Time</title></head><body>");
out.println("<h2>System Date and Time:</h2>");
out.println("<p>" + formattedDate + "</p>");
out.println("</body></html>");
}
}
Output:
2024-01-05 11:57:
22
Advance Java Programming (3160707) Enrollment No:
Practical:6
Aim: Design a web page that takes the Username from user and if it is a valid
username prints “Welcome Username”. Use JSF to implement.
welcome.xhtml:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<head>
<title>Welcome Page</title>
</head>
<body>
<h:form>
<h:outputLabel for="username">Enter Username:</h:outputLabel>
<h:inputText id="username" value="#{userBean.username}" required="true" />
<h:commandButton value="Submit" action="#{userBean.validateUsername}" />
</h:form>
<h:outputText value="#{userBean.welcomeMessage}" rendered="#{not empty
userBean.welcomeMessage}" />
</body>
</html>
UserBean.java:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class UserBean {
private String username;
private String welcomeMessage;
public String getUsername() {
return username;
}
23
Advance Java Programming (3160707) Enrollment No:
24
Advance Java Programming (3160707) Enrollment No:
Practical:7
Aim: Write Hibernate application to store customer records and retrieve the
customer record including name, contact number, address.
Customer.java:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String contactNumber;
private String address;
// Constructors, getters, and setters
public Customer() {
}
public Customer(String name, String contactNumber, String address) {
this.name = name;
this.contactNumber = contactNumber;
this.address = address;
}
// Getters and setters
}
hibernate.cfg.xml:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD
3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- JDBC Database connection settings -->
25
Advance Java Programming (3160707) Enrollment No:
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/your_database</
property>
<property name="hibernate.connection.username">your_username</property>
<property name="hibernate.connection.password">your_password</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
26
Advance Java Programming (3160707) Enrollment No:
try {
transaction = session.beginTransaction();
session.save(customer);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
}
public Customer getCustomer(Long customerId) {
Session session = sessionFactory.openSession();
Customer customer = null;
try {
customer = session.get(Customer.class, customerId);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return customer;
}
}
MainApplication.java:
public class MainApplication {
public static void main(String[] args) {
// Create a customer
Customer customer = new Customer("John Doe", "1234567890", "123 Main Street");
// Save the customer
CustomerDAO customerDAO = new CustomerDAO();
customerDAO.saveCustomer(customer);
27
Advance Java Programming (3160707) Enrollment No:
28
Advance Java Programming (3160707) Enrollment No:
Practical:8
29
Advance Java Programming (3160707) Enrollment No:
}
StudentView.java
public class StudentView {
public void printStudentDetails(List<StudentModel> students) {
for (StudentModel student : students) {
System.out.println("Student ID: " + student.getStudentId());
System.out.println("Enrollment Number: " + student.getEnrollmentNumber());
System.out.println("Semester: " + student.getSemester());
System.out.println("SPI: " + student.getSpi());
System.out.println("--------------------");
}
}
}
StudentApp.java:
import java.util.List;
import java.util.Scanner;
public class StudentApp {
public static void main(String[] args) {
StudentController controller = new StudentController();
StudentView view = new StudentView();
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("1. Add Student");
System.out.println("2. View All Students");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter Student ID: ");
int studentId = scanner.nextInt();
System.out.print("Enter Enrollment Number: ");
int enrollmentNumber = scanner.nextInt();
30
Advance Java Programming (3160707) Enrollment No:
31
Advance Java Programming (3160707) Enrollment No:
Student ID: 1
Enrollment Number: 2023001
Semester: 3
SPI: 8.5
--------------------
1. Add Student
2. View All Students
3. Exit
Enter your choice: 3
Exiting the application. Goodbye!
32
Advance Java Programming (3160707) Enrollment No:
33