java
java
Write a java program to scroll the text from left to right and vice versa
continuously
import javax.swing.*;
@Override
public void run() {
while (true) {
try {
Thread.sleep(scrollSpeed);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (leftToRight) {
text = text.substring(1) + text.charAt(0);
} else {
text = text.charAt(text.length() - 1) + text.substring(0,
text.length() - 1);
}
label.setText(text);
repaint();
}
}
public ChatApplication() {
setTitle("Chat Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
inputPanel.add(inputField, BorderLayout.CENTER);
inputPanel.add(sendButton, BorderLayout.EAST);
add(inputPanel, BorderLayout.SOUTH);
setVisible(true);
// Connect to server
try {
socket = new Socket("localhost", 8888); // Change to the server's IP
address and port
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
<%
// Function to check if a number is perfect
boolean isPerfect(int num) {
int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
return sum == num;
}
if (isNumberProvided) {
int number = Integer.parseInt(numberParam);
boolean perfect = isPerfect(number);
%>
<p>Number: <%= number %></p>
<p><%= perfect ? "The number is perfect." : "The number is not
perfect." %></p>
<%
} else {
%>
<form action="" method="get">
Enter a number: <input type="number" name="number">
<input type="submit" value="Check">
</form>
<%
}
%>
</body>
</html>
B) Write a java program in multithreading using applet for drawing flag
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
// Draw ground
g.setColor(Color.GREEN);
g.fillRect(0, getHeight() - 100, getWidth(), 100);
// Draw flag
g.setColor(Color.RED);
g.fillRect(110, getHeight() - flagHeight, 50, flagHeight / 2);
g.setColor(Color.WHITE);
g.fillRect(110, getHeight() - flagHeight + flagHeight / 2, 50, flagHeight /
2);
}
public void stop() {
flagAnimating = false;
}
}
slip 3
A) Write a socket program in Java to check whether given number is prime or not.
Display result on client terminal.
import java.io.*;
import java.net.*;
String inputLine;
while ((inputLine = in.readLine()) != null) {
int number = Integer.parseInt(inputLine);
boolean isPrime = isPrime(number);
out.println(isPrime ? number + " is prime." : number + " is not
prime.");
}
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
B) Write a java program using applet for bouncing ball, for each bounce color of
ball
should change randomly.
import java.applet.*;
import java.awt.*;
import java.util.Random;
repaint();
Thread.sleep(50); // Adjust the animation speed
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
out.println("<html>");
out.println("<head><title>Request Information</title></head>");
out.println("<body>");
// Client information
out.println("<h2>Client Information:</h2>");
out.println("<p>IP Address: " + request.getRemoteAddr() + "</p>");
out.println("<p>Browser Type: " + request.getHeader("User-Agent") +
"</p>");
// Server information
out.println("<h2>Server Information:</h2>");
out.println("<p>Server Name: " + request.getServerName() + "</p>");
out.println("<p>Server Port: " + request.getServerPort() + "</p>");
out.println("<p>Server Software: " + getServletContext().getServerInfo() +
"</p>");
// Servlet information
out.println("<h2>Servlet Information:</h2>");
Enumeration<String> servletNames = getServletContext().getServletNames();
while (servletNames.hasMoreElements()) {
out.println("<p>Servlet Name: " + servletNames.nextElement() + "</p>");
}
out.println("</body></html>");
out.close();
}
}
slip 5
A) Write a JSP program to calculate sum of first and last digit of a given number.
Display sum in Red Color with font size 18.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-
8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sum of First and Last Digit</title>
</head>
<body>
<h1>Sum of First and Last Digit</h1>
<%!
// Function to calculate sum of first and last digits
int sumOfFirstAndLastDigit(int number) {
if (number < 10) {
return number * 2; // If number has only one digit, sum is twice of
it
} else {
int lastDigit = number % 10;
int firstDigit = 0;
while (number >= 10) {
number /= 10;
firstDigit = number;
}
return firstDigit + lastDigit;
}
}
%>
<%
int inputNumber = Integer.parseInt(request.getParameter("number"));
int sum = sumOfFirstAndLastDigit(inputNumber);
%>
<p style="color: red; font-size: 18px;">Sum of first and last digit of <%=
inputNumber %> is <%= sum %></p>
</body>
</html>
B) Write a java program in multithreading using applet for Traffic signal
import java.applet.*;
import java.awt.*;
public BlinkingImage() {
setTitle("Blinking Image");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(imageLabel, BorderLayout.CENTER);
blinking = true;
Thread thread = new Thread(this);
thread.start();
}
@Override
public void run() {
try {
while (blinking) {
Thread.sleep(500); // Blinking interval in milliseconds
imageLabel.setVisible(!imageLabel.isVisible());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@WebServlet("/VisitCounterServlet")
public class VisitCounterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
response.getWriter().println("</body></html>");
}
}
slip 7
A) Write a JSP script to validate given E-Mail ID.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-
8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Email Validation</title>
</head>
<body>
<h1>Email Validation</h1>
<%
String email = request.getParameter("email");
boolean isValid = validateEmail(email);
%>
</body>
</html>
<%!
// Function to validate email
boolean validateEmail(String email) {
if (email == null || email.isEmpty()) {
return false;
} else {
// Regex pattern for email validation
String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-
zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
return email.matches(emailRegex);
}
}
%>
B) Write a Multithreading program in java to display the number’s between 1 to 100
continuously in a TextField by clicking on button. (use Runnable Interface).
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public NumberDisplay() {
setTitle("Number Display");
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(panel);
setVisible(true);
}
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM employees WHERE name LIKE 'A%'";
ResultSet rs = stmt.executeQuery(sql);
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Query executed successfully.");
}
}
B) Write a java program in multithreading using applet for Digital watch.
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.text.*;
try {
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(JDBC_URL, USER, PASS);
// Create table
Statement stmt = conn.createStatement();
stmt.executeUpdate(createTableSQL);
System.out.println("Emp table created successfully");
// Calculate total
int total = Integer.parseInt(product1 != null ? product1 : "0") +
Integer.parseInt(product2 != null ? product2 : "0") +
Integer.parseInt(product3 != null ? product3 : "0") +
Integer.parseInt(product4 != null ? product4 : "0");
%>
<p>Product 1: $<%= product1 != null ? product1 : "0" %></p>
<p>Product 2: $<%= product2 != null ? product2 : "0" %></p>
<p>Product 3: $<%= product3 != null ? product3 : "0" %></p>
<p>Product 4: $<%= product4 != null ? product4 : "0" %></p>
<h2>Total: $<%= total %></h2>
</body>
</html>
slip 10
A) Write a java Program in Hibernate to display “Hello world” message.
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
// Create session
Session session = sessionFactory.openSession();
// Begin transaction
Transaction tx = session.beginTransaction();
// Commit transaction
tx.commit();
// Close session
session.close();
}
}
B) Write a SERVLET program to display the details of Product (ProdCode, PName,
Price) on the browser in tabular format. (Use database)
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/ProductDetailsServlet")
public class ProductDetailsServlet extends HttpServlet {
private static final String JDBC_URL =
"jdbc:mysql://localhost:3306/your_database_name"; // Change the database URL
private static final String USER = "your_username"; // Change the username
private static final String PASS = "your_password"; // Change the password
try {
// Establish database connection
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(JDBC_URL, USER, PASS);
try {
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(JDBC_URL, USER, PASS);
try {
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(JDBC_URL, USER, PASS);
@Override
public void run() {
try {
System.out.println(getName() + " is created.");
// Iterate through the result set and display the details in the
table
while (rs.next()) {
out.println("<tr>");
out.println("<td>" + rs.getInt("CollegeID") + "</td>");
out.println("<td>" + rs.getString("Coll_Name") + "</td>");
out.println("<td>" + rs.getString("Address") + "</td>");
out.println("</tr>");
}
// Close the resources
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</table>
</body>
</html>
slip 14
A) Write a JSP program to accept Name and Age of Voter and check whether he is
eligible for voting or not.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-
8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Voter Eligibility Result</title>
</head>
<body>
<h1>Voter Eligibility Result</h1>
<%
// Retrieve name and age parameters from the request
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
panel.add(new JLabel("Percentage:"));
txtPer = new JTextField();
panel.add(txtPer);
panel.add(new JLabel("Gender:"));
txtGender = new JTextField();
panel.add(txtGender);
panel.add(new JLabel("Class:"));
txtClass = new JTextField();
panel.add(txtClass);
add(panel);
setVisible(true);
}
try {
// Establish database connection
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database_name",
"your_username", "your_password");
// Close resources
pstmt.close();
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
}
<%
// Retrieve username and password from request parameters
String username = request.getParameter("username");
String password = request.getParameter("password");
// Close connection
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// Student details (you can modify these or get them from user input)
Object[][] studentDetails = {
{101, "John", 85.5},
{102, "Alice", 91.2},
{103, "Bob", 78.9},
{104, "Emily", 89.6},
{105, "Michael", 95.3}
};
rs.close();
stmt.close();
}
}
slip 17
A) Write a java program to accept a String from user and display each vowel from a
String after 3 seconds.
import java.util.Scanner;
try {
ServerSocket serverSocket = new ServerSocket(portNumber);
System.out.println("Server is running...");
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected.");
if (file.exists()) {
out.println("File found.");
BufferedReader fileReader = new BufferedReader(new
FileReader(file));
String line;
while ((line = fileReader.readLine()) != null) {
out.println(line);
}
fileReader.close();
} else {
out.println("File not found.");
}
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
try {
Socket socket = new Socket(serverAddress, portNumber);
BufferedReader userInput = new BufferedReader(new
InputStreamReader(System.in));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
System.out.print("Enter file name: ");
String fileName = userInput.readLine();
out.println(fileName);
String response;
while ((response = in.readLine()) != null) {
if (response.equals("File found.")) {
System.out.println("File contents:");
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} else if (response.equals("File not found.")) {
System.out.println("File not found.");
}
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
slip 18
A) Write a java program to calculate factorial of a number. (Use sleep () method).
public class FactorialCalculator {
public static void main(String[] args) {
int number = 5; // Change this to the desired number
return result;
}
}
B) Write a java program for simple standalone chatting application.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ChatServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(5000);
System.out.println("Server started. Waiting for client to connect...");
while (true) {
// Read client's message
clientMessage = in.readLine();
if (clientMessage.equals("exit")) {
break;
}
System.out.println("Client: " + clientMessage);
if (serverMessage.equals("exit")) {
break;
}
}
serverSocket.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
while (true) {
// Send client's message
System.out.print("You: ");
clientMessage = consoleInput.readLine();
out.println(clientMessage);
if (clientMessage.equals("exit")) {
break;
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
slip 19
A) Write a JSP program which accept UserName in a TextBox and greets the user
according to the time on server machine.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-
8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Greeting Page</title>
</head>
<body>
<h1>Greeting Page</h1>
<%-- Generate the greeting message based on the current hour --%>
<% String greetingMessage = "";
if (hour >= 0 && hour < 12) {
greetingMessage = "Good morning, " + userName + "!";
} else if (hour >= 12 && hour < 18) {
greetingMessage = "Good afternoon, " + userName + "!";
} else {
greetingMessage = "Good evening, " + userName + "!";
}
%>
public DisplayFirstRecord() {
setTitle("Display First Record");
setSize(300, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel.add(new JLabel("Percentage:"));
txtPer = new JTextField();
panel.add(txtPer);
add(panel);
setVisible(true);
}
// Prepare SQL query to fetch the first record from student table
String sql = "SELECT * FROM Student LIMIT 1";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
// Close resources
rs.close();
stmt.close();
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
}
try {
// Establish database connection
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database_name",
"your_username", "your_password");
// Close resources
pstmt.close();
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
B) Write a java program in multithreading using applet for drawing temple
import java.applet.*;
import java.awt.*;
// Calculate percentage
double percentage = (totalMarks / 500.0) * 100;
// Close resources
out.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
// Close resources
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
B) Write a JSP program to accept the details of Account (ANo, Type, Bal) and store
it
into database and display it in tabular form.
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Account Details</title>
</head>
<body>
<h2>Enter Account Details</h2>
<form action="SaveAccountDetails.jsp" method="post">
Account Number: <input type="text" name="ano"><br>
Account Type: <input type="text" name="type"><br>
Balance: <input type="text" name="bal"><br>
<input type="submit" value="Submit">
</form>
<h2>Account Details</h2>
<table border="1">
<tr>
<th>Account Number</th>
<th>Account Type</th>
<th>Balance</th>
</tr>
<%
try {
// Establish database connection
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database_name",
"your_username", "your_password");
// Close resources
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</table>
</body>
</html>
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String ano = request.getParameter("ano");
String type = request.getParameter("type");
double bal = Double.parseDouble(request.getParameter("bal"));
try {
// Establish database connection
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database_name",
"your_username", "your_password");
// Close resources
pstmt.close();
conn.close();
response.sendRedirect("AccountDetails.jsp"); // Redirect to
AccountDetails.jsp to display updated account details
} catch (Exception e) {
e.printStackTrace();
}
%>
slip 23
A) Write a Java Program to display the details of College(CID, CName, address,
Year)
on JTable.
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
public CollegeDetails() {
setTitle("College Details");
setSize(600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
// Close resources
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// Close resources
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return userFound;
}
}
slip 24
A) Create a JSP page to accept a number from a user and display it in words:
Example: 123 – One Two Three. The output should be in red color.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-
8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Number to Words Converter</title>
</head>
<body>
<h2>Number to Words Converter</h2>
<form action="" method="post">
Enter a number: <input type="text" name="number">
<input type="submit" value="Convert">
</form>
<%
// Function to convert a digit to word
String toWord(int digit) {
switch(digit) {
case 0: return "Zero";
case 1: return "One";
case 2: return "Two";
case 3: return "Three";
case 4: return "Four";
case 5: return "Five";
case 6: return "Six";
case 7: return "Seven";
case 8: return "Eight";
case 9: return "Nine";
default: return "";
}
}
switch (choice) {
case 1:
insertEmployee();
break;
case 2:
updateEmployee();
break;
case 3:
deleteEmployee();
break;
case 4:
searchEmployee();
break;
case 5:
displayEmployees();
break;
case 6:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please enter a number
between 1 and 6.");
}
} while (choice != 6);
scanner.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database_name",
"your_username", "your_password");
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO Emp (ENo,
EName, Salary, Desg) VALUES (?, ?, ?, ?)");
pstmt.setInt(1, eno);
pstmt.setString(2, ename);
pstmt.setDouble(3, salary);
pstmt.setString(4, desg);
pstmt.executeUpdate();
System.out.println("Employee inserted successfully.");
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void updateEmployee() {
try {
Scanner scanner = new Scanner(System.in);
System.out.println("Update Employee");
System.out.print("Enter Employee Number: ");
int eno = scanner.nextInt();
System.out.print("Enter New Salary: ");
double salary = scanner.nextDouble();
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database_name",
"your_username", "your_password");
PreparedStatement pstmt = conn.prepareStatement("UPDATE Emp SET Salary
= ? WHERE ENo = ?");
pstmt.setDouble(1, salary);
pstmt.setInt(2, eno);
int rowsUpdated = pstmt.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("Employee updated successfully.");
} else {
System.out.println("No employee found with the given Employee
Number.");
}
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database_name",
"your_username", "your_password");
PreparedStatement pstmt = conn.prepareStatement("DELETE FROM Emp WHERE
ENo = ?");
pstmt.setInt(1, eno);
int rowsDeleted = pstmt.executeUpdate();
if (rowsDeleted > 0) {
System.out.println("Employee deleted successfully.");
} else {
System.out.println("No employee found with the given Employee
Number.");
}
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database_name",
"your_username", "your_password");
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM Emp
WHERE ENo = ?");
pstmt.setInt(1, eno);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
System.out.println("Employee Details:");
System.out.println("Employee Number: " + rs.getInt("ENo"));
System.out.println("Employee Name: " + rs.getString("EName"));
System.out.println("Salary: " + rs.getDouble("Salary"));
System.out.println("Designation: " + rs.getString("Desg"));
} else {
System.out.println("No employee found with the given Employee
Number.");
}
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// Close resources
userInput.close();
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
import java.util.*;
// Close resources
in.close();
out.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// Close resources
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
B) Write a SERVLET program to Design an HTML page containing 4 option buttons
(Painting, Drawing, Singing and swimming) and 2 buttons reset and submit. When the
user clicks submit, the server responds by adding cookie containing the selected
hobby
and sends the HTML page to the client. Program should not allow duplicate cookies
to
be written.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Close resources
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
slip 28
A) Write a java program for the implementation of synchronization.
class Counter {
private int count = 0;
public OddNumbersThread(int n) {
this.n = n;
}
public PrimeNumbersThread(int n) {
this.n = n;
}
@WebServlet("/ChangeSessionTimeoutServlet")
public class ChangeSessionTimeoutServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Get the session object
HttpSession session = request.getSession();
response.setContentType("text/html");
response.getWriter().println("<h3>Session timeout changed to 60
seconds</h3>");
}
}
slip 30
A) Write a JSP script to accept a String from a user and display it in reverse
order.
<!DOCTYPE html>
<html>
<head>
<title>Reverse String</title>
</head>
<body>
<h2>Enter a String</h2>
<form action="ReverseString.jsp" method="post">
<input type="text" name="inputString">
<input type="submit" value="Submit">
</form>
<h2>Reversed String</h2>
<%-- Retrieve input string from request parameter --%>
<% String inputString = request.getParameter("inputString"); %>