Simple Servlet Example using the getRemoteUser() method. : Authentication « Servlet « Java Tutorial






import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MainClass extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
      java.io.IOException {

    res.setContentType("text/html");
    java.io.PrintWriter out = res.getWriter();
    out.println("<HTML>");
    out.println("<HEAD><TITLE>User Example</TITLE></HEAD>");
    out.println("<BODY>");

    String username = req.getRemoteUser();
    if (username == null) {
      out.println("Hello. You are not logged in.");
    } else if ("Bob".equals(username)) {
      out.println("Hello, Bob. Nice to see you again.");
    } else {
      out.println("Hello, " + username + ".");
    }
    out.println("</BODY>");
    out.println("</HTML>");
    out.close();
  }
}








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