<%@ page language="java" contentType="application/json; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*, org.json.JSONArray, org.json.JSONObject" %>
<%
    String teacherId = request.getParameter("teacher");
    JSONArray courses = new JSONArray();

    if (teacherId != null && !teacherId.isEmpty()) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String sql = "SELECT * FROM courses WHERE teacher_id = ?";
            try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "SchoolST", "123456");
                 PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, teacherId);
                try (ResultSet rs = pstmt.executeQuery()) {
                    while (rs.next()) {
                        JSONObject course = new JSONObject();
                        course.put("course_id", rs.getInt("course_id"));
                        course.put("course_name", rs.getString("course_name"));
                        courses.put(course);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    out.print(courses.toString());
%>