public class ProtectedServlet extends HttpServlet {
public void init(ServletConfig cfg) throws ServletException {
super.init(cfg);
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
String authType = req.getAuthType();
out.println("You are authorized to view this page");
out.println("You were authenticated using: " + authType
+ " method of authentication");
Principal princ = req.getUserPrincipal();
out.println("The user is: " + princ.getName());
}
}
web.xml文件如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">