import java.io.*; import javax.servlet.http.*; import javax.servlet.*; import javax.rmi.*; import javax.naming.*; import java.util.*; public class LoginServlet extends HttpServlet { /* The Salesman EJB is an entity ejb */ SalesmanHome home; Salesman salesman; public void init() throws ServletException { try { System.out.println("trying to get initial context"); Context ic = getInitialContext(); System.out.println("Got InitContext"); Object objRef = ic.lookup("Salesman"); System.out.println("Got obj ref"); home = (SalesmanHome) PortableRemoteObject.narrow(objRef,SalesmanHome.class); System.out.println("Got home"); }catch(Exception e) { System.out.println("Error in init"); e.printStackTrace(); } } public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String name = null; String password = null; try { System.out.println("Within goGet"); /* The parameter names are 'username' and 'password' */ name = req.getParameter("username"); password = req.getParameter("password"); if((name == null) || (name.length() <= 0) || (password == 0) || (password.length() <=0 )) { res.sendRedirect(res.encodeURL("/Status.jsp?code=2")); } name = name.trim(); password = password.trim(); /* Try to see if a salesman by this 'name' exists. The finder method findByName of the Salesman EJB, does this. It returns a java.util.Collection object containing the results of the search. */ Collection users = home.findByName(name); if(users.size() <= 0) { /* No salesmen found by the specified name */ res.sendRedirect(res.encodeURL("/Status.jsp?code=3")); } Iterator i = users.iterator(); /* Iterate throught the search results of findByName. Check if the password of any results matches the password entered */ while (i.hasNext()) { Salesman salesman = (Salesman)i.next(); String tmpPassword = (String)salesman.getPassword(); if(password.equals(tmpPassword)) { /* Check if an old session exists. If it does then invalidate it and create a new one */ HttpSession sess = req.getSession(false); if(sess != null) // a null is returned if an old session does not exist { sess.invalidate(); //invalidate old session } sess = req.getSession(); //create a new session String id = (String)salesman.getPrimaryKey(); //get salesman's id /* At this point the salesman has been validated and his id obtained. Store these parameters in the session. */ sess.setAttribute("SalesmanId",id); sess.setAttribute("SalesmanName",name); res.sendRedirect(res.encodeURL("/MainMenu.jsp")); } } }catch(Exception npe) { if((name == null) || (name.length() <= 0) || (password == 0) || (password.length() <=0 )) { res.sendRedirect(res.encodeURL("/Status.jsp?code=2")); } else res.sendRedirect(res.encodeURL("/Status.jsp?code=1")); } } private Context getInitialContext() throws NamingException { Context initial = null; try { /* There are other methods of obtaining the initial context too. Check the J2EE docs and your application server docs for these. */ initial = new InitialContext(); }catch(Exception ne) { System.out.println("Unable to get an initial context"); } return initial; } }