0% found this document useful (0 votes)
2 views

Detailed Java Notes

The document provides comprehensive notes on Java programming, covering topics such as Java Applets, AWT Controls, Layout Managers, String Handling, JDBC Fundamentals, Java Servlets, and Java Server Pages (JSP). It includes definitions, lifecycle methods, code examples, and key features for each topic. Additionally, it highlights the advantages of JSP and the use of custom tag libraries like JSTL.

Uploaded by

shivam379a
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Detailed Java Notes

The document provides comprehensive notes on Java programming, covering topics such as Java Applets, AWT Controls, Layout Managers, String Handling, JDBC Fundamentals, Java Servlets, and Java Server Pages (JSP). It includes definitions, lifecycle methods, code examples, and key features for each topic. Additionally, it highlights the advantages of JSP and the use of custom tag libraries like JSTL.

Uploaded by

shivam379a
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Java Programming Notes

UNIT-II

1. Java Applets

Definition: Java Applets are small programs that run within a web browser or applet viewer. They are designed for

GUI-based functionalities and internet-based applications.

**Applet Lifecycle**:

- init(): Called once when the applet is initialized.

- start(): Called each time the applet starts or resumes.

- stop(): Called when the applet is stopped.

- destroy(): Final cleanup before the applet is destroyed.

- paint(Graphics g): Used for custom rendering.

**Code Example**:

```java

import java.applet.Applet;

import java.awt.Graphics;

public class HelloWorldApplet extends Applet {

public void paint(Graphics g) {

g.drawString("Hello World!", 20, 20);

```
**Features**:

- Security: Runs in a restricted environment.

- Portability: Works on any browser supporting Java.

2. AWT Controls

**Overview**: AWT (Abstract Window Toolkit) provides GUI components such as buttons, labels, text fields, etc.

**Common Controls**:

- **Button**: Creates clickable buttons.

- **Label**: Displays non-editable text.

- **TextField**: Input field for users.

- **Choice (ComboBox)**: Dropdown list for selecting one item.

- **List**: Displays multiple selectable items.

**Code Example**:

```java

import java.awt.*;

import java.awt.event.*;

public class AWTExample extends Frame implements ActionListener {

Button button;

TextField textField;

public AWTExample() {

button = new Button("Click Me");

button.setBounds(50, 100, 80, 30);

button.addActionListener(this);

textField = new TextField();

textField.setBounds(50, 50, 150, 20);

add(button);
add(textField);

setSize(300, 300);

setLayout(null);

setVisible(true);

public void actionPerformed(ActionEvent e) {

textField.setText("Button Clicked");

public static void main(String[] args) {

new AWTExample();

```

**Event Listeners**:

- ActionListener: Handles button clicks.

- KeyListener: Handles keyboard events.

- MouseListener: Tracks mouse actions.

3. Layout Managers

**Definition**: Layout Managers arrange components in a container. They handle resizing and positioning automatically.

**Types**:

- FlowLayout: Places components in a row.

- BorderLayout: Divides the container into five regions (N, S, E, W, Center).

- GridLayout: Arranges components in a grid.

4. String Handling
Java strings are immutable objects used to manipulate text. Common methods include:

- length(): Returns the string length.

- charAt(index): Gets a character at a specific index.

- substring(): Extracts a portion of the string.

- equals(): Compares two strings.

- toUpperCase()/toLowerCase(): Converts string case.

---

UNIT-III

1. JDBC Fundamentals

**Definition**: JDBC (Java Database Connectivity) allows Java programs to interact with databases.

**Steps**:

- Load Driver: `Class.forName("com.mysql.cj.jdbc.Driver");`

- Establish Connection: `Connection con = DriverManager.getConnection(url, user, password);`

- Execute Queries using Statement or PreparedStatement.

- Process Results via ResultSet.

**Code Example**:

```java

import java.sql.*;

public class JDBCExample {

public static void main(String[] args) {

try {

Class.forName("com.mysql.cj.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");


Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("SELECT * FROM employees");

while (rs.next()) {

System.out.println(rs.getString("name"));

con.close();

} catch (Exception e) {

System.out.println(e);

```

2. Working with Statements

- Statement: Executes static SQL queries.

- PreparedStatement: Used for parameterized queries.

3. ResultSet Methods:

- next(): Moves to the next row.

- getString(column): Retrieves a column value as a string.

---

UNIT-IV

1. Java Servlets

**Definition**: Java Servlets are server-side programs that process client requests and generate dynamic web content.
**Servlet Lifecycle**:

- init(): Called once during initialization.

- service(): Handles client requests.

- destroy(): Called during servlet termination.

**Session Tracking**:

- Cookies: Small data stored on the client-side.

- URL Rewriting: Appends session ID to URLs.

- HTTP Session: Manages session data on the server.

**Code Example**:

```java

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

public class HelloWorldServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<h1>Hello, World!</h1>");

```

---

UNIT-V
1. Java Server Pages (JSP)

**Definition**: JSP is a server-side technology that combines HTML and Java to create dynamic web content.

**Advantages**:

- Simplified syntax compared to Servlets.

- Built-in support for dynamic content.

**Example**:

```jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" %>

<html>

<body>

<h1>Welcome to JSP</h1>

<%= "Dynamic Content Generated on " + new java.util.Date() %>

</body>

</html>

```

**Custom Tag Libraries**:

- JSTL (JavaServer Pages Standard Tag Library) provides pre-defined tags for tasks like looping and database access.

You might also like