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...