Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to avoid large code blocks inside scriplets written in a JSP?  RSS feed

 
Ferdinand Victorinus
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've already seen large code blocks inside JSP's, in a way it is very hard to maintain the web application. Is there a way to avoid that?
 
Amar Saikia
Ranch Hand
Posts: 43
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've already seen large code blocks inside JSP's


That means your JSP contains lots of business code(logic) which is not a good practice. In an ideal world, JSP need to be used only for presentation. So, IMO, you can move those logic to your serverside code .
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write that code in classes that can be tested outside the servlet / jsp environment.

So many things can go wrong in the full client browser - web server environment that you should test as much as possible in a simpler environment.

Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be avoiding putting any Java code in a JSP.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ferdinand Victorinus wrote:I've already seen large code blocks inside JSP's, in a way it is very hard to maintain the web application.

Yes, it's hard to read, hard to maintain, impossible to test, and can't be abstracted or reused. It's a terrible way to write code and yet it continues to this day.

Read this article to start learning the proper way to structure a web application.
 
Sooraj Rajagopalan
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ferdinand Victorinus wrote:I've already seen large code blocks inside JSP's, in a way it is very hard to maintain the web application. Is there a way to avoid that?


Not only is it very hard to manage, but also a very poor and outdated practice, outdated about 8 years ago. The best way to avoid it is by following a MVC (Model View Controller) architecture to begin with.
the following are the advantages of using the MVC arch.

  • advantages
  • the logic stays hidden from the user
  • processing and logic solving will be done at the server side
  • keeping the client side complications to the minimum will reduce the incompatibility issues to the minimum.


  • Learn to use get and setParameters or get and setAttributes. Also learn to use EJB (enterprise java beans). Also implement JSTL Tags. This a cleaner method of writing jsp pages.
    hope this helps.
     
    Ulf Dittmer
    Rancher
    Posts: 42972
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sooraj Rajagopalan wrote:the following are the advantages of using the MVC arch.

  • advantages
  • the logic stays hidden from the user
  • processing and logic solving will be done at the server side
  • keeping the client side complications to the minimum will reduce the incompatibility issues to the minimum.


  • None of these have anything to do with MVC, and none are relevant in the scriptlets-pros-and-cons debate; what point are you trying to make?
     
    Sooraj Rajagopalan
    Ranch Hand
    Posts: 40
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I guess we all can agree that using scriplets in jsp pages is outdated and not a very good idea. So logic should stay at the server and the same is achieved by using Servlets and Busines layers and DAOs. Where the user only has to deal only with the view. And that being my point.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66205
    151
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sooraj Rajagopalan wrote:I guess we all can agree that using scriplets in jsp pages is outdated and not a very good idea.

    Yes, we can all agree on that.

    So logic should stay at the server

    But this is misleading. JSPs are evaluated on the server, so this is not a factor.
     
    Ulf Dittmer
    Rancher
    Posts: 42972
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So logic should stay at the server and the same is achieved by using Servlets

    JSPs are a server-side technology just like servlets, so that's not a differentiating factor.
     
    Sooraj Rajagopalan
    Ranch Hand
    Posts: 40
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i guess i should probably start from Square one all over again..
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66205
    151
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Naw, your point about scriptlets being obsolete is spot on. The rest is just details.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!