Unit 4 of 4 · M.Sc IT Sem 4

Unit 4: Applets, AWT, servlets and JDBC

Java Technologies notes · PTU syllabus (PGCA1939)

3 min read7 topics10 exam questions
On this page
  1. Unit summary
  2. The Applet class and life cycle
  3. Event handling
  4. AWT window fundamentals and frames
  5. Servlets and their life cycle
  6. Reading parameters and handling HTTP requests
  7. Cookies and session tracking
  8. JDBC database connectivity
  9. Key terms
  10. Quick revision
  11. Important questions

Unit summary

Java supports graphical, web and database programming. This unit covers applets and their life cycle, graphics, event handling and AWT, servlets — life cycle, API, parameters, HTTP requests, cookies and sessions — and JDBC connectivity.

After this unit you can

  • Explain applets, AWT and event handling
  • Explain the servlet life cycle and API
  • Handle parameters, cookies and sessions in servlets
  • Connect to a database with JDBC

PTU syllabus topics

  • Applet class and lifecycle
  • graphics in applets
  • event handling
  • AWT window fundamentals
  • frame windows
  • graphics/color/fonts
  • servlet lifecycle and API
  • reading servlet parameters
  • handling HTTP requests/responses
  • cookies and session tracking
  • JDBC database connectivity
  • SQL queries
  • retrieving and updating data
ProcessServlet life cycle
  1. 1Load and instantiate
  2. 2init()

    Called once

  3. 3service()

    Handles each request: doGet, doPost

  4. 4destroy()

    Called before removal

1

Topic 1

The Applet class and life cycle

  • Applet: a small Java program embedded in a web page and run by a browser plug-in or appletviewer. Applets are now obsolete — browsers removed plug-in support, appletviewer was dropped in JDK 11 and the Applet API is deprecated — but the concepts carry over to GUI programming and remain in the syllabus.
CycleApplet life cycle
Applet life cycle
1init()
2start()
3paint(Graphics g)
4stop()
5destroy()
  1. 1. init(): Called once to initialise
  2. 2. start(): Called after init and whenever the page is revisited
  3. 3. paint(Graphics g): Draws output; called on repaint
  4. 4. stop(): Called when the page is left
  5. 5. destroy(): Called once before the applet is removed
ComparisonApplet and application
Applet
Application

Entry point

init(), no main()

main()

Runs in

Browser or appletviewer

JVM directly

Security

Sandboxed — no local file access

Full access

Status

Obsolete

Standard

2

Topic 2

Event handling

ProcessEvent delegation model
  1. 1Source

    Component generates an event — button click

  2. 2Event object

    ActionEvent carries details

  3. 3Registration

    Listener registered with addActionListener

  4. 4Listener

    Receives the event and runs the handler method

Key termsListeners and methods
ActionListener
actionPerformed(ActionEvent) — buttons, menu items, Enter in a text field
MouseListener
mouseClicked, mousePressed, mouseReleased, mouseEntered, mouseExited
KeyListener
keyPressed, keyReleased, keyTyped
WindowListener
windowClosing, windowOpened, windowClosed, windowActivated and others
Adapter classes
MouseAdapter, WindowAdapter — override only needed methods
3

Topic 3

AWT window fundamentals and frames

ClassificationAWT hierarchy
Component
  • Container

    Holds other components

  • Window and Frame

    Top-level window with title bar

  • Dialog

    Pop-up window

  • Panel

    Container without a title bar; groups components

  • Basic components

    Button, Label, TextField, TextArea, Checkbox, Choice, List, Scrollbar

  • AWT (Abstract Window Toolkit) uses native, heavyweight components; Swing (JFrame, JButton) adds lightweight, richer components and is preferred for new desktop code.
4

Topic 4

Servlets and their life cycle

ProcessServlet life cycle
  1. 1Loaded and instantiated by the container (Tomcat)
  2. 2init()

    Called once

  3. 3service()

    Called for each request → doGet() or doPost()

  4. 4destroy()

    Called once before unloading

Key termsServlet API
javax.servlet (jakarta.servlet)
Servlet, ServletRequest, ServletResponse, ServletConfig
HttpServlet
Base class with doGet, doPost
HttpServletRequest
getParameter, getSession, getCookies
HttpServletResponse
setContentType, getWriter, addCookie, sendRedirect
5

Topic 5

Reading parameters and handling HTTP requests

java@WebServlet("/hello")
public class Hello extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
        String name = req.getParameter("name");
        res.setContentType("text/html");
        res.getWriter().println("<h2>Welcome, " + name + "</h2>");
    }
}
  • GET sends data in the URL (bookmarkable, limited); POST sends it in the body (forms, larger data).
6

Topic 6

Cookies and session tracking

ComparisonSession tracking
Cookies
HttpSession

Stored

In the browser

On the server (ID in a cookie)

Code

res.addCookie(new Cookie("user", "asha"))

req.getSession().setAttribute("cart", cart)

Use

Remembering preferences

Logins, shopping carts

  • Other techniques: URL rewriting and hidden form fields.
7

Topic 7

JDBC database connectivity

ProcessJDBC steps
  1. 1

    Load driver (automatic in JDBC 4)

  2. 2

    Open Connection with DriverManager.getConnection(url, user, pwd)

  3. 3

    Create PreparedStatement

  4. 4

    Execute query or update

  5. 5

    Process ResultSet

  6. 6

    Close resources (try-with-resources)

javaString url = "jdbc:mysql://localhost:3306/college";
try (Connection con = DriverManager.getConnection(url, "root", "pwd");
     PreparedStatement ps = con.prepareStatement("SELECT name FROM student WHERE marks > ?")) {
    ps.setInt(1, 60);
    ResultSet rs = ps.executeQuery();
    while (rs.next()) System.out.println(rs.getString("name"));
    PreparedStatement up = con.prepareStatement("UPDATE student SET marks = marks + 5 WHERE id = ?");
    up.setInt(1, 7); up.executeUpdate();
}
  • PreparedStatement prevents SQL injection and is precompiled.

Key terms

Applet
Small Java program run in a browser (now obsolete)
Servlet
Server-side Java class handling HTTP requests
HttpSession
Server-side store for a user's session
JDBC
Java Database Connectivity API
PreparedStatement
Precompiled parameterised SQL statement

Quick revision

  • Applet life cycle; AWT; event delegation.
  • Servlet life cycle; API; GET vs POST; parameters.
  • Cookies vs sessions.
  • JDBC steps; PreparedStatement; ResultSet.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.List the applet life-cycle methods.
  2. Q2.What is the event delegation model?
  3. Q3.List the servlet life-cycle methods.
  4. Q4.Distinguish GET and POST.
  5. Q5.Distinguish cookies and sessions.
  6. Q6.Why use PreparedStatement?

Long-answer questions

  1. Q1.Explain the applet life cycle and event handling.
  2. Q2.Explain the servlet life cycle with an example.
  3. Q3.Explain session tracking in servlets.
  4. Q4.Explain JDBC with a program.

Stuck on this unit?

Message SBS on WhatsApp for help with Java Technologies, or to ask about studying M.Sc IT at Synetic.

WhatsApp us