JSP only application possibilities
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 st开发者_运维知识库uff ...
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 HttpServlet
the 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 on web.xml
.
public final class ServletManager {
private static final Map<String, HttpServlet> servlets = new HashMap<String, HttpServlet>();
static {
servlets.put("controller1", new Controller1());
servlets.put("controller2", new Controller2());
servlets.put("controller3", new Controller3());
// ...
}
public static HttpServlet get(String name) {
return servlets.get(name);
}
}
This way you can implement servlets the usual way by creating classes which extends HttpServlet
.
Let your forms submit to a generic JSP
<form action="controller.jsp?controller=controller1" method="post">
...
<form action="controller.jsp?controller=controller2" method="post">
...
where the controller.jsp
look like this
<%
HttpServlet servlet = ServletManager.get(request.getParameter("controller"));
if (servlet != null) {
servlet.service(request, response);
} else {
// Handle non existing controller.
}
%>
(note that you should not have any whitespace beyond the <% %>
in this controller.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
<input type="hidden" name="controller" value="controller1" />
Finally, this way the app is more easier to transform whenever the web.xml
is available for use. You just have to create the web.xml
and change the form URLs.
Can you add
<%@ page isELIgnored="false" %>
to the top of your JSP to see whether EL is working or not?
The absence of this page directive is probably why you get the following
(<c:out value="${test}" /> just prints "${test}")
.
The EL gets evaluated as text if isELIgnored is true.
精彩评论