AdvancedJava(BIS402)
AdvancedJava(BIS402)
1. Implement a java program to demonstrate creating and ArrayList, adding elements, removing
elements, sorting elements of ArrayList. Also illustrate the use of toArray() method.
import java.util.ArrayList;
import java.util.Collections;
Output:
2. Develop a java program to read random numbers between a given range that are multiple of 2
and 5 the numbers according to tens place using comparator
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Output:
import java.util.LinkedList;
class Address
{
private String name; private String street; private String city; private String state; private String
code;
Address(String n, String s, String c, String st, String cd)
{
name = n; street = s; city = c; state = st; code = cd;
}
public String toString()
{
return name + "\n" + street + "\n" + city + " " + state + " " +code;
}
}
public class Main
{
public static void main(String args[])
{
LinkedList<Address> ml = new LinkedList<Address>();
Output:
5. Implement a java program to illustrate the use of different types of character extraction, string
comparison, string search and string modification methods.
Output:
// 2. Insert method
sb.insert(5, " Java");
System.out.println("After insert: " + sb);
// 3. Replace method
sb.replace(6, 10, "C++");
System.out.println("After replace: " + sb);
// 4. Delete method
sb.delete(5, 9);
System.out.println("After delete: " + sb);
// 5. Reverse method
sb.reverse();
System.out.println("After reverse: " + sb);
// Resetting the StringBuffer for further methods
sb.reverse(); // To restore the original state
// 6. Capacity method
System.out.println("Capacity: " + sb.capacity());
// 9. Char at method
char ch = sb.charAt(1);
System.out.println("Char at index 1: " + ch);
Output:
7. Demonstrate a swing event handling application that create 2 buttons Alpha and Beta display
the text “Alpha Pressed” when alpha button is clicked and “Beta pressed” when beta button is
clicked.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
betaButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
messageLabel.setText("Beta Pressed");
}
});
// Add the buttons and label to the panel
panel.add(alphaButton);
panel.add(betaButton);
panel.add(messageLabel);
// Add the panel to the frame
frame.add(panel);
// Make the frame visible
frame.setVisible(true);
Output:
8. A Program to display greeting message on the browser “Hello UserName”, “How are you?”,
accept username from the client using servlet.
import java.io.*;
import jakarta.servlet.*; import jakarta.servlet.http.*;
public class TestServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)throws
ServletException,IOException
{
PrintWriter out=res.getWriter(); out.println("Hello userNamr"); out.println("How are you");
}
}
<web−app>
<servlet>
<servlet−name>Test</servlet−name>
<servlet−class>TestServlet</servlet−class>
</servlet>
<servlet−mapping>
<servlet−name>Test</servlet−name>
<url−pattern>/test</url−pattern>
</servlet−mapping>
</web−app>
Output:
Setup path Steps:
YourProject/
│
├── src/
│ └── com.example/
│ └── StudentServlet.java
├── WebContent/
│ ├── index.html
│ └── WEB-INF/
│ └── web.xml
index.html
<!DOCTYPE html>
<html>
<head>
<title>Student Details Form</title>
</head>
<body>
<h2>Enter Student Details</h2>
<form action="StudentServlet" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="usn">USN:</label>
<input type="text" id="usn" name="usn"><br><br>
<label for="marks">Total Marks:</label>
<input type="number" id="marks" name="marks"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
StudentServlet.java:
package com.example;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet { private static final long serialVersionUID = 1L;
<web−app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema−instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web−app_3_0.xsd" version="3.0">
<servlet>
<servlet−name>StudentServlet</servlet−name>
<servlet−class>com.example.StudentServlet</servlet−class>
</servlet>
<servlet−mapping>
<servlet−name>StudentServlet</servlet−name>
<url−pattern>/StudentServlet</url−pattern>
</servlet−mapping>
</web−app>
Output:
YourProject/
│
├── src/
│ └── com.example/
│ ├── SetCookieServlet.java
│ └── ReadCookieServlet.java
├── WebContent/
│ ├── index.html
│ └── WEB−INF/
│ └── web.xml
index.html
<!DOCTYPE html>
<html>
<head>
<title>Cookie Example</title>
</head>
<body>
<h2>Cookie Example</h2>
<p><a href="SetCookieServlet">Set Cookie</a></p>
<p><a href="ReadCookieServlet">Read Cookie</a></p>
</body>
</html>
SetCookieServlet.java:
package com.example;
@WebServlet("/SetCookieServlet")
public class SetCookieServlet extends HttpServlet { private static final long serialVersionUID = 1L;
To create and read a cookie with the name "EMPID" and value "AN2356" in a Java web application
using servlets, you need to perform the following steps:
Create a Servlet to Set the Cookie:
This servlet will create a cookie with the name "EMPID" and value "AN2356" and send it to the client.
Create a Servlet to Read the Cookie:
This servlet will read the cookies sent by the client and display the value of the "EMPID" cookie.
Directory Structure:
css
Copy code YourProject/
│
├── src/
│ └── com.example/
│ ├── SetCookieServlet.java
│ └── ReadCookieServlet.java
├── WebContent/
│ ├── index.html
│ └── WEB−INF/
│ └── web.xml
SetCookieServlet.java:
This servlet sets a cookie with the name "EMPID" and value "AN2356". java
Copy code
package com.example;
import java.io.IOException;
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import
javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@WebServlet("/SetCookieServlet")
public class SetCookieServlet extends HttpServlet { private static final long serialVersionUID = 1L;
// Set the maximum age of the cookie to 24 hours (86400 seconds) empIdCookie.setMaxAge(86400);
// Notify the user that the cookie has been set response.setContentType("text/html");
response.getWriter().println("<html><body><h2>Cookie 'EMPID' with value 'AN2356' has been
set.</h2></body></html>");
}
}
ReadCookieServlet.java:
This servlet reads the cookies sent by the client and displays the value of the "EMPID" cookie. java
Copy code
package com.example;
import java.io.IOException;
import javax.servlet.ServletException;
@WebServlet("/ReadCookieServlet")
public class ReadCookieServlet extends HttpServlet { private static final long serialVersionUID = 1L;
// Initialize a variable to hold the value of the "EMPID" cookie String empIdValue = "Cookie not found";
// Check if cookies are present and search for the "EMPID" cookie if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("EMPID")) { empIdValue = cookie.getValue(); break;
}
}
}
web.xml:
<web−app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema−instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web−app_3_0.xsd" version="3.0">
<servlet>
<servlet−name>SetCookieServlet</servlet−name>
11. Write a JAVA Program to insert data into Student DATA BASE and Retrieve info based on
particular queries. (For Example, update, delete, search etc...)
Database Setup:
Install a database system like MySQL or PostgreSQL.
Create a database named studentdb.
Create a table named students with the following structure:
import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.ResultSet;
import java.sql.SQLException; import java.util.ArrayList; import java.util.List;
public void updateStudent(String usn, int marks) throws SQLException { String sql = "UPDATE students
SET marks = ? WHERE usn = ?";
try (Connection conn = JDBCUtil.getConnection(); PreparedStatement pstmt =
conn.prepareStatement(sql)) { pstmt.setInt(1, marks);
pstmt.setString(2, usn); pstmt.executeUpdate();
}
}
public void deleteStudent(String usn) throws SQLException { String sql = "DELETE FROM students
WHERE usn = ?";
try (Connection conn = JDBCUtil.getConnection(); PreparedStatement pstmt =
conn.prepareStatement(sql)) { pstmt.setString(1, usn);
pstmt.executeUpdate();
}
}
public Student getStudentByUsn(String usn) throws SQLException { String sql = "SELECT * FROM
students WHERE usn = ?";
try (Connection conn = JDBCUtil.getConnection(); PreparedStatement pstmt =
conn.prepareStatement(sql)) { pstmt.setString(1, usn);
ResultSet rs = pstmt.executeQuery(); if (rs.next()) {
return new Student(rs.getInt("id"), rs.getString("name"), rs.getString("usn"), rs.getInt("marks"));
}
}
return null;
}
public class Student { private int id; private String name; private String usn; private int marks;
public Student(int id, String name, String usn, int marks) { this.id = id;
this.name = name; this.usn = usn; this.marks = marks;
}
@Override
public String toString() {
return "Student{id=" + id + ", name='" + name + "', usn='" + usn + "', marks=" + marks + "}";
}
}
try {
// Insert a new student studentDAO.insertStudent("John Doe", "AN2356", 90);
System.out.println("Student inserted.");
Output:
12. A Program to design the Login page and validating the USER_ID and PASSWORD using JSP and
Database.
Database Setup:
JDBC Driver:
Directory Structure:
YourProject/
│
├── src/
│ └── com.example/
│ └── JDBCUtil.java
├── WebContent/
│ ├── login.jsp
│ ├── login-success.jsp
│ ├── login-fail.jsp
│ └── WEB-INF/
│ └── web.xml
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>Login</h2>
<form action="login−validate.jsp" method="post">
<label for="user_id">User ID:</label>
<input type="text" id="user_id" name="user_id"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
if (isValid) {
response.sendRedirect("login−success.jsp");
} else {
response.sendRedirect("login−fail.jsp");
}
%>
<!DOCTYPE html>
<html>
<head>
<title>Login Successful</title>
</head>
<body>
<h2>Login Successful</h2>
<p>Welcome, <%= request.getParameter("user_id") %>!</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Login Failed</title>
</head>
<body>
<h2>Login Failed</h2>
<p>Invalid User ID or Password. Please try again.</p>
</body>
</html>
<web−app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema−instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web−app_3_0.xsd" version="3.0">
<servlet>
<servlet−name>jsp</servlet−name>
<servlet−class>org.apache.jasper.servlet.JspServlet</servlet−class>
<init−param>
<param−name>fork</param−name>
<param−value>false</param−value>
</init−param>
<load−on−startup>3</load−on−startup>
</servlet>
<servlet−mapping>
<servlet−name>jsp</servlet−name>
<url−pattern>*.jsp</url−pattern>
</servlet−mapping>
</web−app>
Output: