aspose file tools*
The moose likes JSP and the fly likes Reducing redundant code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Reducing redundant code." Watch "Reducing redundant code." New topic
Author

Reducing redundant code.

Bharadwaj Adepu
Ranch Hand

Joined: Dec 30, 2007
Posts: 99
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

JSP1
-------------
line1
line2
xyz
abc
line5
pqr
line7

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.


SCJP 1.5
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

As far i know , in this scenario using a common jsp is again workaround
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41509
    
  53
You can use either of the possibilities discussed in http://faq.javaranch.com/java/IncludesActionDirective

Of course, JSP pages should not contain ANY code - that should be in servlets and backing beans. JSPs should be used as view templates only.


Ping & DNS - my free Android networking tools app
Bharadwaj Adepu
Ranch Hand

Joined: Dec 30, 2007
Posts: 99
Hi Seetharaman
As far i know , in this scenario using a common jsp is again workaround


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.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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?
Ramkumar Subburaj
Ranch Hand

Joined: Sep 07, 2007
Posts: 83
if the redundant code is java code, you can use tag handlers


SCJP 1.5, SCWCD 1.4.
Hanging between Web Services and EJB
Bharadwaj Adepu
Ranch Hand

Joined: Dec 30, 2007
Posts: 99
if the redundant code is java code, you can use tag handlers

Ram
Do you mean to say the JSTL Tags? Can you please elaborate!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41509
    
  53
Bharadwaj Adepu wrote:Do you mean to say the JSTL Tags?

No, JSTL is just one (standardized) tag library that you can -and should- use in your JSP pages. But in this case you could write your own tags: Tutorial on tag libraries

A still better solution would be to do what I mentioned before: keep the code in servlets and backing beans. The world has moved away from JSPs doing any kind of processing.
Vishwanathan Nagarajan
Ranch Hand

Joined: Jan 28, 2009
Posts: 37
Bharadwaj,

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.


MCP, SCJP 1.4, SCWCD 1.4, SCBCD 5.0, SCDJWS 1.4
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

Vishwanathan Nagarajan wrote:An even better solution would be for you to move to Struts framework
I would not recommend this.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vishwanathan Nagarajan
Ranch Hand

Joined: Jan 28, 2009
Posts: 37
Bear Bibeault,

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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

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.
Bharadwaj Adepu
Ranch Hand

Joined: Dec 30, 2007
Posts: 99
Hi All
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

That is something you should strive to avoid in the future.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reducing redundant code.