# Closed immersion of affine schemes equivalent to surjection on rings

For any morphism of schemes $$f:Y\to X$$, we say that $$f$$ is a closed immersion if:

• $$|Y|$$ is homeomorphic to a closed subspace of $$|X|$$,
• $$f^\#: \mathcal{O}_{X}\to f_*\mathcal{O}_{Y}$$ is surjective on stalks.

Let us specialize to $$f: \text{Spec}(B)\to \text{Spec}(A)$$. Then I want to show that

$$f$$ is a closed immersion if and only if $$A\to B$$ is surjective.

I am able to show the converse. But I am a bit lost on showing the forward direction. Suppose $$f$$ is a closed immersion. My plan is to show that $$A_{\mathfrak{p}}\to B_{\mathfrak{p}}$$ is surjective for all $$\mathfrak{p}\in\text{Spec}(A)$$ as $$A_{\mathfrak{p}}$$-modules, thus implying the surjectivity of $$A\to B$$.

Taking $$\mathfrak{p}\in \text{Spec}(A)$$ (and let us assume $$\mathfrak{q}=f^{-1}(\mathfrak{p})$$ for now), and localizing the sheaf map, we get $$f^\#_{\mathfrak{p}}: A_\mathfrak{p}=\mathcal{O}_{\text{Spec}(A),\mathfrak{p}}\to (f_*\mathcal{O}_{\text{Spec}(B)})_{\mathfrak{p}} = \mathcal{O}_{\text{Spec}(B),\mathfrak{q}} = B_\mathfrak{q}.$$ So this "localization" is not really compatible with the one I want as modules. How should I proceed?

It turns out the localization map is precisely localization as modules: $$(f_*\mathcal{O}_{\text{Spec}(B)})_\mathfrak{p} =\varinjlim_{\mathfrak{p}\in U} \mathcal{O}_{\text{Spec}(B)}(f^{-1}U)= \varinjlim_{t\notin \mathfrak{p}} \mathcal{O}_{\text{Spec}(B)}(f^{-1}D_X(t)) = \varinjlim_{t\notin \mathfrak{p}} \mathcal{O}_{\text{Spec}(B)}(D_Y(f^\#t)) = B_\mathfrak{p}.$$ Here $$D_X(\bullet)$$ and $$D_Y(\bullet)$$ are basic open sets. This immediately implies the global sections $$f^\#:A\to B$$ is injective. So at least for morphisms between affine schemes, I believe that the first topological condition is redundant.