Java Web Development

Section 8: Java Web Development

Lesson 1: Introduction to Servlets and JSP

1.1 Basics of Web Applications in Java

Web applications in Java are built using Servlets and JSP (JavaServer Pages). Servlets handle server-side logic, while JSP allows for the dynamic generation of web content.

1.2 Setting Up a Simple Servlet and JSP Project

Setting up a Servlet and JSP project involves configuring a web application in a servlet container like Apache Tomcat.

Example (Simple Servlet):

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.IOException;

import java.io.PrintWriter;


public class MyServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        // Set content type

        response.setContentType("text/html");


        // Get PrintWriter object

        PrintWriter out = response.getWriter();


        // Write HTML content

        out.println("<html><body>");

        out.println("<h2>Hello, Servlet!</h2>");

        out.println("</body></html>");


        // Close PrintWriter

        out.close();

    }

}

Example (Simple JSP):


jsp

Copy code

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>My JSP Page</title>

</head>

<body>

    <h2>Hello, JSP!</h2>

</body>

</html>

1.3 Spring Framework Overview

The Spring framework is a comprehensive framework for Java enterprise applications. It provides features like dependency injection, aspect-oriented programming, and a wide range of modules for various functionalities.


Lesson 2: Dependency Injection and Spring Beans

2.1 Introduction to the Spring Framework

Spring simplifies the development of Java applications by promoting a modular and loosely coupled architecture.

Example (Dependency Injection):

public class Car {

    private Engine engine;


    // Constructor-based dependency injection

    public Car(Engine engine) {

        this.engine = engine;

    }


    public void start() {

        engine.start();

        System.out.println("Car started");

    }

}


public interface Engine {

    void start();

}


public class GasolineEngine implements Engine {

    public void start() {

        System.out.println("Gasoline engine started");

    }

}


public class ElectricEngine implements Engine {

    public void start() {

        System.out.println("Electric engine started");

    }

}

Example (Spring Bean Configuration):


xml code

<!-- Spring bean configuration -->

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans.xsd">


    <!-- Defining a bean of type GasolineEngine -->

    <bean id="gasolineEngine" class="com.example.GasolineEngine" />


    <!-- Defining a bean of type ElectricEngine -->

    <bean id="electricEngine" class="com.example.ElectricEngine" />


    <!-- Defining a bean of type Car with constructor injection -->

    <bean id="car" class="com.example.Car">

        <constructor-arg ref="gasolineEngine" />

    </bean>

</beans>

Understanding Servlets, JSP, and the Spring framework is crucial for developing robust and scalable Java web applications. Practice building and configuring simple Servlets, JSP pages, and Spring beans to solidify your understanding of Java web development.