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 (index.html):
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 (SimpleServlet.java):
Java code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpleServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String name = request.getParameter("name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Hello, " + name + "!</h1>");
2. Implementing GET and POST Handling
o Modify SimpleServlet.java 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 (DataApplet.java):
Java code:
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class DataApplet extends Applet {
public void paint(Graphics g) {
try {
URL servletUrl = new URL(getCodeBase(), "DataServlet");
URLConnection connection = servletUrl.openConnection();
connection.setDoOutput(true);
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.println("data=SampleData");
out.close();
BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String response = in.readLine();
g.drawString("Response: " + response, 20, 20);
in.close();
} catch (Exception e) {
g.drawString("Error: " + e.getMessage(), 20, 20);
o Servlet to Process Applet Data (DataServlet.java):
Java code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DataServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String data = request.getParameter("data");
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println("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 (input.jsp):
Jsp code:
<form action="display.jsp" method="post">
<input type="text" name="username">
<input type="submit" value="Submit">
</form>
o Display JSP Page (display.jsp):
Jsp code:
<% String username = request.getParameter("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 (UserBean.java):
Java code:
public class UserBean {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
o JSP Page Utilizing JavaBean (user.jsp):
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 (HelloBean.java):
Java code:
import javax.ejb.Stateless;
@Stateless
public class HelloBean implements HelloBeanRemote {
public String sayHello(String name) {
return "Hello, " + name;
o Remote Interface (HelloBeanRemote.java):
Java code:
import javax.ejb.Remote;
@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 (JDBCExample.java):
Java code:
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) {
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase",
"user", "password");
Statement stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO users (name) VALUES ('John Doe')");
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println(rs.getString("name"));
conn.close();
} catch (Exception e) {
e.printStackTrace();
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 (EmailSender.java):
Java code:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class EmailSender {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.example.com");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "password");
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to-
[email protected]"));
message.setSubject("Test Email");
message.setText("This is a test email.");
Transport.send(message);
System.out.println("Email Sent!");
} catch (MessagingException e) {
e.printStackTrace();
}
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.