HttpServletRequest: getRemoteUser() : HttpServletRequest « javax.servlet.http « Java by API






HttpServletRequest: getRemoteUser()

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Hashtable;

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

public class PersonalizedServlet extends HttpServlet {

  Hashtable accesses = new Hashtable();

  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    String remoteUser = req.getRemoteUser();

    if (remoteUser == null) {
      out.println("Welcome!");
    }
    else {
      out.println("Welcome, " + remoteUser + "!");
      Date lastAccess = (Date) accesses.get(remoteUser);
      if (lastAccess == null) {
        out.println("This is your first visit!");
      }
      else {
        out.println("Your last visit was " + accesses.get(remoteUser));
      }
      if (remoteUser.equals("User A")) {
        out.println("HI.");
      }
      accesses.put(remoteUser, new Date());
    }

  }
}

           
       








Related examples in the same category

1.HttpServletRequest: getAuthType()
2.HttpServletRequest: getCookies()
3.HttpServletRequest: getHeaders(String name)
4.HttpServletRequest: getHeader( ) by Accept-Language
5.HttpServletRequest: getHeader( ) by Accept-Charset
6.HttpServletRequest: getRequestURI()
7.ServletRequest: getReader()
8.ServletRequest: getRequestDispatcher(String arg0)
9.HttpServletRequest: getHeaderNames()
10.HttpServletRequest: getPathInfo()
11.HttpServletRequest: getPathTranslated()
12.HttpServletRequest: getQueryString()
13.HttpServletRequest: getSession()
14.HttpServletRequest: getUserPrincipal()
15.HttpServletRequest: isRequestedSessionIdFromCookie()
16.HttpServletRequest: isRequestedSessionIdFromURL()
17.HttpServletRequest: isRequestedSessionIdValid()
18.HttpServletRequest: isUserInRole(String arg0)