Volume: 6 Issue: 4
Enterprise Java
by Tony Loton


Listing 1
public class LoginServlet extends HttpServlet
{
public void doGet(HttpServletRequest req
, HttpServletResponse res) throws IOException
{
  // -- find out who the remote user is --
  String user=req.getRemoteUser();


  // -- get the current http session --
  HttpSession session=req.getSession(true);


  res.setContentType("text/html");
  PrintWriter out = res.getWriter();


  if (user==null)
  {
    // -- authentication failed, show error --
  }
  else
  {
    // -- save user name for future servlets --
    session.setAttribute("user", user);


    // -- respond with HTML including --
    // -- this link to an application --
    out.println("<a href='AppPage'>Click here to run an application.</a>");
  }
}
}



Listing 2
public class AppServlet extends HttpServlet
{
public void doGet(HttpServletRequest req
, HttpServletResponse res) throws IOException
{
  // -- get the current http session -
  HttpSession session=req.getSession(true);


  res.setContentType("text/html");
  PrintWriter out = res.getWriter();


  // -- get the current user --
  String user=(String)
   session.getAttribute("user");


  if (user==null)
  {
    // -- not authenticated, show message --
  }
  else if (user.equals("bill"))


  {
    // -- we like this user, so run the app --
  }
  else
  {
    // -- authenticated, not allowed this app --
  }
}
}



Listing 3
public void doGet(HttpServletRequest req
, HttpServletResponse res) throws IOException
{
// -- find out who the remote user is --
String user=req.getRemoteUser();


// -- get the current http session --
HttpSession session=req.getSession(true);


res.setContentType("text/html");
PrintWriter out = res.getWriter();


if (user==null)
{
  // -- print the unauthorized message here --
}
else
{
  // -- save the user name for future servlets --
  session.setAttribute("user", user);


  out.println("<html>");
  out.println("<head><title>LoginServlet</title></head>");
  out.println("<body>");


  Vector options=new Vector();


  try
  {
    // -- get DB connection from a datasource --
    InitialContext ic = new InitialContext();


    DataSource ds = (DataSource)
     ic.lookup("java:comp/env/jdbc/LOTONtech");


    Connection con = ds.getConnection();


    // -- select user's menu options --
    Statement st=con.createStatement();


    ResultSet results=st.executeQuery
     ("SELECT username, useroption
     FROM useroptions WHERE username='"+user+"'");


    while (results.next())
    {
      String useroption=results.getString(2);
      options.addElement(useroption);
    }
  }
  catch (Exception e)
  {
    out.println("ERROR: Connecting to database!");
  }


  //-- write the options out as html --
  out.println("Welcome "+user+", choose an option:");


  for (int opNum=0; opNum<options.size(); opNum++)
  {
    String thisOption=(String)
     options.elementAt(opNum);


    out.println("<a href='"+thisOption
     +"' target='main'><font size=3><b>"
     +thisOption+"</b></font></a>  ");
  }


  out.println("</body>");
  out.println("</html>");
}