Package addressbook

Source Code of addressbook.ListContacts

package addressbook;

/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* CVS $Id: ListContacts.java,v 1.8 2004/02/08 01:16:21 vgritsenko Exp $
*/
import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.xmldb.api.base.Collection;
import org.xmldb.api.base.ResourceIterator;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.modules.XPathQueryService;

/**
* Class to list all contacts in the database
*/
public class ListContacts extends Action {

   public boolean list(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
  
      Collection col = null;
     
      try {
         // Get a reference to the session
         HttpSession session = request.getSession(true);
  
         // Retrieve the Group instance from the session
         Group group = (Group)session.getAttribute("group");
  
         // Get a collection instance
         col = getCollection(request,response);
  
         XPathQueryService service = (XPathQueryService)col.getService("XPathQueryService",XMLDBAPIVERSION);
  
         // Get all Person elements from the database
         ResourceSet resultSet = service.query("/person");
         ResourceIterator results = resultSet.getIterator();
        
         // Clear out group object...
         group.removeAll();
        
         // Add results of the Xpath query to the Group instance
         group.addResults(results);
        
      } catch (Exception e) {
         e.printStackTrace();

   // there's not much else we can do if the response is committed
   if (response.isCommitted())
       return true;
         e.printStackTrace();

         // Catch the exception and send the user to the error page
         if (e.getMessage() != null ) {
            response.sendRedirect("/addressbook/error.jsp?error=" + URLEncoder.encode(e.getMessage()) );
         }
         else {
            response.sendRedirect("/addressbook/error.jsp" );
         }
      }
           
      return true;
   }
}
TOP

Related Classes of addressbook.ListContacts

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.