MVC design Structure
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Tony_Tuckert
Posted On:   Wednesday, February 21, 2007 12:07 PM

I'm implementing a property search online application. I'm doing this in MVC design pattern, which consist of a main controller which maps a request action to a corresponding sub controller to handle the processing. This is done using a hashmap. The problem occurs during runtime, basically some actions are handled and some are not. the thing is its tempermental. Sometimes it works and sometimes it doesnt. Looking at the bundled tomcat, there errors stating it doesnt recognise the put() method in userActionController and for example in the myAppointments action controller when you need to get a session value the line before that is where the error indicates. This suggest its to do with grabbing session variables.    More>>

I'm implementing a property search online application.

I'm doing this in MVC design pattern, which consist of a main controller which maps a request action to a corresponding sub controller to handle the processing. This is done using a hashmap.

The problem occurs during runtime, basically some actions are handled and some are not. the thing is its tempermental.

Sometimes it works and sometimes it doesnt.

Looking at the bundled tomcat, there errors stating it doesnt recognise the put() method in userActionController and for example in the myAppointments action controller when you need to get a session value the line before that is where the error indicates. This suggest its to do with grabbing session variables.


package Controller;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

import Model.*;

/**
*
* @author Compaq_Owner
* @version
*/
public class userActionController extends HttpServlet {

private HashMap actions = new HashMap(50);
private final String errorPage = "http://localhost:8084/error.html";


public void init(ServletConfig sCon) throws ServletException {
super.init(sCon);


// sets the actions values and maps them to a sub-controller that will handle the processing
actions.put("login", "/loginActionController");
actions.put("register", "/registerActionController");
actions.put("propertySearch", "/propertySearchActionController");
actions.put("myAppointments", "/myAppointmentsActionController");
actions.put("moreAppointmentDetails", "/moreAppointmentDetailsActionController");
actions.put("morePropertyDetails", "/morePropertyDetailsActionController");
actions.put("editProperty", "/editPropertyActionController");
actions.put("adminPropertySearch", "/adminPropertySearchActionController");
actions.put("registerInterest", "/registerInterestActionController");
actions.put("addProperty", "/addPropertyActionController");
actions.put("bookAppointment", "/bookAppointmentActionController");
actions.put("selectAppointment", "/selectAppointmentActionController");
actions.put("backHomepage", "/backHomepageActionController");
actions.put("login", "/loginActionController");

}


/** Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
*/

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");
HttpServletRequest theRequest = request;
HttpSession session = theRequest.getSession(false);

if (session == null) {
response.sendRedirect(errorPage);
}

String action = theRequest.getParameter("action");
String url = (String) actions.get(action);

//session.setAttribute("actionRequest", request);

System.out.println("***** "+action+" *****");

//The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.
ServletContext servCon = getServletContext();
RequestDispatcher reqDis = servCon.getRequestDispatcher(url);
reqDis.forward(theRequest, response);
}
//
/** Handles the HTTP GET method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try
{
processRequest(request, response);
}
catch (Exception e)
{
e.printStackTrace();
}
}

/** Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try
{
processRequest(request, response);
}
catch (Exception e)
{
e.printStackTrace();
}
}



/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
//
}





package Controller.subControllers;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

import Model.*;

/**
*
* @author Administrator
* @version
*/
public class myAppointmentsActionController extends HttpServlet {

private final String errorPage = "http://localhost:8080/error.html";

public void init(ServletConfig sCon) throws ServletException {
super.init(sCon);
}


/** Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
*/

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");
HttpServletRequest theRequest = request;
// session will equal null if currently there isnt a request
HttpSession session = theRequest.getSession(false);

if (session == null) {
response.sendRedirect(errorPage);
}


//HttpServletRequest theRequest = (HttpServletRequest) session.getAttribute("actionRequest");
ServletContext sc = getServletContext();

appointment a = new appointment();
appointmentList al = new appointmentList();

user u = (user) session.getAttribute("Auser");
int uid = u.getID();

al.getAppointments(uid);

session.setAttribute("appointmentList", al);

response.sendRedirect("myappointments.jsp");
}


//
/** Handles the HTTP GET method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/** Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
//
}

   <<Less

Re: MVC design Structure

Posted By:   Robert_Lybarger  
Posted On:   Wednesday, February 21, 2007 10:28 PM

Wow... my eyes are positively bleeding. Maybe you could try again : use
 block(s) for your code dumps, and use preview/edit to make sure it, you know, looks like code. Then again, I'm sure the volume of code after formatting would still be more than I'd want to wade through, but you'd at least improve your odds from "zero."
About | Sitemap | Contact