www.pudn.com > 200513174851152(JSP+wml).zip > LoginServlet.java


import java.io.*; 
import javax.servlet.http.*; 
import javax.servlet.*; 
import javax.rmi.*; 
import javax.naming.*; 
import java.util.*; 
 
public class LoginServlet extends HttpServlet 
{ 
 
	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  
   { 
   	try 
		{ 
			System.out.println("Within goGet"); 
	   	String name= null; 
		   String password = null; 
 
	 
			name = req.getParameter("username"); 
			password = req.getParameter("password"); 
		   System.out.println("name - pawd " + name + " :" + password); 
		   System.out.println(name.length()); 
		   System.out.println(password.length()); 
 
			name.trim(); 
			password.trim(); 
			System.out.println(name.length()); 
		   System.out.println(password.length()); 
 
			Collection users = home.findByName(name); 
         Salesman tmpSalesman = (Salesman)home.findByPrimaryKey("0001"); 
         if(tmpSalesman != null) 
         { 
         	System.out.println("got some salesman"); 
            System.out.println(tmpSalesman.getPassword()); 
         } 
                         
			System.out.println("got users size: " + users.size()); 
			if(users.size() <= 0) 
			{ 
				res.sendRedirect(res.encodeURL("/Status.jsp?code=1")); 
			} 
	 
			Iterator i = users.iterator(); 
      	while (i.hasNext())  
      	{ 
				System.out.println("Beginning search"); 
      		Salesman salesman = (Salesman)i.next(); 
            String tmpPassword = (String)salesman.getPassword(); 
            if(password.equals(tmpPassword)) 
            { 
            	HttpSession sess = req.getSession(false); 
            	String id = (String)salesman.getPrimaryKey(); 
					sess.setAttribute("SalesmanId",id); 
					sess.setAttribute("SalesmanName",name); 
					System.out.println("Redirecting to mainmenu.jsp"); 
					res.sendRedirect(res.encodeURL("/MainMenu.jsp")); 
					 
				} 
             
      	} 
   	}catch(Exception npe) 
		{ 
			res.sendRedirect(res.encodeURL("/Status.jsp?code=2")); 
		}		 
	    
   } 
	 
	 
    private Context getInitialContext() throws NamingException 
    { 
    	Properties h = null; 
    	try { 
      // Get an InitialContext 
      	h = new Properties(); 
      	h.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory"); 
	      h.put(Context.PROVIDER_URL, "t3://localhost:7001"); 
      	 
    	}catch(Exception ne) { 
      	System.out.println("Unable to get an initial context"); 
    	} 
	return new InitialContext(h);    
   } 
 
 }