Java Topics Not Covered in Edureka Video (Based on NPTEL PDF + Detailed Additions)
---
1. Java Applets
Definition:
Applets are small Java programs embedded in web pages, running inside a Java-enabled browser. They are
used for interactive tasks like games or visualizations.
Analogy:
Think of an applet like a mini-game embedded in a webpage, like a YouTube video but coded in Java.
Lifecycle:
- init() - Initialization phase.
- start() - Starts or resumes execution.
- paint(Graphics g) - Draws graphics.
- stop() - Stops execution temporarily.
- destroy() - Cleans resources.
Diagram:
Browser -> init() -> start() -> paint() -> stop() or destroy()
Example Code:
import java.applet.Applet;
import java.awt.Graphics;
public class HelloApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Welcome to Applet", 20, 20);
HTML to Embed Applet:
<applet code="HelloApplet.class" width="300" height="200"></applet>
---
2. Abstract Window Toolkit (AWT)
Definition:
AWT is Java's original GUI toolkit, providing components like windows, buttons, text fields, etc.
Analogy:
AWT is like using physical Lego blocks to build desktop interfaces.
AWT Flow:
User clicks -> Event generated -> Listener receives -> Event handler executes
Main Components:
- Frame
- Label
- TextField
- Button
- Checkbox
Event Handling:
- ActionListener, WindowListener interfaces
Example:
import java.awt.*;
import java.awt.event.*;
public class AWTExample {
public static void main(String[] args) {
Frame f = new Frame("AWT Example");
Label l = new Label("Enter Name:");
TextField tf = new TextField();
Button b = new Button("Submit");
l.setBounds(50, 50, 100, 20);
tf.setBounds(50, 80, 100, 20);
b.setBounds(50, 110, 50, 30);
f.add(l); f.add(tf); f.add(b);
f.setSize(300, 300);
f.setLayout(null);
f.setVisible(true);
---
3. Java Servlets
Definition:
Servlets are server-side Java programs to handle client requests and generate dynamic web content.
Analogy:
A Servlet is like a restaurant waiter who takes your order (request), brings it to kitchen (server), and delivers
food (response).
Servlet Lifecycle:
- init() -> Called once to initialize.
- service() -> Called to handle requests.
- destroy() -> Cleanup resources.
Servlet Working Diagram:
Client Request -> Web Server -> Servlet Container -> Servlet (doGet()/doPost()) -> Response
Example Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<h1>Hello from Servlet</h1>");
---
4. Java Networking
Definition:
Networking in Java allows communication between machines via TCP/IP protocols.
Analogy:
Imagine two people talking over a phone call - sending and receiving information.
Networking Classes:
- Socket (Client)
- ServerSocket (Server)
- DataInputStream and DataOutputStream for message transmission
Network Communication Diagram:
Client -> Socket Connect -> ServerSocket Accept -> Data Read/Write
Server Example:
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(5000);
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
String message = dis.readUTF();
System.out.println("Client says: " + message);
ss.close();
Client Example:
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost", 5000);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();