• Post Reply Bookmark Topic Watch Topic
  • New Topic

Avoiding duplicate code  RSS feed

 
Edward Nunez
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a scenario:

I have a log in page that may redirect the user to alternative log in pages depending on what happens. Perhaps the username or password is incorrect, there are a couple pages for that. Or maybe the user has been banned and cannot log in, there is a page for that as well. These alternative pages are essentially the same as the original log in page, save for an extra little error message. What's a good way to avoid duplicating the code that has the same function for all of these pages?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP has a couple different ways to include parts of a page: http://www.daniweb.com/web-development/jsp/threads/105695/difference-between-include-and-jspinclude. You can use those to factor out common parts. Or use tags and tag files.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Edward

What presentation technologies are you using?

You may find this article useful for JSPs:
http://www.javaworld.com/article/2075857/java-web-development/jsp-best-practices.html
 
Edward Nunez
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks James. I may be misinterpreting your question abut I'm using the Bootstrap framework.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I meant were you using JSF, JSP, Spring etc.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bootstrap is a JavaScript framework and has nothing at all to do with JSP.
 
Edward Nunez
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I'm just working on a dynamic web project in Eclipse with JSP's and servlets. I haven't touched Spring yet, can it help me do what I'm trying to accomplish?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, now I'm really confused. Can you re-ask your question with appropriate context? What are you using for technology? Bootstrap is a CSS and JavaScript framework for the client side. Spring is a Java family of libraries and frameworks for server-side code. We can't really answer your question until we know your environment.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, it sounds like what you are really after is a servlet filter -- code that executes for each request and is orthogonal to the actual purpose of the request. Not sure yet though...
 
Edward Nunez
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright. I've created a dynamic web project in Eclipse using TomCat server.

In my WebContent folder I have my JSP files as well as css, fonts, and js folders holding my Bootstrap files. Some of those JSP files are very similar to each other. For example, I have my login.jsp file which prompts the user for his username and password and a I have a wrongpassword.jsp file, which is a copy of the login.jsp file with an added error message. I think that all of the code that is shared between the files should actually be stored in one place but I'm not sure how to do it.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, it it's code that is supposed to execute prior to any request processing (like checking for login), then a filter is what you want.

If you have code that needs to be shared across multiple JSPs, then tag files or custom tags is the best bet. <c:import> might also be used, but is less preferred.

I assume you are not putting Java code in the JSPs, right? Right?
 
Edward Nunez
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haha, right Bear. Never have, never will. I'm trying to get my JSTL down so that I'm never tempted to.
Maybe if I had a tag that accepts a possible an error message String to change its code slightly...
OR maybe I could just have my login servlet set that String as an attribute and forward to the same login jsp. Hm, that solution seems pretty obvious to me now.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the advantages of the tag approaches is that they are easily (and explicitly) parameter-izable. Reduces strong binding.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edward
There is a very detailed answer (from BalusC) about JSP code reuse here:

http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files

(Apologies for promoting SO here...)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!