Java AWT, JDBC, Servlet Essentials: Controls, Layouts, and More

AWT Controls: Label, Button, TextField

AWT (Abstract Window Toolkit) is Java’s GUI toolkit used to create graphical user interfaces. It provides basic UI components like buttons, labels, text fields, etc.

1. Label:

  • A Label is a non-editable text element used to display a single line of read-only text.
  • It does not accept user input.
  • Mainly used to identify other GUI components.

2. Button:

  • A Button is a clickable component used to perform an action.
  • It generates an ActionEvent when clicked.
  • Useful in performing operations when the user interacts with the GUI.

3. TextField:

  • A TextField is an input component that allows the user to enter a single line of text.
  • Commonly used in forms for taking input like name, email, etc.

Layout Managers: FlowLayout, BorderLayout, GridLayout

Layout Managers are responsible for arranging GUI components inside containers.

1. FlowLayout:

  • Default layout for Applet and Panel.
  • Arranges components in a row, left to right.
  • When space runs out, it moves components to the next line.

2. BorderLayout:

  • Divides the container into five regions:
    • North, South, East, West, Center
  • Only one component can be placed in each region.

3. GridLayout:

  • Arranges components in rows and columns.
  • All cells are of equal size.
  • Best used when a uniform layout is required.

Applet Basics: init(), paint()

An applet is a Java program that runs in a web browser or applet viewer.

Applet Life Cycle Methods:

  1. init(): Called once when the applet is first loaded. Used to initialize resources.
  2. start(): Called after init() and every time the applet becomes active.
  3. paint(Graphics g): Called to draw output on the screen.
  4. stop(): Called when the applet is no longer active.
  5. destroy(): Called when the applet is removed from memory.

JDBC Connectivity: Connect Java with MySQL

JDBC (Java Database Connectivity) is an API that allows Java programs to interact with databases.

Steps to connect Java with MySQL:

  1. Import JDBC package
    import java.sql.*;
  2. Load JDBC Driver
    Class.forName("com.mysql.cj.jdbc.Driver");
  3. Establish Connection
    DriverManager.getConnection(URL, USERNAME, PASSWORD);
  4. Create Statement or PreparedStatement
    → To execute SQL queries.
  5. Execute SQL Queries
    → Use executeQuery() for SELECT and executeUpdate() for INSERT, UPDATE, DELETE.
  6. Process Results
    → Use ResultSet to read data from SELECT query.
  7. Close Connection
    → Frees resources and prevents memory leaks.

CRUD Operations in JDBC

CRUD = Create, Read, Update, Delete
These are the four basic operations in a database.

Create (Insert):

  • Adds new data into the database.

Read (Select):

  • Retrieves data from the database.

Update:

  • Modifies existing data.

Delete:

  • Removes data from the database.

In Java, we use PreparedStatement to perform these operations securely and efficiently.


JDBC Code Flow

Code Flow Description:

  1. Java code uses DriverManager to load the driver.
  2. DriverManager establishes a connection with the MySQL database.
  3. SQL queries are executed using Statement/PreparedStatement.
  4. Results are fetched using ResultSet.
  5. Connection is closed after the operations.

Text Diagram:

plaintext
CopyEdit

Java Code → DriverManager → JDBC Driver → MySQL Database → ResultSet

Servlet Lifecycle: init(), service(), destroy()

A Servlet is a Java class that handles client requests and generates a dynamic response.

Servlet Lifecycle Methods:

  1. init():
    • Called only once when the servlet is initialized.
    • Used to allocate resources like database connections.
  2. service():
    • Called for every client request.
    • Handles both doGet() and doPost() methods.
  3. destroy():
    • Called before the servlet is removed from service.
    • Used to release resources.

Handling GET and POST in Servlet

GET and POST are HTTP request methods.

GET:

  • Sends data via URL.
  • Used to retrieve data.
  • Limited amount of data.
  • Not secure.

POST:

  • Sends data via request body.
  • Used to send large or sensitive data.
  • More secure than GET.

Servlet uses doGet() to handle GET requests and doPost() for POST requests.


Form Handling in Servlet

  • A form is an HTML structure to collect user input (like login form).
  • When a user submits a form, the data is sent to the servlet.
  • Servlet uses request.getParameter("field_name") to fetch input values.
  • Then the data is processed and an appropriate response is sent back to the client.

Login and Logout using Servlet

Login Functionality:

  • User enters username and password in an HTML form.
  • Servlet receives the form data and validates it.
  • If correct:
    • A new HTTP session is created.
    • User is redirected to a welcome/dashboard page.
  • If incorrect:
    • Error message is shown.

Logout Functionality:

  • When user clicks logout, the servlet:
    • Invalidates the session using session.invalidate().
    • Redirects user back to login page.

Would you like this in PDF format for easy printing or writing down? I can generate that too.

4o