Professional Documents
Culture Documents
JSP File
JSP File
Nurturing Excellence
Page 2 of 27
Date of Exp.: Exp No.: 1
AIM: Create a webpage that prints your name to the screen, print your name in Tahoma font, print a
definition list with 5 items, Create links to five different pages, etc.
INPUT:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Your Webpage</title>
<style>
body {
font-family: Tahoma, sans-serif;
}
</style>
</head>
<body>
<h1>TOM&JERRY</h1>
<dl>
<dt>JSP</dt>
<dd>It is a server-side technology which is used for creating web
applications.</dd>
<dt>Java</dt>
<dd>Java is used to develop mobile apps, web apps, desktop apps, games and
much more.</dd>
<dt>HTML</dt>
<dd>HTML is the standard markup language for Web pages.</dd>
<dt>CSS</dt>
<dd>CSS describes how HTML elements should be displayed.</dd>
<dt>Javascript</dt>
<dd>JavaScript is a lightweight, interpreted programming language.</dd>
</dl>
<h2>Links to above mentioned topics</h2>
<ul>
<li><a href="page1.html">JSP</a></li>
<li><a href="page2.html">Java</a></li>
Page 3 of 27
<li><a href="page3.html">HTML</a></li>
<li><a href="page4.html">CSS</a></li>
<li><a href="page5.html">Javascript</a></li>
</ul>
</body>
</html>
OUTPUT:
TOM&JERRY
Page 4 of 27
Date of Exp.: Exp No.: 2
AIM: Write a JAVA program to create different shapes and fill colors
INPUT:
package pack;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ShapeDrawer extends JFrame {
public ShapeDrawer() {
setTitle("Shape Drawer");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Adding a custom JPanel to the frame
add(new ShapePanel());
}
public static void main(String[] args) {
// Creating and displaying the frame
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ShapeDrawer().setVisible(true);
}
});
}
}
class ShapePanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//rectangle
g.setColor(Color.RED);
g.fillRect(50, 50, 100, 60);
//ellipse
g.setColor(Color.BLUE);
g.fillOval(200, 50, 100, 60);
//rounded rectangle
g.setColor(Color.GREEN);
g.fillRoundRect(50, 200, 150, 80, 20, 20);
Page 5 of 27
}
}
OUTPUT:
Page 6 of 27
Date of Exp.: Exp No.: 3
INPUT:
package pack;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SwingDemo {
public static void main(String[] args) {
// Creating the main frame
JFrame frame = new JFrame("Swing Demo");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Creating a panel to hold components
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
// Setting the frame visibility
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
// Creating a label
JLabel userLabel = new JLabel("User:");
userLabel.setBounds(10, 20, 80, 25);
panel.add(userLabel);
// Creating a label
JLabel passwordLabel = new JLabel("Password:");
passwordLabel.setBounds(10, 50, 80, 25);
panel.add(passwordLabel);
// Create a button
JButton loginButton = new JButton("Login");
loginButton.setBounds(10, 80, 80, 25);
panel.add(loginButton);
Page 8 of 27
OUTPUT:
Page 9 of 27
Date of Exp.: Exp No.: 4
AIM: Configure Apache Tomcat and write a hello world JSP page.
INPUT:
Page 10 of 27
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World JSP</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
OUTPUT:
Page 11 of 27
Date of Exp.: Exp No.: 5
AIM: Write a java program that connects to a database using JDBC and does add,
delete and retrieve operations.
INPUT:
package demo;
import java.sql.*;
public class Demo {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_demo", "root",
"");
addData(connection, 104, "john cena", 32);
retrieveData(connection);
deleteData(connection,105);
retrieveData(connection);
connection.close();
} catch (Exception e) {
System.out.println(e);
}
}
private static void addData(Connection connection, int id, String name, int age)
throws SQLException {
String addDataSQL = "INSERT INTO student (id, name, age) VALUES (?, ?,
?)";
try (PreparedStatement preparedStatement =
connection.prepareStatement(addDataSQL)) {
preparedStatement.setInt(1, id);
preparedStatement.setString(2, name);
preparedStatement.setInt(3, age);
int rowsAffected = preparedStatement.executeUpdate();
System.out.println(rowsAffected + " row added.");
System.out.println("updated data after adding the data from the database");
}
}
private static void retrieveData(Connection connection) throws SQLException {
String retrieveDataSQL = "SELECT * FROM student";
try (Statement statement = connection.createStatement();
Page 12 of 27
ResultSet resultSet = statement.executeQuery(retrieveDataSQL)) {
while (resultSet.next()) {
System.out.println(resultSet.getInt(1) + " " + resultSet.getString(2) + " " +
resultSet.getString(3));
}
}
}
private static void deleteData(Connection connection, int id) throws
SQLException
{
String deleteDataSQL = "DELETE FROM student WHERE id = ?";
try (PreparedStatement preparedStatement =
connection.prepareStatement(deleteDataSQL)) {
preparedStatement.setInt(1, id);
int rowsAffected = preparedStatement.executeUpdate();
System.out.println(rowsAffected + " row(s) deleted.");
System.out.println("updated data after delection the data from the database");
}
}
}
OUTPUT:
Page 13 of 27
Date of Exp.: Exp No.: 6
AIM: Write a program to display a “Hello World” message in the Web browser. In
addition, display the host name and Session Id. Write JSP code using HTML code.
INPUT:
<!DOCTYPE html>
<html>
<head>
<title>Hello World JSP</title>
</head>
<body>
<h1>Hello World!</h1>
<p>Hostname: <%= java.net.InetAddress.getLocalHost().getHostName()
%></p>
<p>Session ID: <%= session.getId() %></p>
</body>
</html>
OUTPUT:
Page 14 of 27
Date of Exp.: Exp No.: 7
AIM: Write a program to implement a Java Beans to set and get values.
INPUT:
package fileprograms;
public class PersonBean {
private String name;
private int age;
public PersonBean() {
// Empty constructor
}
Page 15 of 27
// Set values using setters
person.setName("TOM&JERRY");
person.setAge(20);
TOM&JERRY
Page 16 of 27
Date of Exp.: Exp No.: 8
INPUT:
Server.java
package fileprograms;
import java.io.*;
import java.net.*;
System.out.println("Client connected.");
Client.java
package fileprograms;
import java.io.*;
import java.net.*;
System.out.println("Client connected.");
Page 18 of 27
outputToClient.println("Message received by the server.");
Page 19 of 27
Date of Exp.: Exp No.: 9
INPUT:
package fileprograms;
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
System.out.println("Client connected.");
Page 20 of 27
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT:
Page 21 of 27
Date of Exp.: Exp No.: 10
AIM: Write a client-server program which displays the server machine's date and
time on the client machine.
INPUT:
package fileprograms;
import java.io.*;
import java.net.*;
OUTPUT:
Page 22 of 27
Date of Exp.: Exp No.: 11
AIM: Write a JSP application to validate the username and password from the
database.
INPUT:
Login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form action="LoginServlet" method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
LoginServlet.jsp
package Raminder;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
Page 24 of 27
OUTPUT:
Page 25 of 27
Date of Exp.: Exp No.: 12
INPUT:
LoginBean.java
public class LoginBean {
private String username;
private String password;
public LoginBean() {
}
Page 26 of 27
LoginBean loginBean = new LoginBean();
// Performing validation
if (loginBean.getUsername().equals("Tom") &&
loginBean.getPassword().equals("jerry")) {
// Successful login, redirect to a success page
response.sendRedirect("success.jsp");
} else {
// Failed login, redirect back to the login page with an error message
response.sendRedirect("login.jsp?error=invalid");
}
}
}
Login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form action="LoginServlet" method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
OUTPUT:
Tomjerry
Page 27 of 27