Advanced Java Lab Manual (1)
Advanced Java Lab Manual (1)
IV SEMESTER
ADVANCED JAVA LABORATORY LAB MANUAL
[BIS402]
Compiled By:
Mrs. Punitha M
Assistant Professor,
Dept of Information Science & Engineering, JSSATEB
VISION
To emerge as a centre for achieving academic excellence, by producing competent professionals
to meet the global challenges in the field of Information science and Technology.
MISSION
M1: To prepare the students as competent professionals to meet the advancements in the
industry and academia by imparting quality technical education.
M2: To enrich the technical ability of students to face the world with confidence, commitment,
and teamwork
.
M3: To inculcate and practice strong techno-ethical values to serve the society.
PEO3: To engage in research and development leading to new innovations and products.
PSO3: Demonstrate the knowledge towards the domain specific initiatives of Information
Science and Engineering.
Program Outcomes (POs):
Information Science and Engineering Graduates will be able to:
Course Outcomes:
At the end of the course, students will be able to
CO4 Develop web based applications using Java servlets and JSP. L3
import java.util.ArrayList;
import java.util.Collections;
Output:
ArrayList before sorting:
Apple
Banana
Orange
Mango
Grapes
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;
import java.util.Random;
// Method to generate random numbers between a given range that are multiples of 2 and 5
public static ArrayList<Integer> generateRandomNumbers(int minRange, int maxRange,
int numCount) {
ArrayList<Integer> randomNumbers = new ArrayList<>();
Random random = new Random();
return randomNumbers;
}
Output:
[30, 80, 80, 30, 90, 100, 10, 10, 60, 70]
[100, 10, 10, 30, 30, 60, 70, 80, 80, 90]
import java.util.ArrayList;
class Student {
private String name;
private int age;
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
Output:
Implement a java program to illustrate the use of different types of string class
constructors.
Output:
Implement a java program to illustrate the use of different types of StringBuffer methods
// Append method
stringBuffer.append(" World");
System.out.println("After appending: " + stringBuffer);
// Insert method
stringBuffer.insert(5, ", ");
System.out.println("After inserting: " + stringBuffer);
// Delete method
stringBuffer.delete(5, 8);
System.out.println("After deleting: " + stringBuffer);
// Reverse method
stringBuffer.reverse();
System.out.println("After reversing: " + stringBuffer);
// Replace method
stringBuffer.replace(0, 5, "Hola");
System.out.println("After replacing: " + stringBuffer);
// Capacity method
System.out.println("Capacity of StringBuffer: " + stringBuffer.capacity());
// Length method
System.out.println("Length of StringBuffer: " + stringBuffer.length());
// EnsureCapacity method
stringBuffer.ensureCapacity(50);
System.out.println("Capacity after ensuring: " + stringBuffer.capacity());
// SetLength method
stringBuffer.setLength(10);
System.out.println("StringBuffer after setting length: " + stringBuffer);
}
}
Output:
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.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public ButtonEventHandlingDemo() {
setTitle("Button Event Handling Demo");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Creating buttons
alphaButton = new JButton("Alpha");
betaButton = new JButton("Beta");
// Setting layout
setLayout(new FlowLayout());
Output:
A program to display greeting message on the browser “Hello UserName”, “How Are
You?”, accept username from the client using servlet.
HTML Code
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="GreetingServlet" method ="post">
<p>Enter the Username: <input type="text" name="username"> </p>
<p> <input type="submit" value="submit"></p>
</form>
</body>
</html>
Servlet Code
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Chetan
*/
public class GreetingServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
out.println("<html>");
out.println("<head>");
out.println("<title>Greeting Message</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>Hello " + username + "</h2>");
out.println("<p>How Are You?</p>");
out.println("</body>");
out.println("</html>");
}
Output:
A servlet program to display the name, USN, and total marks by accepting student detail
HTML Code
<!DOCTYPE html>
<html>
<head>
<title>Student Details Form</title>
</head>
<body>
<h2>Enter Student Details</h2>
<form action="StudentDetailsServlet" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="usn">USN:</label>
<input type="text" id="usn" name="usn" required><br><br>
Servlet Code
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Chetan
*/
public class StudentDetailsServlet extends HttpServlet {
/**
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Student Details</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>Student Details</h2>");
out.println("<p>Name: " + name + "</p>");
out.println("<p>USN: " + usn + "</p>");
out.println("<p>Total Marks: " + totalMarks + "</p>");
out.println("</body>");
out.println("</html>");
}
A Java program to create and read the cookie for the given cookie name as “EMPID” and its
value as “AN2356”.
import java.io.IOException;
import java.io.PrintWriter;
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("/CookieExample")
public class CookieExample extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
}
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
Output
Write a JAVA Program to insert data into Student DATA BASE and retrieve info
based on particular queries(For example update, delete, search etc…).
package studentdatabase;
import java.sql.*;
} catch (SQLException e) {
e.printStackTrace();
}
}
Output:
A new student has been inserted successfully!
Student found:
Name: John
Age: 22
Department: ISE
Student's age has been updated successfully!
Student has been deleted successfully!
A program to design the Login page and validating the USER_ID and PASSWORD
using JSP and DataBase.
Login.jsp
<body>
<h2>Login</h2>
<form action="loginvalidation.jsp" method="post">
<label for="userId">User ID:</label>
<input type="text" id="userId" name="userId" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
LoginValidation.jsp
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Validation</title>
</head>
<body>
<%
String userId = request.getParameter("userId");
String password = request.getParameter("password");
boolean valid = false;
// JDBC variables
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// Establishing database connection
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, dbUser, dbPassword);
Welcome.jsp
Output: