0% found this document useful (0 votes)
34 views27 pages

JSP File

Uploaded by

Johna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views27 pages

JSP File

Uploaded by

Johna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

GURU GOBIND SINGH INDRAPRASTHA UNIVERSITY

INSTITUTE OF INNOVATION IN TECHNOLOGY & MANAGEMENT


WEB DEVELOPMENT USING JAVA LAB
PRACTICAL FILE
BCA-317

Nurturing Excellence

Submitted To: Submitted By:


Mr. Dhruv Srivastava Name: Kartikeya Aggarwal
Associate Professor Class: BCA V (E1)
Enrolment no: 00824402021
Lab Date on which PD Remarks
Problem Statement
No Completed
Create a webpage that prints your name to the
screen, print your name in Tahoma font, print a
1 definition list with 5 items, Create links to five
different pages, etc.

Write a JAVA program to create different shapes


2 and fill colors

3 Program to demonstrate Swing components.

Configure Apache Tomcat and write a hello world


4 JSP page.

Write a java program that connects to a database


5 using JDBC and does add, delete and retrieve
operations

Write a program to display a “Hello World”


message in the Web browser. In addition, display
6 the host name and Session Id. Write JSP code
using HTML code.

Write a program to implement a Java Beans to set


7 and get values

Create a Java application to demonstrate Socket


8 Programming in Java.

Write a program to retrieve hostname--using


9 methods in Inetaddress class

Write a client-server program which displays the


10 server machine's date and time on the client
machine

Write a JSP application to validate the username


11 and password from the database.

Write a program to create a login page using Java


12 Beans.

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

AIM: Program to demonstrate Swing components.

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 text field


JTextField userText = new JTextField(20);
userText.setBounds(100, 20, 165, 25);
panel.add(userText);

// Creating a label
JLabel passwordLabel = new JLabel("Password:");
passwordLabel.setBounds(10, 50, 80, 25);
panel.add(passwordLabel);

// Creating a password field


Page 7 of 27
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100, 50, 165, 25);
panel.add(passwordText);

// Create a button
JButton loginButton = new JButton("Login");
loginButton.setBounds(10, 80, 80, 25);
panel.add(loginButton);

// Creating a text area


JTextArea resultArea = new JTextArea();
resultArea.setBounds(10, 110, 300, 100);
panel.add(resultArea);

// Adding action listener to the button


loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Performing an action when the button is clicked
String username = userText.getText();
String password = new String(passwordText.getPassword());

// Displaying the result in the text area


resultArea.setText("Username: " + username + "\nPassword: " +
password);
}
});
}
}

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
}

// Setter method for name


public void setName(String name) {
this.name = name;
}

// Getter method for name


public String getName() {
return name;
}

// Setter method for age


public void setAge(int age) {
this.age = age;
}

// Getter method for age


public int getAge() {
return age;
}
}
TestPersonBean.java
package fileprograms;
public class TestPersonBean {
public static void main(String[] args) {
// Create an instance of PersonBean
PersonBean person = new PersonBean();

Page 15 of 27
// Set values using setters
person.setName("TOM&JERRY");
person.setAge(20);

// Get values using getters and display


System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
}
}
OUTPUT:

TOM&JERRY

Page 16 of 27
Date of Exp.: Exp No.: 8

AIM: Create a Java application to demonstrate Socket Programming in Java.

INPUT:
Server.java
package fileprograms;
import java.io.*;
import java.net.*;

public class Server {


public static void main(String[] args) {
try {
// Creating a server socket on port 12345
ServerSocket serverSocket = new ServerSocket(12345);

System.out.println("Server has started. Waiting for a client...");

// Accepting client connection


Socket clientSocket = serverSocket.accept();

System.out.println("Client connected.");

// Getting input and output streams for communication


BufferedReader inputFromClient = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
PrintWriter outputToClient = new
PrintWriter(clientSocket.getOutputStream(), true);

// Reading data from the client


String receivedMessage = inputFromClient.readLine();
System.out.println("Received from client: " + receivedMessage);

// Sending a response back to the client


outputToClient.println("Message received by the server.");

// Closing streams and sockets


inputFromClient.close();
outputToClient.close();
clientSocket.close();
serverSocket.close();
Page 17 of 27
} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT:

Client.java
package fileprograms;
import java.io.*;
import java.net.*;

public class Server {


public static void main(String[] args) {
try {
// Creating a server socket on port 12345
ServerSocket serverSocket = new ServerSocket(12345);

System.out.println("Server has started. Waiting for a client...");

// Accepting client connection


Socket clientSocket = serverSocket.accept();

System.out.println("Client connected.");

// Getting input and output streams for communication


BufferedReader inputFromClient = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
PrintWriter outputToClient = new
PrintWriter(clientSocket.getOutputStream(), true);

// Reading data from the client


String receivedMessage = inputFromClient.readLine();
System.out.println("Received from client: " + receivedMessage);

// Sending a response back to the client

Page 18 of 27
outputToClient.println("Message received by the server.");

// Closing streams and sockets


inputFromClient.close();
outputToClient.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT:

Page 19 of 27
Date of Exp.: Exp No.: 9

AIM: Write a program to retrieve hostname--using methods in Inetaddress class

INPUT:
package fileprograms;
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Program12Server {


public static void main(String[] args) {
try {
// Creating a server socket on port 12345
ServerSocket serverSocket = new ServerSocket(12345);

System.out.println("Server has started. Waiting for a client...");

// Accepting client connection


Socket clientSocket = serverSocket.accept();

System.out.println("Client connected.");

// Getting output stream to send data to the client


PrintWriter outputToClient = new
PrintWriter(clientSocket.getOutputStream(), true);

// Getting current date and time


Date currentDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
String formattedDate = dateFormat.format(currentDate);

// Sending date and time to the client


outputToClient.println("Server's date and time: " + formattedDate);

// Closing streams and sockets


outputToClient.close();
clientSocket.close();

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.*;

public class Program12Client {


public static void main(String[] args) {
try {
// Creating a socket to connect to the server at localhost and port 12345
Socket socket = new Socket("localhost", 12345);

// Getting input stream to receive data from the server


BufferedReader inputFromServer = new BufferedReader(new
InputStreamReader(socket.getInputStream()));

// Receiving date and time from the server


String receivedDateTime = inputFromServer.readLine();
System.out.println("Received from server: " + receivedDateTime);

// Closing stream and socket


inputFromServer.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

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.*;

public class LoginServlet extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");

// Implementing database connection and validation logic here


// Checking if the username and password are valid

if (username.equals("Tom") && password.equals("jerry")) {


// Validation successful, redirect to a success page
response.sendRedirect("success.jsp");
} else {
Page 23 of 27
// Validation failed, redirect back to the login page with an error message
response.sendRedirect("login.jsp?error=invalid");
}
}
}
Success.jsp
package Raminder;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginServlet extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");

// Implementing database connection and validation logic here


// Checking if the username and password are valid

if (username.equals("Tom") && password.equals("jerry")) {


// Validating successful, redirect to a success page
response.sendRedirect("success.jsp");
} else {
// Validation failed, redirect back to the login page with an error message
response.sendRedirect("login.jsp?error=invalid");
}
}
}

Page 24 of 27
OUTPUT:

Page 25 of 27
Date of Exp.: Exp No.: 12

AIM: Write a program to create a login page using Java Beans.

INPUT:
LoginBean.java
public class LoginBean {
private String username;
private String password;

public LoginBean() {
}

// Getters and setters for username and password


public String getUsername() {
return username;
}

public void setUsername(String username) {


this.username = username;
}

public String getPassword() {


return password;
}

public void setPassword(String password) {


this.password = password;
}
}
LoginServlet.jsp
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginServlet extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// Creating a LoginBean object

Page 26 of 27
LoginBean loginBean = new LoginBean();

// Setting values from the form to the LoginBean


loginBean.setUsername(request.getParameter("username"));
loginBean.setPassword(request.getParameter("password"));

// 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

You might also like