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: Task.java,v 1.7 2004/02/08 01:16:21 vgritsenko Exp $
*/
import java.beans.Beans;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
/**
Servlet class for the addressbook
This class acts as a controller, forwarding HTML request onto the correct class/jsp page
depending on the value of request.action
*/
public class Task extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
JspFactory jspFactory = null;
PageContext pageContext = null;
HttpSession session = null;
Group group = null;
String action = null;
try {
// If the Group bean hasn't been added to the session alerady, add it
// this allows the user to go directly to ANY page
jspFactory = JspFactory.getDefaultFactory();
pageContext = jspFactory.getPageContext(this, request, response, "/error.jsp", true, 8192, true );
session = pageContext.getSession();
group = (Group)pageContext.getAttribute("group",PageContext.SESSION_SCOPE);
if (group == null) {
group=(Group)Beans.instantiate(this.getClass().getClassLoader(), "addressbook.Group" );
pageContext.setAttribute("group", group, PageContext.SESSION_SCOPE );
}
// Since case matters, we need to check for both....
if ( request.getParameter("action") != null ) {
action = request.getParameter("action");
}
else {
action = request.getParameter("ACTION");
}
// Decide where to send the user depending on value of action
if ( action != null ) {
// Add a Contact
if ( action.equalsIgnoreCase("addform") ) {
gotoPage("/addContact.jsp",request,response);
} else if ( action.equalsIgnoreCase("addcontact") ) {
AddContact contact = new AddContact();
contact.add(request,response);
ListContacts lcontact = new ListContacts();
lcontact.list(request,response);
gotoPage("/listContacts.jsp",request,response);
// List all contacts
} else if ( action.equalsIgnoreCase("listcontacts") ) {
ListContacts contact = new ListContacts();
contact.list(request,response);
gotoPage("/listContacts.jsp",request,response);
// Show individual Contact detail
} else if ( action.equalsIgnoreCase("showcontact") ) {
gotoPage("/showContact.jsp",request,response);
// Show individual XML file
} else if ( action.equalsIgnoreCase("showxml") ) {
gotoPage("/showXML.jsp",request,response);
// Edit a contact
} else if ( action.equalsIgnoreCase("editcontactform") ) {
gotoPage("/editContactForm.jsp",request,response);
} else if ( action.equalsIgnoreCase("editcontact") ) {
EditContact contact = new EditContact();
contact.edit(request,response);
ListContacts lcontact = new ListContacts();
lcontact.list(request,response);
gotoPage("/listContacts.jsp",request,response);
// Search for a contact
} else if ( action.equalsIgnoreCase("searchform") ) {
gotoPage("/searchContacts.jsp",request,response);
} else if ( action.equalsIgnoreCase("attributesearch") ) {
SearchContacts contact = new SearchContacts();
contact.attributeSearch(request,response);
gotoPage("/listContacts.jsp",request,response);
} else if ( action.equalsIgnoreCase("xpathsearch") ) {
SearchContacts contact = new SearchContacts();
contact.xpathSearch(request,response);
gotoPage("/listContacts.jsp",request,response);
// Delete a contact
} else if ( action.equalsIgnoreCase("deletecontact") ) {
DeleteContact contact = new DeleteContact();
contact.delete(request,response);
ListContacts lcontact = new ListContacts();
lcontact.list(request,response);
gotoPage("/listContacts.jsp",request,response);
} else gotoPage("/error.jsp",request,response);
}
else {
gotoPage("/error.jsp",request,response);
}
} catch( Exception e ) {
e.printStackTrace();
// there's not much else we can do if the response is committed
if (response.isCommitted())
return;
// 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" );
}
}
}
// Treat a form-get as a post, seding user to doPost
public void doGet(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
doPost(request,response);
}
// Forward the user on to the pages specified in address
private void gotoPage( String address, HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
dispatcher.forward(request, response);
}
}