I have a query which I am unable to find the answer too. I have a small web app set up using the struts framework. Within this I have a number of jsp files for content.
I also have a number of servlets used to access the database.
My question is this: Is there a way to include a servlet in a jsp page i.e. have a jsp with all the html tags and stuff and make a call to a servlet which will then go access the database, retrieve it and then display it within the page
Or using struts and the tiles capability, can the servlet be embedded.
Hope someone can help as I've only been looking at struts and jsp/servlets for a week now so I still have a lot to learn
Sure, you could include the servlet with a JSP include action.
But... it's not a very standard or accepted thing to do.
If you're already working in Struts, then this is the type of thing that would normally take place in a Struts action prior to reaching the JSP page in the first place. Why are you waiting until you have forwarded to a presentation resource (the JSP) before trying to access the database?
This is generally done the other way around. The call is made to the servlet (controller). The Servlet instanciates a bean or some beans and passes the request parameters to them. The beans do whatever work in needed (talk to database, etc...). The servlet then bind the beans (or results from the beans) to a particular scope (request, session, context) and forwards to the JSP (view) for handling.
What you've described can be accomplished with the <jsp:include ... /> action.
[KLUNK] [ July 28, 2006: Message edited by: Ben Souther ]