Forum Moderators: phranque
(im new to webmasterworld so please let me know if this post is on the wrong forum.)
i am developing a website using both jsp and servlets. my current design uses an "index.jsp" to include various other pages (ie. banner, content, etc). this has become increasingly complex, however, when passing many parameters between jsp pages, so i would like to map the main "index" to a servlet instead.
my question is how i might best "include" my jsp pages as "templates" for the page generated by the servlet (preferrably allowing some sort of textual replacement across the template page). i want to avoid having to just copy the entire jsp file into the servlet as out.println() statements. i hear that something like this can be done with disney's "tea" as well as jakarta's "velocity", but i have no experience with either. i am currently using tomcat as my jsp/servlet container (if that has any relevance).
any suggestions or recommendations would be greatly appreciated.
much thanks, emory
Your post is definitely in the right board for now as we don't have a specific JSP related board. Unfortunately, I'm the closest thing we have to a JSP "expert" here and I'm really and truly no expert on the subject.
Here's what little I know.
A servlet is just a compiled java class that will display in a web browser, written mostly in Java with lots of out.println() commands. a JSP is an HTML page with bits (or lots) or java code that gets compiled into a servlet, which is then displayed.
To map a servlet, (help me if I'm wrong) is similar to mapping a virtual directory with the servlet class as the default page in the directory.
Therefore, it seems to me that you can use include commands just as you would in a JSP page (with the proper syntax of course.)
But I'm just grabbing here. I never use servlets.
Please let us know what you find out.
Thanks.
RequestDispatcher dispatcher =getServletContext().getRequestDispatcher("/path/resource");
dispatcher.include(request, response);
Note: Unlike the JSP include directive (<%@ include file="file1.jsp" %>) the include method of RequestDispatcher just includes the result of the specified resource.
I don't know if you are changing from a jsp to servlet because you want to use the servlet as a "controller" eg to determine where the user goes next or to check he/she has appropriate permission. If so it is normal to do so and you can register it in the web.xml file of the application.
eg
<servlet>
<servlet-name>ControlServlet</servlet-name>
<display-name>ControlServlet</display-name>
<description>Main Control Servlet</description>
<servlet-class>com.test.control.ControlServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ControlServlet</servlet-name>
<url-pattern>/control/*</url-pattern>
</servlet-mapping>
However if you are duplicating a lot of the jsp/html code (ie the view/visual code) and want to switch to a servlet to control this, I am not so sure. Generally within the MVC (model view controller) architecture, servlets are not used to render the view. JSPS are used instead. There are many ways to keep tabs on this, using templates, xml etc. An easy way to reuse the same code, is to have jsp 'templates' in every page and use the JSP include directive. eg
<%@ include file="header.jsp" %>
<%@ include file="lookAndFeel.jsp" %>
Unique content here
<%@ include file="footer.jsp" %>
and you can embed the common html into each of these common jsps. (There are better ways of doing this I know - this is just one method).(Note if you use the above and change any of the included files you have to update/save the files which include them to see the changes).
Jsps mean that the html coder can work on the appearance while the java coder only has to worry about the logic (assuming more than one person is working on them). If you do as you suggest ( ie using out.println) it will be a nightmare to maintain (having to recompile every time you change a little bit of html) and it will not be so easy to prototype or divide the tasks.
Anyway it all depends on what you want to do and why, but for maintenance sake the out.println way would not be my first choice. Unfortunately I have never used disney's "tea" or jakarta's "velocity" so I cannot comment on these.
Hope that helps (apologies if I have misconstrued your first post) and please tell us how you get on.
Kind Regards and Good Luck
PBG