I’ve been fortunate enough to inherit a project which uses a “platform” that only allows JSP. The platform is basically tomcat and we get our own directory where we place our JSPs and JARs. There’s no servlet support for us (we don’t have access to a web.xml) and even JSTL isn’t supported. I tried adding the JSTL jars and <c:out value="blah" /> works, but expressions don’t work. Using <c:set var="test" value="blah" /> isn’t helpful since I cannot access the var using EL (<c:out value="${test}" /> just prints “${test}”).
The project currently consists of only JSPs with Java expressions throughout and it’s quite a mess. I’m trying to clean it up by moving the Java code to classes, but am wondering if there’s some easy way to mimic the servlet/MVC behavior.
All I can think of right now is to have the following. Does this seem like the best solution? I’m used to using Spring or at least servlets…
- HTML form clicked
- JSP file (call Java Controller)
- Controller (handle content, redirect/forward to view)
- JSP view
somePage.jsp
<form action="control/someController.jsp" method="post">
<input type="submit" />
</form>
someController.jsp
<%
SomeController sc = new SomeController(request, response);
sc.doSubmit();
%>
SomeController.java
public class SomeController extends AbstractController {
public SomeController(HttpServletRequest request,
HttpServletResponse response) {
super(request, response);
}
protected void handleSubmit() {
// do stuff ...
redirect("nextPage.jsp");
}
}
This is doable. But the recreation of the classes on every request is pretty expensive. I would also just extend them from
HttpServletthe usual way. I’d create a factory class which loads all servlets statically. You’re then basically taking over the job the servletcontainer is doing based onweb.xml.This way you can implement servlets the usual way by creating classes which extends
HttpServlet.Let your forms submit to a generic JSP
where the
controller.jsplook like this(note that you should not have any whitespace beyond the
<% %>in thiscontroller.jsp, also no newlines, otherwise the response may be committed and your servlets would be unable to forward/redirect the request/response)Or when your servletcontainer doesn’t extract the parameters from the query string in case of POST, instead use
Finally, this way the app is more easier to transform whenever the
web.xmlis available for use. You just have to create theweb.xmland change the form URLs.