Unit 4: Applets, AWT, servlets and JDBC
Java Technologies notes · PTU syllabus (PGCA1939)
On this page
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
- 1Load and instantiate
- 2init()
Called once
- 3service()
Handles each request: doGet, doPost
- 4destroy()
Called before removal
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.
- 1. init(): Called once to initialise
- 2. start(): Called after init and whenever the page is revisited
- 3. paint(Graphics g): Draws output; called on repaint
- 4. stop(): Called when the page is left
- 5. destroy(): Called once before the applet is removed
Entry point
init(), no main()
main()
Runs in
Browser or appletviewer
JVM directly
Security
Sandboxed — no local file access
Full access
Status
Obsolete
Standard
Topic 2
Event handling
- 1Source
Component generates an event — button click
- 2Event object
ActionEvent carries details
- 3Registration
Listener registered with addActionListener
- 4Listener
Receives the event and runs the handler method
- 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
Topic 3
AWT window fundamentals and frames
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.
Topic 4
Servlets and their life cycle
- 1Loaded and instantiated by the container (Tomcat)
- 2init()
Called once
- 3service()
Called for each request → doGet() or doPost()
- 4destroy()
Called once before unloading
- javax.servlet (jakarta.servlet)
- Servlet, ServletRequest, ServletResponse, ServletConfig
- HttpServlet
- Base class with doGet, doPost
- HttpServletRequest
- getParameter, getSession, getCookies
- HttpServletResponse
- setContentType, getWriter, addCookie, sendRedirect
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).
Topic 6
Cookies and session tracking
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.
Topic 7
JDBC database connectivity
- 1
Load driver (automatic in JDBC 4)
- 2
Open Connection with DriverManager.getConnection(url, user, pwd)
- 3
Create PreparedStatement
- 4
Execute query or update
- 5
Process ResultSet
- 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
- Q1.List the applet life-cycle methods.
- Q2.What is the event delegation model?
- Q3.List the servlet life-cycle methods.
- Q4.Distinguish GET and POST.
- Q5.Distinguish cookies and sessions.
- Q6.Why use PreparedStatement?
Long-answer questions
- Q1.Explain the applet life cycle and event handling.
- Q2.Explain the servlet life cycle with an example.
- Q3.Explain session tracking in servlets.
- 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.
