We have the case of a radiant field confined in one dimensional cavity along the z-axis with perfectly conducting walls at $z = 0$ and $z = L$, so that the electric field vanish in the boundaries as shown in:
\begin{equation} \nabla \times \textbf{E} = \frac{\partial \textbf{B}}{\partial t} \end{equation}
\begin{equation} \nabla \times \textbf{B} = \mu_0 \varepsilon_0 \frac{\partial \textbf{E}}{\partial t} \end{equation}
\begin{equation} \nabla \cdot \textbf{B} = 0 \end{equation}
\begin{equation} \nabla \cdot \textbf{E} = 0 \end{equation}
The field is polarized in x-direction, i.e. $\textbf{E}(\textbf{r},t) = \mathbf{e}_x E_x(z,t)$, with $\textbf{e}_x $ an unit polarization vector. A field that satisfies Maxwell's equations and boundary conditions is:
\begin{equation} E_x(z,t)=\left( \frac{2\omega^{2}}{V\varepsilon_0} \right)^{1/2}q(t) \text{sin}(kz) \end{equation}
With $\omega$ the frequency of the mode, $k=\cfrac{\omega}{c}$ the wave number, $V$ the effective volume of the cavity and $q(t)$ the canonical position. We can not have all frequencies due to the boundary condition at $z=L$, this only allowed frequencies $\omega_m = c \cfrac{m\pi}{L}, \quad m=1,2,\cdots$. From Eq. (2) and Eq. (5) we obtain:
\begin{equation*} \textbf{e}_x \left( \partial_y B_z - \partial_z B_y \right) + \textbf{e}_y \left( \partial_z B_x - \partial_x B_z \right) + \textbf{e}_z \left( \partial_x B_y - \partial_y B_x \right) = \textbf{e}_x \left( \mu_0\varepsilon_0 \partial_t E_x \right) \\ \partial_y B_z - \partial_z B_y = \mu_0\varepsilon_0 \left( \frac{2\omega^{2}}{V\varepsilon_0} \right)^{1/2} \text{sin}(kz) \partial_t q = \mu_0\varepsilon_0 \left( \frac{2\omega^{2}}{V\varepsilon_0} \right)^{1/2} \dot{q}(t) \text{sin}(kz) \\ - \partial_z B_y = \mu_0\varepsilon_0 \left( \frac{2\omega^{2}}{V\varepsilon_0} \right)^{1/2} \dot{q}(t) \text{sin}(kz) \end{equation*}
\begin{equation} \Rightarrow B_y (z,t) = \frac{\mu_0\varepsilon_0}{k} \left( \frac{2\omega^{2}}{V\varepsilon_0} \right)^{1/2} \dot{q}(t) \text{cos}(kz) \end{equation}
The Hamiltonian of the field described above is:
\begin{equation} H=\frac{1}{2}\int dV \left[ \varepsilon_0 E_x^{2}(z,t) + \frac{1}{\mu_0}B_y^{2}(z,t) \right] \end{equation}
Substituting Eq. (5) and Eq. (6) in Eq. (7):
\begin{equation*} H=\frac{1}{2} \left[ \varepsilon_0 \frac{2\omega^{2}}{V\varepsilon_0} q^2(t) \int dV \text{sin}^2(kz) + \frac{1}{\mu_0} \frac{\mu_0^2 \varepsilon_0^2}{k^2} \frac{2\omega^{2}}{V\varepsilon_0} \dot{q}^2(t) \int dV \text{cos}^2(kz) \right] \end{equation*}
It is important to emphasize that $V$ in the equations for $E_x$ and $B_y$ is the volume of the cavity, i.e. it is a constant. Noting that $k=\cfrac{\omega}{c}$ and $\mu_0\varepsilon_0 = \cfrac{1}{c^2}$
\begin{equation*} H=\frac{1}{2} \left[ \frac{2\omega^{2}}{V} q^2(t) \int dV \text{sin}^2(kz) + \frac{2}{V} \dot{q}^2(t) \int dV \text{cos}^2(kz) \right] \end{equation*}
For this case we have $\int dV = A\int_L dz$, so:
\begin{align*} H &= \frac{1}{2}A \left[ \frac{2\omega^{2}}{V} q^2(t) \int_L dz \; \text{sin}^2(kz) + \frac{2}{V} \dot{q}^2(t) \int_L dz \; \text{cos}^2(kz) \right] \\ \\ &= \frac{1}{2}A \left[ \frac{\omega^2 q^2(t)L}{V} + \frac{\dot{q}^2(t) L}{V} \right]\end{align*}
Considering a particle of unit mass, i.e. the canonical momentum is $p(t) = \dot{q}(t)$ and noting that $V=AL$, the Hamiltonian results:
\begin{equation} \boxed{H = \frac{1}{2} \left( p^2 + \omega^2 q^2 \right)} \end{equation}