Web Enabled Java Programming Lab
Manual
Subject Name: Web Enabled Java Programming Lab
Subject Code : MCAN-E394B
Branch : MCA
Year : Year- 2nd / Semester-3rd
Techno International New Town
Block - DG 1/1, Action Area 1
Newtown, Rajarhat, Kolkata - 700156
Course Code: MCAN-E394B
Course Title: Web Enabled JAVA Programming LAB
Contact Hours / Week: 4
Total Contact Hours: 40
Credit: 2
Course Outcome:
Upon successful completion of this course, students will be able to:
• Create dynamic websites and web-based applications.
Lab Structure and Exercises
Unit 1: HTML to Servlet Applications
• Objective: Understand the basics of Servlet technology and how to handle HTTP requests
and responses.
• Lab Exercises:
1. Set up a simple HTML form for user input and create a corresponding servlet to
process the form data.
2. Implement a basic GET and POST request handling in a servlet.
3. Create a servlet-based application to display dynamic content using data from user
input.
Lab Solutions:
HTML to Servlet Applications
1. Creating a Simple HTML Form with Servlet Processing
o HTML Form ([Link]):
Html code:
<form action="SimpleServlet" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<input type="submit" value="Submit">
</form>
o Servlet ([Link]):
Java code:
import [Link].*;
import [Link].*;
import [Link].*;
public class SimpleServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String name = [Link]("name");
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h1>Hello, " + name + "!</h1>");
2. Implementing GET and POST Handling
o Modify [Link] to include both doGet and doPost methods, each handling
different inputs.
Unit 2: Applet to Servlet Communication
• Objective: Learn how to establish communication between applets and servlets.
• Lab Exercises:
1. Design an applet that collects user data and sends it to a servlet.
2. Implement communication from the servlet to the applet, such as sending data back
to the applet for display.
3. Develop a simple applet-servlet interaction that involves basic CRUD operations.
Lab Solutions:
Applet to Servlet Communication
1. Designing an Applet Sending Data to Servlet
o Applet Code ([Link]):
Java code:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class DataApplet extends Applet {
public void paint(Graphics g) {
try {
URL servletUrl = new URL(getCodeBase(), "DataServlet");
URLConnection connection = [Link]();
[Link](true);
PrintWriter out = new PrintWriter([Link]());
[Link]("data=SampleData");
[Link]();
BufferedReader in = new BufferedReader(new
InputStreamReader([Link]()));
String response = [Link]();
[Link]("Response: " + response, 20, 20);
[Link]();
} catch (Exception e) {
[Link]("Error: " + [Link](), 20, 20);
o Servlet to Process Applet Data ([Link]):
Java code:
import [Link].*;
import [Link].*;
import [Link].*;
public class DataServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String data = [Link]("data");
[Link]("text/plain");
PrintWriter out = [Link]();
[Link]("Received: " + data);
}
}
Unit 3: Designing Online Applications with JSP
• Objective: Use JSP to create dynamic web applications.
• Lab Exercises:
1. Set up a JSP page that dynamically displays content based on user input.
2. Design a simple online application (e.g., an online calculator or a registration form)
using JSP.
3. Explore basic JSP directives, actions, and scripting elements.
Lab Solutions:
Designing Online Applications with JSP
1. Simple JSP Page Displaying User Input
o JSP Page ([Link]):
Jsp code:
<form action="[Link]" method="post">
<input type="text" name="username">
<input type="submit" value="Submit">
</form>
o Display JSP Page ([Link]):
Jsp code:
<% String username = [Link]("username"); %>
<h1>Welcome, <%= username %>!</h1>
Unit 4: Creating JSP Programs using JavaBeans
• Objective: Learn how to use JavaBeans in JSP for modular programming.
• Lab Exercises:
1. Create a JavaBean to store user information and use it within a JSP page.
2. Develop a JSP application that utilizes JavaBeans to separate business logic from
presentation.
3. Implement an e-commerce cart system using JavaBeans and JSP for managing items
and totals.
Lab Solutions:
Creating JSP Programs using JavaBeans
1. Using JavaBeans in JSP
o JavaBean Class ([Link]):
Java code:
public class UserBean {
private String name;
public String getName() { return name; }
public void setName(String name) { [Link] = name; }
o JSP Page Utilizing JavaBean ([Link]):
Jsp code:
<jsp:useBean id="user" class="UserBean" scope="session" />
<jsp:setProperty name="user" property="name" value="Alice" />
<h2>User Name: <jsp:getProperty name="user" property="name" /></h2>
Unit 5: Working with Enterprise JavaBeans (EJB)
• Objective: Understand the basics of EJBs and their usage in enterprise-level applications.
• Lab Exercises:
1. Set up a simple EJB and deploy it on an application server.
2. Implement session beans (stateless and stateful) and use them in a sample
application.
3. Create a basic online banking application to manage user accounts with EJB.
Lab Solutions:
Working with Enterprise JavaBeans (EJB)
1. Setting Up a Simple EJB (Example for Stateless Session Bean)
o Stateless Session Bean ([Link]):
Java code:
import [Link];
@Stateless
public class HelloBean implements HelloBeanRemote {
public String sayHello(String name) {
return "Hello, " + name;
o Remote Interface ([Link]):
Java code:
import [Link];
@Remote
public interface HelloBeanRemote {
String sayHello(String name);
Unit 6: Performing Java Database Connectivity (JDBC)
• Objective: Gain skills in database connectivity using JDBC.
• Lab Exercises:
1. Connect to a database and perform basic operations (INSERT, UPDATE, DELETE)
using JDBC.
2. Create a JDBC-based application to display data from a database in a web
application.
3. Develop an application to manage employee records with JDBC for data storage and
retrieval.
Lab Solutions:
Performing Java Database Connectivity (JDBC)
1. Basic JDBC Operations
o Database Connection Example ([Link]):
Java code:
import [Link].*;
public class JDBCExample {
public static void main(String[] args) {
try {
Connection conn = [Link]("jdbc:mysql://localhost:3306/mydatabase",
"user", "password");
Statement stmt = [Link]();
[Link]("INSERT INTO users (name) VALUES ('John Doe')");
ResultSet rs = [Link]("SELECT * FROM users");
while ([Link]()) {
[Link]([Link]("name"));
[Link]();
} catch (Exception e) {
[Link]();
Unit 7: Creating and Sending Email with Java
• Objective: Learn how to send emails using JavaMail API.
• Lab Exercises:
1. Set up a JavaMail environment and send a basic email.
2. Create an email-sending web application that takes user input (email, subject, body)
and sends emails.
3. Implement email functionality in an existing application, such as sending a
registration confirmation.
Lab Solutions:
Creating and Sending Email with Java
1. Sending Email Using JavaMail API
o JavaMail Example ([Link]):
Java code:
import [Link].*;
import [Link].*;
import [Link];
public class EmailSender {
public static void main(String[] args) {
Properties props = new Properties();
[Link]("[Link]", "[Link]");
[Link]("[Link]", "true");
Session session = [Link](props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your-email@[Link]", "password");
});
try {
Message message = new MimeMessage(session);
[Link](new InternetAddress("from-email@[Link]"));
[Link]([Link], [Link]("to-
email@[Link]"));
[Link]("Test Email");
[Link]("This is a test email.");
[Link](message);
[Link]("Email Sent!");
} catch (MessagingException e) {
[Link]();
}
Unit 8: Building Web Applications
• Objective: Integrate previous concepts into a cohesive web application.
• Lab Exercises:
1. Develop a web application integrating HTML, JSP, JavaBeans, EJB, and JDBC (e.g., an
e-commerce site, job portal, or blog platform).
2. Implement security measures like authentication and authorization for the
application.
3. Final project: Create a full-featured web application using concepts learned
throughout the course.
Lab Solutions:
Building Web Applications
1. Full Web Application Example
o Combine HTML forms, JSP, Servlets, JavaBeans, and JDBC into a single project, such
as an E-commerce application.
o Use a structured setup to include:
▪ HTML Forms for user input (e.g., product search).
▪ JSP for dynamic page rendering.
▪ Servlets to handle HTTP requests.
▪ JavaBeans to manage application data.
▪ JDBC to connect and perform database operations.
o Incorporate Authentication using sessions and simple access control for restricted
pages.