Java and Web Development
Java is a high-level, object-oriented programming language used for building applications,
including web applications, enterprise solutions, and mobile apps. It is platform-independent,
meaning Java code can run on any device that has a Java Virtual Machine (JVM).
What is Java?
Java is a widely-used programming language known for its "Write Once, Run Anywhere"
capability. It is secure, scalable, and used in enterprise applications, web development, and
mobile apps.
Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
J2EE (Java 2 Enterprise Edition)
J2EE is a platform for developing large-scale, distributed, and multi-tier applications. It includes
APIs like Servlets, JSP, EJB, and JDBC for enterprise application development.
Example: A banking application using J2EE for transactions and user management.
JRE, JDK & JVM
JRE (Java Runtime Environment): Provides libraries and JVM to run Java programs.
JDK (Java Development Kit): Includes JRE + development tools like the compiler (javac).
JVM (Java Virtual Machine): Executes Java bytecode, making Java platform-independent.
Example:
JDK is required for development, while JRE is sufficient for running Java applications.
Latest Versions of Java
Java receives regular updates, with new features, performance improvements, and security
enhancements. Some of the latest features include pattern matching, record types, and the
ZGC garbage collector.
Example:
record Person(String name, int age) {} // Introduced in Java 14
Class & Objects
A class is a blueprint for objects, and an object is an instance of a class.
Example:
class Car {
String model;
int year;
}
Car myCar = new Car();
Object-Oriented Programming Concepts
Java follows OOP principles: Encapsulation, Abstraction, Inheritance, and Polymorphism.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
Abstract Class & Interface
Abstract Class: Contains abstract (unimplemented) and concrete (implemented) methods.
Interface: Contains only abstract methods (before Java 8).
Example:
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start() {
System.out.println("Car starts");
}
}
Sample Java Programming
Writing a basic Java program involves defining a class with a main method.
Example:
public class Sample {
public static void main(String[] args) {
System.out.println("This is a sample Java program.");
}
}
Exception Handling: try, catch, finally, throw, throws
Exception handling prevents runtime errors from crashing the program.
Example:
try {
int num = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Execution completed");
}
Data Types & Variables
Java has primitive data types (int, float, boolean) and non-primitive types (String, Arrays).
Example:
int age = 25;
String name = "John";
Statements & Expressions
Statements execute an action, while expressions evaluate a value.
Example:
int sum = 5 + 10; // Expression
System.out.println(sum); // Statement
Operators
Java has arithmetic, relational, logical, and bitwise operators.
Example:
int a = 10, b = 20;
int sum = a + b; // Addition operator
Coding Standards
Writing clean, readable, and maintainable code is essential. Naming conventions, proper
indentation, and documentation improve code quality.
Example:
Class names should be PascalCase: BankAccount
Variable names should be camelCase: userName
if-else, for, while, break, continue
Control flow statements help in decision-making and loops.
Example:
for (int i = 0; i < 5; i++) {
if (i == 3) continue; // Skips iteration when i=3
System.out.println(i);
}
Collection Framework: List, ArrayList, Set
Java collections store multiple elements dynamically.
Example:
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
System.out.println(names);
Threads & Concurrency
Java supports multithreading to execute multiple tasks simultaneously.
Example:
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
}
new MyThread().start();
JUnit (Unit Testing)
JUnit is used to write test cases for Java applications.
Example:
@Test
public void testAddition() {
assertEquals(5, Math.add(2, 3));
}
Advanced Java Programming
Advanced Java includes Servlets, JSP, JDBC, Hibernate, Spring, and REST APIs.
Web Development
What is JSP?
Java Server Pages (JSP) allows embedding Java code within HTML to create dynamic web
pages.
Example:
<%@ page language="java" %>
<html>
<body>
<%= "Hello, JSP!" %>
</body>
</html>
What is Servlet?
Servlets handle HTTP requests and generate responses dynamically on the server-side.
Example:
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws IOException {
response.getWriter().println("Hello, Servlet!");
}
}
doGet & doPost
These methods handle HTTP GET and POST requests.
Example:
protected void doPost(HttpServletRequest request, HttpServletResponse
response) {
String name = request.getParameter("username");
}
HTTP & HTTPS
HTTP (Hypertext Transfer Protocol) is used for data communication.
HTTPS (Secure HTTP) encrypts data for security.
XML, DOM, Cookies, Headers
XML: Stores structured data.
DOM: Represents document structure.
Cookies: Store user preferences.
Example:
Cookie userCookie = new Cookie("username", "John");
response.addCookie(userCookie);
Concepts of APIs, JSON, RestAPIs, GraphQL, Postman,
Authentication, SoapUI
APIs allow communication between software. REST APIs use JSON for data exchange.
Example:
{
"name": "John",
"age": 30
}
Life Cycle of a Servlet
A servlet follows: init() → service() → destroy().
Servlet Interface & Classes
Servlets use HttpServlet , GenericServlet , and interfaces like ServletConfig and
ServletContext .
Session Management & Cookies in Servlet
Manages user sessions using HttpSession , URL rewriting , and hidden fields .
Example:
HttpSession session = request.getSession();
session.setAttribute("user", "John");
Advanced Concepts
Includes filters, interceptors, WebSockets, and microservices.