I have a Problem.
I have 10 JSP pages, and few lines of code repeats in all JSPs and i want to keep this common code in a common JSP and call this JSP in all the previous JSPs, is this possible??
The situations is, the code repeats at different places, for example
This is the way the code is. xyz, abc, pqr are the lines that are repeating in all the JSPs and my concern is, if the code repeats at a particular position then we can keep that code in the common JSP and call that JSP at that position but here code is repeating in different places so is this possible to keep the repeating code in a common JSP and call that in different places? Please Explain if there is a way.
Bharadwaj Adepu wrote:
How come this would be a workaround again? Here we'll include the common JSP and replacing around 25 lines of code, that too in all 10 to 15 JSPs.
suppose around 25 lines of code lies continuesly then you can use common jsp . As you mentioned above if code breakes like here and there then using common jsp (passing some html text from one jsp to another) is looks bad. is n't it?
As you have said you wanted to move some common repeating JSP code logic to a seperate JSP and call this new JSP in all other places, it is not just the view logic that you have in JSP, you will have business logic that is hard bound to your JSP. So even if you move this logic to a seperate JSP there is no way to reuse this business logic. There would be scenarios wherein it would be better off if you encapsulate the view form the business logic so that there would be more reusability of the business logic when it is commonly coded.
Ofcourse as said by others, you could cretae your own tag libraries or move the logic to a seperate Servlet.
An even better solution would be for you to move to Struts framework, wherein you would have better control of this scenario, but again it all depends on the timeline you are looking at to acheive this. It is always better to go for a framework that would address your problem, wherein you could get more than what you ask for, as these frameworks are designed by people who had already faced similar kind of issues.
Could you explain why Struts would not solve this issue.
I mentioned Struts coz not only to solve the current problem what Bharadwaj has mentioned, but it would be of more help on a long run wherein the application would grow up to be off 100 or more JSP's, wherein as I mentioned, going for Struts would be fruitful.
As sugested by others, in my past experience as a programmer, I have worked in custom tag libraries, Servlets and Struts as well. It all pivots around the same, MVC.
But, I rather found Struts to be of more use as it had much clear demarcation of the layers.
My dislike of Struts is well-documented in these forums.
In my opinion, adopting a cantankerous behemoth like Struts just to address an issue easily solved by some minor code refactoring or the use of custom tags is like using atomic bombs to crack walnuts.
Joined: Dec 30, 2007
Many thanks for replying to my problem.
Here there is one more thing that i have to mention, the architecture we are using is not MVC, from the JSP itself we are calling the Bean using <jsp:useBean /> , there is no controller like servlets.