• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When to use a JSP page?

 
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question is why do we use JSP ?? Is the sole purpose of a JSP to accomodate the response from a servlet perhaps organize it into a presentable form , by applying MVC type pattern where the jsp page serves as the view a servlet as a controller and a plain old java object as a model ??
What other uses could their be for JSP pages (i have been told not to use scripts in JSP) ?? Secondly are JSP still being used ?? if not what has replaced them ??
I actually just finished Head First jsp and servlets though the book was pretty good but it would have been nice to read suggestions on when to use a jsp and when the use of a jsp should be discouraged. (well you cant cover everything in one book i guess). I'd Love to hear other opinions about JSP uses ??
So should I always keep this as a rule of thumb that even if a couple of items on a form need to be populated i should post the data to servlet which in return should send that data to a different jsp page (which exactly looks similar to the orignial page the user just had filled in) except that the items which required population were populated (looks like a long route)??? I was told that once i am finished with JSP and servlets i should move to a framework such as spring... but before that i suggest i should look at javascript and Ajax.... any sugeestions
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Zedan wrote:My question is why do we use JSP ?? Is the sole purpose of a JSP to accomodate the response from a servlet perhaps organize it into a presentable form , by applying MVC type pattern where the jsp page serves as the view a servlet as a controller and a plain old java object as a model ??


The role of modern JSP is to serve, as it was originally intended, as a template for text output, most often an HTMl page. Any other use of JSP is a perversion of its intention and a violation of best practices. Java code in a JSP went away with the dodo bird as of the introduction of JSP 2 in 2002 -- but as can be seen, many people still write JSP like it's 2001. For shame.

Prior to JSP 2, the purpose of allowing Java into JSPs was to allow conditionals and loops and dynamic expressions. As of JSP 2, the JSTL and EL fill those roles and Java is no longer needed or appropriate.

What other uses could their be for JSPS (i have been told not to use scripts in JSPS) ??


None.

Secondly are JSPS still being used ?? if not what has replaced them ??


Of course they're still being used. Some frameworks such as Grails and Play introduce their own proprietary JSP replacements, but they are not widely used. Even so, understanding JSP is a big help in understanding almost all modern templating systems.

I actually just finished Head First jsp and servlets though the book was pretty good but it would have been nice to read suggestions on when to use a jsp and when the use of a jsp should be discouraged. (well you cant cover everything in one book i guess). I'd Love to hear other opinions about JSP uses ??


JSP should be used to format the HTML that is to be sent to the browser. It's as bad an idea to build up HTML in strings inside servlets as it is to put Java code in a JSP.

So should I always keep this as a rule of thumb that even if a couple of items on a form need to be populated i should post the data to servlet


Yes. In a properly structured web app, it is almost never appropriate to post to a JSP directly. In fact, the JSPs are usually placed under the WEB-INF folder so that direct access is denied and can never happen accidentally.

which in return should send that data to a different jsp page (which exactly looks similar to the orignial page the user just had filled in)


You lost me here. Huh?

I was told that once i am finished with JSP and servlets i should move to a framework such as spring


I'm not a big fan of the frameworks, but they have their place. And if you want a job, well, you better know at least one of the big frameworks.

If you are going to study one, I recommend Spring MVC.

... but before that i suggest i should look at javascript and Ajax....


If you are also going to be working on the client side, HTML, CSS and JavaScript (with Ajax) is needed.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Must-read articles for JSP novices:
  • The Secret Life of JSPs
  • The Front Man
  •  
    Adam Zedan
    Ranch Hand
    Posts: 124
    C++ Fedora Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Bear Bibeault wrote:

    Adam Zedan wrote:
    which in return should send that data to a different jsp page (which exactly looks similar to the orignial page the user just had filled in)


    You lost me here. Huh?



    Thanks for all the great answers , here i was actually referring to a scenario let me explain ...
    suppose the user logs into a page say index.html which has like 5 text boxes and list box the user fills two text boxes and hits the submit button. The user now expects the remaining text boxes and list boxes to get populated according to his needs... So In the above post i was simply referrring to a scenario ...
    the user enters his data in the two textboxes and hits the submit button in index.html, this data goes to the servlet which processes it and then calls a JSP page which resembles the index.html only now all the field are populated. Is this the proper way to accomplish this task..I just think its an overkill to design a completely new jsp page similar to index.html jsut to get the field populated... ??
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    (I fixed your quoting in the previous reply -- please use Preview to make sure your post is correctly formatted)

    Adam Zedan wrote:and then calls a JSP page which resembles the index.html only now all the field are populated. Is this the proper way to accomplish this task ?


    Why would you need a JSP that resembles an HTML page when the JSP creates an HTML page in the first place?

    Hint: the above sentence has enough information for you to answer the question.
     
    Adam Zedan
    Ranch Hand
    Posts: 124
    C++ Fedora Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Bear Bibeault wrote:(I fixed your quoting in the previous reply -- please use Preview to make sure your post is correctly formatted)

    Adam Zedan wrote:and then calls a JSP page which resembles the index.html only now all the field are populated. Is this the proper way to accomplish this task ?


    Why would you need a JSP that resembles an HTML page when the JSP creates an HTML page in the first place?

    Hint: the above sentence has enough information for you to answer the question.




    when the JSP creates an HTML page in the first place?

    I really cant figure that out

    Ill instead answer Why would you need a JSP that resembles an HTML page

    Since my servlet received posted data from an HTML page and after it was done processing it . The servlet then sent the data to a JSP page using


    The jsp which looked similar to the original HTML page then used the data from servlet to fill in the missing fields which the user wanted.(giving the user the illusion that he never left that page and the remaining fields had been populated at the same time back end processing was also accomplished)

    When the user was on the html page and he clicked the submit button he expected to see a couple of fields get populated. rite ?? but to populate those fields back end server processing was required on the data which he did enter.. so eventually when the data was processed by the servlet it was sent to a jsp which looked exactly like the html file but this time all the required field were populated..(The jsp was a separate file which was created initially by copying the code from the main html page and then extending the code to populate all the fields) this is where i got the two almost identical files one html and other a jsp file... I hope i explained this properly.. Now is this the proper way to accomplish such a task ??



     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Adam Zedan wrote:Now is this the proper way to accomplish such a task ?


    Nope.

    Does it not strike you as completely wasteful and a maintenance nightmare to have a JSP and an HTML page that need to be maintained in sync?

    I'll ask a slightly different way: why do you need an HTML page that resembles the JSP?
     
    Adam Zedan
    Ranch Hand
    Posts: 124
    C++ Fedora Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Bear Bibeault wrote:

    Adam Zedan wrote:Now is this the proper way to accomplish such a task ?


    Nope.

    Does not strike you as completely wasteful and a maintenance nightmare to have a JSP and an HTML page that need to be maintained in sync?

    I'll ask a slightly different way: why do you need an HTML page that resembles the JSP?



    Honestly it does look like a maintainenece nightmare say if a field was added to the html i would also have to add it to the jsp.. and that would definitely be annoying.
    Are you suggesting that i get rid of the HTML and just keep a single JSP file.. The user enters his data on the jsp file and this data after processing from the servlet gets returned back to the same jsp which repopulates all the fields now ??
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Exactly. Why on earth would you think that you'd need to have an HTML version and a JSP version? It's all just HTML in the end.
     
    Adam Zedan
    Ranch Hand
    Posts: 124
    C++ Fedora Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Bear Bibeault wrote:Exactly. Why on earth would you think that you'd need to have an HTML version and a JSP version? It's all just HTML in the end.


    Thank you so much for clarifying this out.. I myself was a little freaked out and thought that there had to be a better way.
     
    Marshal
    Posts: 28193
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Right. And if you create a system with lots of JSPs, it quickly becomes clear that static HTML pages are a liability, and you should implement them as JSPs as well. Even though there's apparently no decisions being made in those JSPs -- yet.

    Even such a simple requirement as having a copyright notice with the current year at the bottom of each displayed page is a negative for HTML, for example.
     
    Adam Zedan
    Ranch Hand
    Posts: 124
    C++ Fedora Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Clapham wrote:Right. And if you create a system with lots of JSPs, it quickly becomes clear that static HTML pages are a liability, and you should implement them as JSPs as well. Even though there's apparently no decisions being made in those JSPs -- yet.

    Even such a simple requirement as having a copyright notice with the current year at the bottom of each displayed page is a negative for HTML, for example.



    Is it weird to not even have a single HTML in our website??
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Adam Zedan wrote:Is it weird to not even have a single HTML in our website?


    No.
     
    Adam Zedan
    Ranch Hand
    Posts: 124
    C++ Fedora Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Bear Bibeault wrote:

    Adam Zedan wrote:Is it weird to not even have a single HTML in our website?


    No.



    Thanks again....
     
    Paul Clapham
    Marshal
    Posts: 28193
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Adam Zedan wrote:Is it weird to not even have a single HTML in our website??



    We have some HTML pages in our web application. But a lot of our web application was written by people who didn't know any better (e.g. me several years ago), so I'm not holding it up as a paragon of web design. If we had it to do all over again, we wouldn't have any HTML pages.

    (And perhaps we will get to do it all over again -- it seems like the useful life of a web application is only a few years.)
     
    reply
      Bookmark Topic Watch Topic
    • New Topic