1

I have a jee project in which im using MVC and front controller design patterns.

I actually have all the links like this :

<a href="Controller?page=ForwardProducts">Products</a>

Or from javascript to make ajax calls:

Controller?page=AJAX&type=CheckID&_id=something

And a bit of code, whenever the links are pressed they go to a servlet that forwards info to the proper .class and receives the info (PD: the switch is bigger, just made it smaller):

        switch (request.getParameter("page")) {
            case "AJAX":
                String ajaxClass = request.getParameter("type");
                IAJAX _ajax = (IAJAX) Class.forName("my.package.Ajax." + ajaxClass).newInstance();
                String _json = _ajax.getJSON(request, response);
                try (PrintWriter out = response.getWriter()) {
                    out.print(_json);
                }   
                break;
            default:
                ICommand _command = (ICommand) Class.forName("my.package.classes." + request.getParameter("page")).newInstance();
                _command.initPage(request, response);
                _includePage = _command.execute(request, response);
                request.getSession().setAttribute("_includePage", _includePage);
                request.getRequestDispatcher("/index.jsp").forward(request, response);
                break;
        }

The ForwardAbout inside classes package looks like this:

public class ForwardAbout extends ICommand{
@Override
public void initPage(HttpServletRequest request, HttpServletResponse response) throws Exception {

}

@Override
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
    return "/about.jsp";
}
}

And the ICommand:

public abstract class ICommand {

public void initPage(HttpServletRequest request, HttpServletResponse response) throws Exception {

}

public abstract String execute(HttpServletRequest request, HttpServletResponse response) throws Exception;

}

I want to make pretty links, for example:

http://localhost:8084/testWeb/Controller?page=ForwardAbout

To

http://localhost:8084/testWeb/about-us

But also reverse, when an user writes http://localhost:8084/testWeb/about-us they should be redirected "internally" to http://localhost:8084/testWeb/Controller?page=ForwardAbout

I have tried http://www.tuckey.org/urlrewrite/ so that when the user writes the url they should be forwarded to the right content, but doesnt seem to work, got a lot of exceptions in the classes.

Is there any way to achieve this straight through code, without touching apache mods?

PD: due to "teaching purposes" as teacher said, we arent allowed to use any frameworks...

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Alpha2k
  • 2,212
  • 7
  • 38
  • 65
  • All real MVC frameworks allow doing that. You simply map the front controller to a URL pattern (/, or *.html, for example), and then map every action to a specfic URL. Then, in the from controller, instead of using a parameter to know which action to call, you use the URL. Don't reinvent the wheel. Use a real MVC framework (like Spring MVC, for example) – JB Nizet Apr 27 '15 at 19:44
  • @JBNizet the lazy teacher told us not to use any frameworks xD, which makes the job even harder... – Alpha2k Apr 27 '15 at 19:48
  • Then do how I described. Don't use a parameter to know which action to call. Use the URL. – JB Nizet Apr 27 '15 at 19:49
  • @BalusC nope its not allowed but rather than reinventing the wheel... – Alpha2k Apr 27 '15 at 19:54
  • @BalusC shoudnt be... how can i call a servlet without parameters? for example localhost/myweb/about coz this page can contain imports that depends on the parameter... In my case im allways redirecting to request.getRequestDispatcher("/index.jsp").forward(request, response); and then the index includes pages where needed from the parameter – Alpha2k Apr 27 '15 at 20:05
  • @BalusC could this be achieved though javascript ? Would javascript be a viable solution ? – Alpha2k Apr 27 '15 at 20:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76387/discussion-between-alpha2k-and-balusc). – Alpha2k Apr 27 '15 at 20:16

0 Answers0