PRACTICAL 1
AIM :- WAP Setting an Icon for a Frame in Java using Swing?
import javax.swing.*;
import java.awt.*;
public class FrameIconExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Frame Icon Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
// Set the icon
ImageIcon icon = new ImageIcon("icon.png"); // Replace with your image file path
frame.setIconImage(icon.getImage());
frame.setVisible(true);
}
PRACTICAL 2
AIM :- WAP Show Dialog Box in Java using Swing Dialogs?
import javax.swing.*;
public class DialogBoxExample {
public static void main(String[] args) {
// Create a frame (required for dialogs to be attached)
JFrame frame = new JFrame("Dialog Box Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
// Show a simple dialog box
JOptionPane.showMessageDialog(frame, "Hello! This is a dialog box.");
// Optional: Close the frame after showing dialog
frame.dispose();
}
}
PRACTICAL 3
import javax.swing.*;
public class MessageAndConfirmDialogExample {
public static void main(String[] args) {
// Create a frame (required for dialogs)
JFrame frame = new JFrame("Message and Confirm Dialog Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
// Show a Message Dialog
JOptionPane.showMessageDialog(frame, "This is a Message Dialog!", "Message",
JOptionPane.INFORMATION_MESSAGE);
// Show a Confirm Dialog
int response = JOptionPane.showConfirmDialog(frame, "Do you want to continue?", "Confirm",
JOptionPane.YES_NO_OPTION);
// Check the user's response
if (response == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(frame, "You chose YES!");
} else if (response == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(frame, "You chose NO!");
} else {
JOptionPane.showMessageDialog(frame, "You closed the dialog without choosing!");
// Close the frame after showing dialogs
frame.dispose();
}
PRACTICAL 4
2. WAP Changing the Label of a JButton Component in Java using Swing?
import javax.swing.*;
import java.awt.event.*;
public class ChangeButtonLabelExample {
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("Change Button Label Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setLayout(null); // using absolute layout
// Create a button
JButton button = new JButton("Click Me");
button.setBounds(90, 50, 120, 40);
// Add ActionListener to change label when clicked
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button.setText("Clicked!");
}
});
// Add button to the frame
frame.add(button);
// Set frame visible
frame.setVisible(true);
}
}
OUTPUT
PRACTICAL 5
3. WAP Setting Multi-Line label on the Button using Swing?
import javax.swing.*;
public class MultiLineButtonExample {
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("Multi-Line Button Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setLayout(null); // Absolute layout
// Create a button with multi-line text using HTML
JButton button = new JButton("<html>This is<br>Multi-Line<br>Button</html>");
button.setBounds(70, 50, 160, 80);
// Add the button to frame
frame.add(button);
// Set frame visible
frame.setVisible(true);
Practical 5
4. WAP Setting Multi-Line label on the Button using Swing?
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.Font;
public class MultiLineButtonStyled {
public static void main(String[] args) {
JFrame frame = new JFrame("Styled Multi-Line Button");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 250);
frame.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 40)); // Center aligned layout
// Create a JButton with multi-line HTML text
JButton button = new JButton("<html><div style='text-align: center;'>First Line<br>Second
Line</div></html>");
// Set bigger font for the button text
button.setFont(new Font("Arial", Font.BOLD, 18));
frame.add(button);
frame.setVisible(true);
}
}
Practical 6
5. WAP Setting icon on the button in Java using Swing?
import javax.swing.*;
import java.awt.*;
public class ButtonIconExample {
public static void main(String[] args) {
// Create a new frame
JFrame frame = new JFrame("Button Icon Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());
// Load the image as an icon
ImageIcon icon = new ImageIcon("settings.png"); // Make sure the file is in the project
directory
// Create a button with text and icon
JButton button = new JButton("Settings", icon);
// Optionally, you can set text position relative to the icon
button.setHorizontalTextPosition(SwingConstants.CENTER);
button.setVerticalTextPosition(SwingConstants.BOTTOM);
// Add the button to the frame
frame.add(button);
// Set frame visibility
frame.setVisible(true);
}
}
Practical 7
. WAP Changing the Label of a JButton Component in Java using Swing?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChangeButtonLabel {
public static void main(String[] args) {
JFrame frame = new JFrame("Button Label Change");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());
JButton button = new JButton("Click Me");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button.setText("Clicked!");
}
});
frame.add(button);
frame.setVisible(true);
}
}
Practical 8
WAP Making a Frame Non Resizable in Java using Swing?
import javax.swing.*;
public class NonResizableFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("Non-Resizable Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
// Make the frame non-resizable
frame.setResizable(false);
frame.setVisible(true);
}
}
Practical 9
WAP Remove Minimize and Maximize Button of Frame in Java using Swing?
import javax.swing.*;
import java.awt.*;
public class RemoveMinMaxButtons {
public static void main(String[] args) {
JFrame frame = new JFrame("Remove Minimize and Maximize Buttons");
// Set default close operation and size
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
// Disable resizing of the frame
frame.setResizable(false);
// Remove maximize and minimize buttons only, keep the title bar
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setVisible(true);
}
}
Practical 10
WAP Removing the Title Bar of a Frame using Swing?
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.FlowLayout;
public class RemoveTitleBarExample {
public static void main(String[] args) {
// Create a new frame
JFrame frame = new JFrame();
// Remove the title bar and borders
frame.setUndecorated(true);
// Set layout manager
frame.setLayout(new FlowLayout());
// Add a label
frame.add(new JLabel("Frame without Title Bar!"));
// Set size of the frame
frame.setSize(300, 200);
// Close the application when frame is closed
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Center the frame on screen
frame.setLocationRelativeTo(null);
// Make frame visible
frame.setVisible(true);
}
}
Pratical 11
WAP Setting Bounds for a maximized frame using Swing?
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Toolkit;
public class MaximizeFrameWithBounds {
public static void main(String[] args) {
// Create a new frame
JFrame frame = new JFrame("Maximized Frame with Custom Bounds");
// Set default close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Maximize the frame
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
// Get the screen size
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// Set custom bounds (for example, center 80% width and height)
int width = (int) (screenSize.width * 0.8);
int height = (int) (screenSize.height * 0.8);
int x = (screenSize.width - width) / 2;
int y = (screenSize.height - height) / 2;
// Set the bounds
frame.setBounds(x, y, width, height);
// Make the frame visible
frame.setVisible(true);
}
}
Practical 12
Write a java Program to create a simple servlet and run it using tomcat server?
// HelloWorldServlet.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;
// This servlet will handle requests to /hello
@WebServlet("/hello")
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// Handles GET requests (when you open it in browser)
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html><head><title>Hello Servlet</title></head>");
out.println("<body>");
out.println("<h1>Hello, World! This is a simple servlet running on Tomcat!</h1>");
out.println("</body></html>");
}
}
Practical 13
Write a java Program to create a servlet to read information from client Registration page?
1. register.html
<!DOCTYPE html>
<html>
<head>
<title>User Registration</title>
</head>
<body>
<h2>Register Here</h2>
<form action="register" method="post">
<label>First Name:</label>
<input type="text" name="firstName" required><br><br>
<label>Last Name:</label>
<input type="text" name="lastName" required><br><br>
<label>Email:</label>
<input type="email" name="email" required><br><br>
<label>Password:</label>
<input type="password" name="password" required><br><br>
<input type="submit" value="Register">
</form>
</body>
</html>
2. RegistrationServlet.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("/register")
public class RegistrationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Reading form data from client
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String email = request.getParameter("email");
String password = request.getParameter("password"); // Should be encrypted/stored
safely!
// Setting response type
response.setContentType("text/html");
// Sending back response
PrintWriter out = response.getWriter();
out.println("<html><head><title>Registration Successful</title></head><body>");
out.println("<h2>Thank You for Registering!</h2>");
out.println("<p><strong>First Name:</strong> " + firstName + "</p>");
out.println("<p><strong>Last Name:</strong> " + lastName + "</p>");
out.println("<p><strong>Email:</strong> " + email + "</p>");
out.println("</body></html>");
}
}
Practical 14
Write a java Program to create a JSP page to display a simple message along with current
Date?
<%@ page import="java.util.Date" %>
<!DOCTYPE html>
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h1>Welcome to My Website!</h1>
<p>Current Date and Time is: <strong><%= new Date() %></strong></p>
</body>
</html>
Practical 15
Write a java Program to create a JSP page to display the random number?
<%@ page import="java.util.Random" %>
<!DOCTYPE html>
<html>
<head>
<title>Random Number Generator</title>
</head>
<body>
<h1>Random Number Generator</h1>
<p>Your Random Number is: <strong><%= new Random().nextInt(1000) %></strong></p>
</body>
</html>
Practical 16
Write a java Program to create a User request page in JSP?
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>User Request Page</title>
</head>
<body>
<h2>Welcome! Please Enter Your Name</h2>
<form method="post" action="userRequest.jsp">
<input type="text" name="username" placeholder="Enter your name" required>
<input type="submit" value="Submit">
</form>
<%
// Read the submitted name
String name = request.getParameter("username");
if (name != null && !name.trim().isEmpty()) {
%>
<h3>Hello, <%= name %>! Welcome to our website.</h3>
<%
}
%>
</body>
</html>