HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Enterprise Java"
Vol. 6, Issue 4, p. 64

	





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>");
 }




  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: info@sys-con.com

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.