I am going to be very rude to you for your own good.
So it works in a JSP, but not in a servlet huh? This is VERY odd, do you know why? Because a JSP is translated into a servlet the first time you run it, the code IS running in a servlet and working... just not in the servlet that you created.
"it does not work" is not much of a description. Are you getting an exception? Post the code of your servlet so that we can spot the mistake, because you most definately made a mistake somewhere (bluntly copy/pasting code and expecting it to work is the first one).