Professional Documents
Culture Documents
SEMISTER 5
COMPUTER SCIENCE & ENGINEERING DEPARTMENT
LABORATORY MANUAL
1
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
CERTIFICATE
2
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
INDEX
3
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
EXPERIMENT-1
a basic Java program that demonstrates how to use JDBC (Java Database Connectivity) to
retrieve data from a database and perform DML (Data Manipulation Language) operations
like inserting, updating, and deleting data. Please note that you'll need to have a database set
up and the appropriate JDBC driver for your database type.
In this example, I'll demonstrate using MySQL as the database. Make sure to replace the
placeholders (<placeholders>) with your actual database connection details.
import java.sql.*;
public class JDBCDemo {
// JDBC database URL, username, and password
static final String DB_URL = "jdbc:mysql://localhost:3306/your_database";
static final String USER = "your_username";
static final String PASS = "your_password";
4
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
5
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
EXPERIMENT-2
a. Eclipse:
a. Eclipse:
When you first launch Eclipse, you'll be asked to select a workspace directory. This is where
your projects will be stored.
You can customize the IDE's appearance, plugins, and settings through the Preferences menu.
b. IntelliJ IDEA:
On launching IntelliJ IDEA, you'll need to set up your preferences and keymap.
6
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
Create or import a project and define its settings like SDK (JDK), dependencies, and version
control.
Project Setup:
Create a new project or import an existing one:
In Eclipse, use the File > New > Java Project option.
In IntelliJ IDEA, select File > New > Project and choose a Java project template.
b. Importing an Existing Project:
In Eclipse, use the File > Import option and select General > Existing Projects into
Workspace.
In IntelliJ IDEA, select File > New > Project from Existing Sources and navigate to your
project's root directory.
Adding External Libraries:
Enterprise applications often rely on external libraries and frameworks. You can add these
dependencies to your project:
In Eclipse, right-click your project, choose Build Path > Configure Build Path, and add
external JARs or libraries.
In IntelliJ IDEA, open your project's settings (File > Project Structure), navigate to Libraries,
and add the necessary JARs or dependencies.
Version Control Integration:
It's essential to use version control systems like Git to manage your codebase:
Both Eclipse and IntelliJ IDEA have built-in Git integration. You can clone repositories,
commit changes, and manage branches directly from the IDE.
Coding and Development:
With your IDE and project set up, you can start coding enterprise applications. Leverage the
IDE's features for code completion, debugging, testing, and more.
Eclipse and IntelliJ IDEA provide buttons to build and run your code. You can also configure
run configurations to specify how your application should run.
Deployment and Testing:
Depending on your enterprise application, you might need to deploy it to a server or test
various deployment scenarios.
7
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
8
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
EXPERIMENT-3
AIM : Building a web application with java EE create a registration form and showing data
In another servlet
Building a web application using Java EE involves creating servlets, JSP (JavaServer Pages),
and using the features of the Java EE platform. In this example, we'll create a simple
registration form using servlets and then display the registered data in another servlet.
Create a new dynamic web project in your IDE (Eclipse, IntelliJ, etc.).
Make sure you have the Java EE libraries (servlet-api.jar) in your project's classpath.
2. Create Registration Form (index.jsp):
Create a JSP file named index.jsp in the WebContent directory. This will be the registration
form.
<!DOCTYPE html>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<h2>Registration Form</h2>
<form action="RegisterServlet" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
<input type="submit" value="Register">
</form>
</body>
</html>
3. Create Registration Servlet (RegisterServlet.java):
Create a servlet named RegisterServlet that handles the registration process. It will save the
registered data in a list (for simplicity).
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
9
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
import javax.servlet.http.HttpServletResponse;
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private List<User> userList = new ArrayList<>();
response.sendRedirect("index.jsp");
}
}
4. Create User Class (User.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;
10
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
@WebServlet("/DisplayServlet")
public class DisplayServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
out.println("<html><body>");
out.println("<h2>Registered Users</h2>");
for (User user : RegisterServlet.userList) {
out.println("Name: " + user.getName() + "<br>");
out.println("Email: " + user.getEmail() + "<br><br>");
}
out.println("</body></html>");
}
}
6. Create Web Deployment Descriptor (web.xml):
In the WEB-INF directory, create a web.xml file to map servlets to their URLs.
<web-app>
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>RegisterServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>DisplayServlet</servlet-name>
<servlet-class>DisplayServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/RegisterServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DisplayServlet</servlet-name>
<url-pattern>/DisplayServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
7. Run the Application:
11
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
12
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
EXPERIMENT-4
Design patterns are reusable solutions to common problems that occur in software design and
development. They provide a structured way to solve recurring design problems while
promoting flexibility, maintainability, and scalability. One of the widely used design patterns
is the Factory Design Pattern, which falls under the creational category and is used for object
creation.
The Factory Design Pattern is used to create objects without specifying the exact class of
object that will be created. It defines an interface (or an abstract class) for creating objects,
and concrete implementations of this interface are responsible for instantiating specific types
of objects.
In this example, we'll create a simple implementation of the Factory Design Pattern to create
different shapes.
13
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
}
Create Shape Factory:
Keep in mind that this is a simple example, and in real-world scenarios, factories might
involve more complex logic and considerations, such as managing object lifecycle,
dependencies, and configurations.
Design patterns like the Factory Design Pattern are powerful tools to enhance code
modularity, reusability, and maintainability by providing standardized solutions to common
design challenges.
14
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
EXPERIMENT-5
AIM : Unit testing in Java Enterprise Applications
Writing unit tests for java classes and components
Design patterns are reusable solutions to common problems that occur in software design and
development. They provide a structured way to solve recurring design problems while
promoting flexibility, maintainability, and scalability. One of the widely used design patterns
is the Factory Design Pattern, which falls under the creational category and is used for object
creation.
The Factory Design Pattern is used to create objects without specifying the exact class of
object that will be created. It defines an interface (or an abstract class) for creating objects,
and concrete implementations of this interface are responsible for instantiating specific types
of objects.
In this example, we'll create a simple implementation of the Factory Design Pattern to create
different shapes.
15
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
}
}
Create Shape Factory:
Keep in mind that this is a simple example, and in real-world scenarios, factories might
involve more complex logic and considerations, such as managing object lifecycle,
dependencies, and configurations.
Design patterns like the Factory Design Pattern are powerful tools to enhance code
modularity, reusability, and maintainability by providing standardized solutions to common
design challenges.
16
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
User
Unit testing in Java Enterprise Applications
Writing unit tests for java classes and components
Unit testing is an essential practice in Java Enterprise Applications to ensure the correctness
and reliability of individual components, classes, and methods. Writing unit tests helps catch
bugs early, facilitates code maintenance, and supports refactoring. In this example, we'll
cover writing unit tests using the JUnit framework, which is widely used for Java unit testing.
First, make sure you have JUnit added to your project's dependencies. You can typically add
JUnit via Maven, Gradle, or download the JAR files manually.
Suppose you have a simple class representing a basic mathematical operation. We'll write unit
tests for this class.
MathOperation.java:
@Before
public void setUp() {
mathOperation = new MathOperation();
}
17
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
@Test
public void testAdd() {
int result = mathOperation.add(3, 4);
assertEquals(7, result);
}
@Test
public void testSubtract() {
int result = mathOperation.subtract(7, 3);
assertEquals(4, result);
}
}
In this example, we're using JUnit to write test methods. Let's break down the key
components:
@Before: This annotation indicates that the annotated method should be run before each test
method. It's used to set up any necessary resources before testing.
@Test: This annotation indicates that the annotated method is a test method. JUnit will
execute methods with this annotation to perform tests.
assertEquals: This is an assertion method provided by JUnit. It checks whether the actual
value matches the expected value. If not, the test will fail.
3. Running Unit Tests:
After writing your unit tests, you can run them using a build tool (like Maven or Gradle) or
directly from your IDE.
JUnit allows you to group related tests into test suites or categories. This is helpful for
organizing and running specific sets of tests.
In enterprise applications, components often have dependencies. When writing unit tests, you
might use mocking frameworks like Mockito to simulate behavior of dependent objects.
Dependency injection is also a common technique to inject dependencies into your classes
during testing.
6. Continuous Integration:
For robust testing in enterprise applications, consider integrating your unit tests into a
continuous integration (CI) process. This ensures that tests are automatically executed
whenever code changes are made.
18
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375
In Java Enterprise Applications, you'll have more complex components like EJBs, servlets,
and database interactions. Writing unit tests for these components might involve using
frameworks that provide mocks and stubs for simulating complex interactions.
Remember that unit testing is just one layer of testing. In enterprise applications, integration
testing, functional testing, and performance testing are also crucial to ensure the overall
quality and reliability of your software.
19
ERP NO: 210303125045