Hamlet had, at the end of the previous act, expressed his anger and frustration at himself and what he believes is his cowardice in his epic "to be or not to be" monologue. When he and Ophelia meet, he is obviously still filled with a cornucopia of emotions. Ophelia now becomes a victim of his pent-up emotions.
Much of Hamlet's anger stems from his frustration that his mother has replaced his father with someone he absolutely despises—someone who, he is beginning to suspect, may have murdered his father. Gertrude married Claudius, Hamlet believes, much too soon after his father's untimely demise: a betrayal.
When Hamlet meets Ophelia, his deep bitterness and resentment for his mother's betrayal spill over and he expresses these sentiments to her. She becomes an innocent victim of his anger. He pours out all his negativity towards his mother and hurls his deep frustrations at her. His mother, to Hamlet, has come to symbolize the worst in all women and since Ophelia is a woman, she must, as a consequence, be the same.
It is remarkable that Hamlet should be so harsh toward Ophelia because it seems he actually has much affection for her. Throughout their discourse he becomes quite commanding and essentially states that women are fickle hypocrites.
Hamlet tells her that the world would be a better place if she should become a nun and not bear children. Even bringing children into the world would be criminal. In this instance, he ironically uses himself as an example of how vile and destructive men are. Adding to the world's woes by bringing another such as himself into it would be wrong; so Ophelia should consider celibacy.
Our unfortunate young prince is clearly disgusted by his mother but, on a secondary level, his aggressive outburst is also informed by his awareness of being snooped on. The dramatic outpouring of his sentiment adds to his plan to deceive those he believes are spying on him to think that his actions are an expression of his "antic disposition." Claudius does, indeed, later state that:
Madness in great ones must not unwatch'd go.
Hamlet's confrontation with Ophelia also foreshadows his encounter with his mother in Act lll, scene lV. In this scene he verbally lashes out at and comes close to physically assaulting her. Gertrude believes that he wants to kill her and screams. Her reaction gives rise to other complications, which introduce a dramatic series of events that, ultimately, culminate in terrible tragedy.
Hamlet suspects that Polonius may be watching, which, of course, he is. He is angry with Ophelia for setting him up in this way and with Polonius for trying to use Ophelia to determine his motives. Later in the play, Hamlet will insist that he loved Ophelia, but his actions in this scene so that he can also become violent or at least thinks he must act violent, to preserve the facade that he is mad. Thus, he hurts Ophelia with his admonition to go to a nunnery and convinces Claudius and Polonius that the cause of his depression is not his inability to see Ophelia.
Hamlet is unjustly transferring his mother's guilt onto Ophelia, presuming she is unchaste simply because that is (according to the role model of his mother) the nature of the weaker sex. In psychology, this kind of presumption based solely on one's own experience is called "supposition of the similitude."
It is Polonius' duty as a father to look after his daughter's comings and goings, and Hamlet thinks he should be paying closer attention to his daughter's whereabouts (since surely she must be up to no good!) Hamlet shows contempt for Ophelia because she inaccessible to him (and therefore eventually available to the next courtier which comes along). By complying to her father's demands of keeping her distance from Hamlet, Ophelia seems to be rejecting his love and giving reason to her father.