1 package csdl.stackmvc.control; 2 3 import java.io.IOException ; 4 5 import javax.servlet.RequestDispatcher ; 6 import javax.servlet.ServletConfig ; 7 import javax.servlet.ServletException ; 8 import javax.servlet.http.HttpServlet ; 9 import javax.servlet.http.HttpServletRequest ; 10 import javax.servlet.http.HttpServletResponse ; 11 12 import csdl.stackmvc.control.command.Command; 13 14 21 public class Controller extends HttpServlet { 22 23 private String exceptionAttribute = "javax.servlet.jsp.jspException"; 24 private String commandParameter = "CommandName"; 25 26 27 33 public void init(ServletConfig conf) throws ServletException { 34 super.init(conf); 35 } 36 37 38 52 public void doPost(HttpServletRequest request, HttpServletResponse response) 53 throws ServletException , IOException { 54 55 String commandName = request.getParameter(commandParameter); 57 RequestDispatcher dispatcher; 58 59 request.setAttribute("errorMessage", ""); 61 62 if (commandName == null) { 64 commandName = "Clear"; 65 } 66 67 try { 69 String commandClassName = "csdl.stackmvc.control.command." 70 + commandName + "Command"; 71 Command command = (Command) Class.forName(commandClassName).newInstance(); 72 Page page = command.process(request); 73 request.setAttribute("PageTitle", page.getTitle()); 74 dispatcher = getServletContext().getRequestDispatcher(page.getFileName()); 75 } 76 catch (Exception e) { 77 request.setAttribute(exceptionAttribute, e); 78 dispatcher = getServletContext().getRequestDispatcher(Page.ERROR.getFileName()); 79 } 80 81 dispatcher.forward(request, response); 83 } 84 85 86 94 public void doGet(HttpServletRequest request, HttpServletResponse response) 95 throws ServletException , IOException { 96 doPost(request, response); 97 } 98 } 99 100 | Popular Tags |