SHOW:
|
|
- or go back to the newest paste.
| 1 | - | //gets cars from database, puts them in memory, and redirects to original JSP file |
| 1 | + | |
| 2 | ||
| 3 | import java.io.IOException; | |
| 4 | import java.util.List; | |
| 5 | import java.util.ArrayList; | |
| 6 | import javax.servlet.*; | |
| 7 | import javax.servlet.http.*; | |
| 8 | import com.DAO.*; | |
| 9 | import com.DTO.*; | |
| 10 | import com.java.names; | |
| 11 | import com.java.sender; | |
| 12 | ||
| 13 | ||
| 14 | public class ControllerServlet extends HttpServlet | |
| 15 | {
| |
| 16 | private static final String ACTION_KEY = "action"; | |
| 17 | private static final String VIEW_NAMES_ACTION = "viewNames"; | |
| 18 | private static final String SEARCH_FIRST_ACTION = "searchFirst"; | |
| 19 | private static final String SEARCH_LAST_ACTION = "searchLast"; | |
| 20 | private static final String ERROR_KEY = "errorMessage"; | |
| 21 | private static final String ERROR_PAGE = "/error.jsp"; | |
| 22 | private static final String XA_QUEUE_CONNECTION_FACTORY = "java:comp/env/jms/MyXAQueueConnectionFactory"; | |
| 23 | private static final String NAME_QUEUE = "java:comp/env/jms/nameQueue"; | |
| 24 | ||
| 25 | public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException | |
| 26 | {
| |
| 27 | try {
| |
| 28 | processRequest(request, response); | |
| 29 | } catch (Exception e) {
| |
| 30 | e.printStackTrace(); | |
| 31 | } | |
| 32 | } | |
| 33 | ||
| 34 | public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException | |
| 35 | {
| |
| 36 | try {
| |
| 37 | processRequest(request, response); | |
| 38 | } catch (Exception e) {
| |
| 39 | e.printStackTrace(); | |
| 40 | } | |
| 41 | } | |
| 42 | ||
| 43 | protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws Exception | |
| 44 | {
| |
| 45 | String actionName = request.getParameter(ACTION_KEY); | |
| 46 | String destinationPage = ERROR_PAGE; | |
| 47 | ||
| 48 | nameDAO DAO = new nameDAO(); | |
| 49 | ||
| 50 | //perform action | |
| 51 | if(VIEW_NAMES_ACTION.equals(actionName)) | |
| 52 | {
| |
| 53 | request.setAttribute("nameList", DAO.findAll());
| |
| 54 | ||
| 55 | destinationPage = "/nameList.jsp"; | |
| 56 | } | |
| 57 | else if(SEARCH_FIRST_ACTION.equals(actionName)) | |
| 58 | {
| |
| 59 | System.out.println("SEARCH FOR FIRST NAME");
| |
| 60 | sender s = new sender("first", request.getParameter("last"));
| |
| 61 | } | |
| 62 | else if(SEARCH_LAST_ACTION.equals(actionName)) | |
| 63 | {
| |
| 64 | System.out.println("SEARCH FOR LAST NAME");
| |
| 65 | sender s = new sender("last", request.getParameter("first"));
| |
| 66 | ||
| 67 | } | |
| 68 | else | |
| 69 | {
| |
| 70 | String errorMessage = "[" + actionName +"] is not a valid action."; | |
| 71 | request.setAttribute(ERROR_KEY, errorMessage); | |
| 72 | } | |
| 73 | ||
| 74 | //redirect to destination page | |
| 75 | RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(destinationPage); | |
| 76 | ||
| 77 | dispatcher.forward(request, response); | |
| 78 | } | |
| 79 | } |