Following example shows how to ask password from a Servlet.

Java Code:
public void askPassword(HttpServletResponse response) {
  response.setStatus(response.SC_UNAUTHORIZED); 
  response.setHeader("WWW-Authenticate",
     "BASIC realm=\"protected-area\"");
}