How to recover from a blunder? I have been in a situation where I have not been paying attention and suddenly I move my queen to an unprotected square and it is taken.  My initial thought is:

Oh $*%! :)

But if the blunder is mediocre or not devastating, how does one get into the mindset of remaining optimistic and still playing for the win?  I have lost my queen before and still went on to win, but this is mainly due to the fact that my opponent does not know how to take advantage of the opportunity.

Many players who make a blunder, obviously depending on how big it is, might just give up in the game when they actually still have a chance to win or at least draw.
After you blunder, it's incredibly common to blunder a second time over the next few moves.  The most important thing you can do is to avoid this second blunder.

In order to do that, you should take a few deep breaths and even get up and walk around.  Although it's nice if your opponent doesn't know that you blundered (i.e. your "blunder" could be part of a deep plan so far as your opponent knows...), it's far more important to remain calm and stay focused on the game.

After this initial "cool down", now it's time to re-evaluate the game.  First, are you hopelessly lost at this point, or is the position still unclear?  Perhaps you gave away your winning advantage, but you are still equal.  If you are not lost, it is crucial that you maintain your focus and play accurately - you still have a chance to win or draw.

On the other hand, if your blunder was so egregious that your game is hopeless now, it's time to start thinking about "tricks".  For example, if you hung a pawn or two for nothing, it's time to attack with abandon.  If you can checkmate your opponent, it doesn't matter that you're losing an endgame.  If you hung an entire piece, keep all other material on the board and maintain tension in the position.  The goal here is to try to win tactically (win material back from your opponent or go for an attack).  Another common theme is to introduce time pressure into the game.  Either play tricky moves and force your opponent to get low on time, or let your own clock tick down to a minute or two and pray that your opponent blunders in your time pressure.

As a final note, when you are behind in a chess game, it's almost always good to trade pawns, but not pieces.  The rationale is that in an endgame, each pawn is a potential queen for your opponent, while on the other hand, pawnless endgames with an extra piece are notoriously difficult to convert to a win.