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


import java.io.*; 
import javax.servlet.http.*; 
import javax.servlet.*; 
import javax.rmi.*; 
import javax.naming.*; 
import java.util.*; 
 
 
public class ClientViewServlet extends HttpServlet 
{ 
 
	 
	ClientHome clientHome; 
	 
	public void init() throws ServletException 
	{ 
		try 
		{ 
			System.out.println("trying to get initial context"); 
			Context ic = (InitialContext) getInitialContext(); 
			System.out.println("Got InitContext"); 
			Object objRef = ic.lookup("Client"); 
			System.out.println("Got obj ref"); 
			clientHome = (ClientHome) PortableRemoteObject.narrow(objRef,ClientHome.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 
		{ 
			HttpSession session = req.getSession(false); 
			String strSalesmanId = (String)session.getAttribute("SalesmanId"); 
			System.out.println("salesman id: " + strSalesmanId); 
			Collection clients = clientHome.findBySalesman(strSalesmanId); 
			if(clients.size() <= 0) 
			{ 
				System.out.println("Clients size: " + clients.size()); 
				res.sendRedirect(res.encodeURL("/Status.jsp?code=3")); 
			} 
			Vector cinfo[] = new Vector[clients.size()]; 
			System.out.println("length of client list: " + clients.size()); 
			Iterator i = clients.iterator(); 
			int cnt=0; 
			System.out.println("Beginning search"); 
 
			while(i.hasNext()) 
			{ 
				 
				Client client = (Client) i.next(); 
				cinfo[cnt] = new Vector(3); 
				cinfo[cnt].add(client.getId()); 
				cinfo[cnt].add(client.getName()); 
				cinfo[cnt].add(client.getAddress()); 
				cnt++; 
			 
			} 
			session.setAttribute("ClientList",cinfo); 
			res.sendRedirect(res.encodeURL("/ClientList.jsp")); 
		}catch(Exception e){ 
			System.out.println("Error while obtaining client list:" + e.getMessage()); 
			res.sendRedirect(res.encodeURL("/Status.jsp?code=1")); 
 
		} 
	} 
	 
	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);    
   } 
 
}