Get Auth Type from Servlet Request : Authentication « Servlet « Java Tutorial






import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.security.*;

public class MyServlet extends HttpServlet {
   
    public void init(ServletConfig cfg) throws ServletException 
    {
        super.init(cfg);
    } 

    public void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws IOException, ServletException 
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<HTML>");
        out.println("<HEAD>");
        out.println("<TITLE>");
        out.println("User Authentication");
        out.println("</TITLE>");
        out.println("</HEAD>");
        out.println("<BODY>");
        out.println("<H1>User Authentication</H1>");

        String type = request.getAuthType();
        out.println("Welcome to this secure page.<BR>");
        out.println("Authentication mechanism: " + type + "<BR>");
        Principal principal = request.getUserPrincipal();
        out.println("Your username is: " + principal.getName() + "<BR>");

        out.println("</BODY>");
        out.println("</HTML>");
    } 
}
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
    <servlet><servlet-name>MyServletName</servlet-name>
             <servlet-class>MyServlet</servlet-class>

             
    </servlet>
    
    <servlet-mapping><servlet-name>MyServletName</servlet-name>
        <url-pattern>/index.html</url-pattern>
    </servlet-mapping>
</web-app>
  Download:  ServletRequestAuthType.zip( 88 k)








25.22.Authentication
25.22.1.Simple Servlet Example using the getRemoteUser() method.
25.22.2.Get Auth Type from Servlet Request
25.22.3.Servlet Login
25.22.4.A password protected servlet
25.22.5.Servlet with bouncy castle security package