tags:

views:

36

answers:

2

If I have a servlet I am able to forward to a jsp in the WebContent folder with no issues:

request.getRequestDispatcher("page.jsp").forward(request, response);

request being an HttpServletRequest and response being an HttpServletResponse.

Now for the question: What if I want to use package by feature? That is, move page.jsp into the same package as my servlet class so that all the files for one "feature" are in the same place. Is this possible?

A: 

JavaEE containers will not serve resources out of a jar. If you wanted to package by feature you'd have to pre-compile your JSPs and place them in the same package as other servlets for your feature. By supposing you did that, you wouldn't be able to serve any static resources from within the jar such as images, css or javascript.

If you really wanted to get fancy I suppose you could write a general purpose servlet that would serve resources from a jar. But I imagine that would get fairly complicated fairly quickly for little gain.

Bryan Kyle
So how does Web4J pull it off? Looking at the classes directories of the sample applications you can see JSP files alongside class files.
npsken
@KPthunder: The `classes` directories you mention are not in a JAR, they're under `WEB-INF`.
skaffman
I'm confused now and was initially confused by your answer. I was never intending to serve anything out of a jar file...
npsken
+2  A: 

If you have a servlet class 'MyServlet 'in a package foo, then it would be available in WEB-INF/classes/foo/MyServlet. Now if you want to have JSP in the same package, you need to have the JSP in the folder /WEB-INF/classes/foo/page.jsp. Then you can forward to the JSP like,

request.getRequestDispatcher("/WEB-INF/classes/foo/page.jsp").forward(request, response); 
Marimuthu Madasamy
Thanks! That worked like a charm.
npsken
Needless to say that this is not the best practice. In a bit decent webapplication you also don't have one servlet per JSP, but one servlet per whole webapplication (front controller).
BalusC