Advanced Java Manual
Advanced Java Manual
import java.util.ArrayList;
import java.util.Collections;
list.add(5);
list.add(3);
list.add(8);
list.add(1);
list.remove(2);
Collections.sort(list);
arr = list.toArray(arr);
System.out.print("Array: ");
OUTPUT:
Array: 1 3 5
2. Develop a program to read random numbers between a given range that are multiples of
2 and 5, sort the numbers according to tens place using comparator.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
if(i % 2 == 0 && i % 5 == 0) {
numbers.add(i);
@Override
});
System.out.println("Sorted numbers: " + numbers);
OUTPUT:
Sorted numbers: [10, 20, 50, 40, 30, 60, 70, 80, 90, 100]
3. Implement a java program to illustrate storing user defined classes in collection.
import java.util.ArrayList;
class Student {
String name;
int rollNo;
this.name = name;
this.rollNo = rollNo;
}
}
OUTPUT:
String str4 = new String(charArray, 0, 2); // String from part of character array
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println(str4);
5 Implement a java program to illustrate the use of different types of character extraction,
string comparison, string search and string modification methods.
// Character extraction
// String search
// String modification
OUTPUT:
str1:
str2: Hello
str3: Hello
str4: He
6 Implement a java program to illustrate the use of different types of StringBuffer methods
// Append method
buffer.append(" world");
// Insert method
buffer.insert(5, ",");
// Delete method
// Reverse method
buffer.reverse();
System.out.println(buffer);
OUTPUT:
dlrow ,olleH
7 Demonstrate a swing event handling application that creates 2 buttons Alpha and Beta
and displays the text “Alpha pressed” when alpha button is clicked and “Beta pressed”
when beta button is clicked.
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
alphaButton.addActionListener(new ActionListener() {
});
betaButton.addActionListener(new ActionListener() {
});
frame.getContentPane().add(alphaButton);
frame.getContentPane().add(betaButton);
frame.pack();
frame.setVisible(true);
Here's a Java Swing program that demonstrates event handling for button clicks:
```java
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton alphaButton = new JButton("Alpha");
alphaButton.addActionListener(new ActionListener() {
});
betaButton.addActionListener(new ActionListener() {
});
frame.getContentPane().add(alphaButton);
frame.getContentPane().add(betaButton);
frame.pack();
frame.setVisible(true);
```Output:
Upon clicking the "Alpha" button, a dialog box with the message "Alpha pressed" will be
displayed. Similarly, clicking the "Beta" button will display a dialog box with the message "Beta
pressed".
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 javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
out.println("<html><body>");
out.println("</body></html>");
9 A servlet program to display the name, USN, and total marks by accepting student detail
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
out.println("<html><body>");
out.println("<h2>Student Details:</h2>");
out.println("</body></html>");
OUTPUT: Here's a simple servlet program that displays a greeting message on the browser,
accepting the username from the client:
```java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/GreetingServlet")
response.setContentType("text/html");
out.println("<html><body>");
out.println("</body></html>");
Output:
When you access this servlet through a web browser, it will display a form prompting for a
username. After submitting the form, it will display a greeting message like "Hello [username],
How Are You?".
If you want to test this servlet, you need to deploy it on a servlet container like Apache Tomcat
and access it through a web browser.
10 A Java program to create and read the cookie for the given cookie name as “EMPID”
and its value as “AN2356”.
import javax.servlet.http.*;
import java.io.*;
response.addCookie(cookie);
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...).
import java.sql.*;
try {
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username",
"password");
// Insert data
stmt.executeUpdate(insertQuery);
// Retrieve data
ResultSet rs = stmt.executeQuery(selectQuery);
while(rs.next()) {
conn.close();
} catch (Exception e) {
System.out.println(e);
OUTPUT:
To create a login page validation using JSP and database, you'll need to follow these steps:
● Setup Database: First, you need to set up a database to store user credentials. You can use
MySQL, PostgreSQL, or any other database of your choice.
● Create Table: Create a table in your database to store user information like username and
password.
● Configure Database Connection: Create a Java class to handle the database connection.
This class will have methods to connect to the database, insert user data, and retrieve user
data.
● Create JSP Files: Create JSP files for the login page and for handling login validation.
● Servlet for Validation: Create a servlet to handle form submission from the login page.
This servlet will validate the username and password against the database.
● Deployment: Deploy your application on a server. You can use Apache Tomcat or any
other servlet container.
1. Database Configuration Class (DBConnection.java):
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/login")
String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
statement.setString(1, username);
statement.setString(2, password);
if (result.next()) {
// Successful login
response.sendRedirect("welcome.jsp");
} else {
// Invalid credentials
response.sendRedirect("login.jsp?error=1");
ex.printStackTrace();
response.sendRedirect("login.jsp?error=2");
}
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>Login</h2>
<% } %>
<% } %>
<label for="username">Username:</label>
<label for="password">Password:</label>
<button type="submit">Login</button>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>Welcome</h2>
</body>
</html>
OUTPUT:
Designing a login page with JSP and validating the user_id and password against a database
involves multiple files and configurations. I'll provide you with a basic example to get you
started.
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
```
```java
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/login")
String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
statement.setString(1, username);
statement.setString(2, password);
if (result.next()) {
// Successful login
response.sendRedirect("welcome.jsp");
} else {
// Invalid credentials
response.sendRedirect("login.jsp?error=1");
ex.printStackTrace();
response.sendRedirect("login.jsp?error=2");
}
```
```jsp
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>Login</h2>
<% } %>
<% } %>
<label for="username">Username:</label>
<label for="password">Password:</label>
<button type="submit">Login</button>
</form>
</body>
</html>
```
```jsp
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>Welcome</h2>
</body>
</html>
```
OUTPUT:
- Make sure you have a MySQL database set up with a table named `users` containing
`username` and `password` columns.