Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

should all the pages of a web-app be in jsps or servlets

 
nagarjuna borra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should all the pages in a web app be in jsps or servlets or php or whatever else???

what goes wrong when i use static html pages when i dont need any dynamic presentation like in simple forms???

for an example:consider a home page of website is a simple login,a simple static html page can do that..why use any jsp page or servlet for that...

im worried abt that because most of my friends are doing same i.e writing every page in jsp.and when i asked them they replied that it should be done in that way.

are they wright...if so why???

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64712
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the page is truly static there should be no problem just having it an HTML page. But, and this is a big but, if the page has any links or references to external resources such as images, stylesheets or script file in it (and how many pages would not?), it should use server-relative addressing for the link and resource URLs. And that can only be done in a JSP as the context path needs to be dynamically determined with the following EL expression: ${pageContext.request.contextPath}

Note that at no time after 2002 should a JSP page contain any Java code or scriptlets.
 
nagarjuna borra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:If the page is truly static there should be no problem just having it an HTML page. But, and this is a big but, if the page has any links or references to external resources such as images, stylesheets or script file in it (and how many pages would not?), it should use server-relative addressing for the link and resource URLs. And that can only be done in a JSP as the context path needs to be dynamically determined with the following EL expression: ${pageContext.request.contextPath}

.


Thanks...finally a relief!!

Bear Bibeault wrote:Note that at no time after 2002 should a JSP page contain any Java code or scriptlets.


im aware of that...not the year though..

but,if seperation of control and presentation is not a big problem(i.e simple web-apps) can i still continue the pracice of writing java code/scriptlets in jsp pages???

what i mean is would there be any other problems??
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64712
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need whatsoever to put Java code in a JSP. Ever.

Anything that should be done in a JSP can be done with one of the existing JSP 2 mechanisms. If it can't, it's a big red flag that you are trying to do something in a JSP that should not be done in a JSP.

If your web app is properly structured, it's easy to do things that need to be done outside of the JSP in the page controller.
 
Paul Clapham
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nagarjuna borra wrote:but,if seperation of control and presentation is not a big problem(i.e simple web-apps) can i still continue the pracice of writing java code/scriptlets in jsp pages???

what i mean is would there be any other problems??


Well, yes, there would be a problem. One day you would come in to work and realize that your "simple web-app" is now a complicated and unmaintainable beast. That's the natural progression of software -- people can't just leave it alone, they keep having to add to it. So over time "untidy" turns into "messy" and then into "a steaming pile". And you'll always find that implementing new features takes precedence over cleaning up messiness.

So don't start with "untidy" and hopefully you won't end up with that steaming pile.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64712
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nagarjuna borra wrote:but,if seperation of control and presentation is not a big problem(i.e simple web-apps)

Paul said it very well, but just to re-emphasize: not properly separating presentation and control is always a big problem; even in "simple web apps".

Develop good coding habits and use them always.
 
nagarjuna borra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:There is no need whatsoever to put Java code in a JSP. Ever.

Anything that should be done in a JSP can be done with one of the existing JSP 2 mechanisms. If it can't, it's a big red flag that you are trying to do something in a JSP that should not be done in a JSP.

If your web app is properly structured, it's easy to do things that need to be done outside of the JSP in the page controller.


i didnt get that...JSP 2 mechanisms???,never even heard of it.

I did google it but couldnt get appropriate results

im a newbie.im just digging basics first.

anyways..if i want to retrieve data from database and present it to user...how would you do it(no offense meant,just want to know it)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64712
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP 2 introduced the EL and JSTL, simple custom tags, tag files, and so on.

JSP 1 style scriptlets, scriptlet expressions, and declarations should not be used in any JSP page created after the introduction of JSP 2 in 2002. That's 10 years ago. That also includes old mechanisms like<jsp:getProperty> and <jsp:setProperty> that have better JSTL equivalents.
 
nagarjuna borra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok,now i have some clarity.

Thanks to you people.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are other ways of creating an HTML view than JSP, though - libraries like FreeMarker and Apache Velocity can also be used.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64712
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True. Or Play! or any other number of templating frameworks. They're just a blip with hardly any penetration though.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic