aspose file tools*
The moose likes Servlets and the fly likes When to use a JSP page? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "When to use a JSP page?" Watch "When to use a JSP page?" New topic
Author

When to use a JSP page?

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

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

Don’t look where you fall, but where you slipped
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Must-read articles for JSP novices:
  • The Secret Life of JSPs
  • The Front Man
  • Adam Zedan
    Ranch Hand

    Joined: Jun 10, 2011
    Posts: 124

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

    Joined: Jan 10, 2002
    Posts: 61413
        
      67

    (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

    Joined: Jun 10, 2011
    Posts: 124

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

    Joined: Jan 10, 2002
    Posts: 61413
        
      67

    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

    Joined: Jun 10, 2011
    Posts: 124

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

    Joined: Jan 10, 2002
    Posts: 61413
        
      67

    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

    Joined: Jun 10, 2011
    Posts: 124

    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.
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18650
        
        8

    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

    Joined: Jun 10, 2011
    Posts: 124

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

    Joined: Jan 10, 2002
    Posts: 61413
        
      67

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

    No.
    Adam Zedan
    Ranch Hand

    Joined: Jun 10, 2011
    Posts: 124

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

    No.


    Thanks again....
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18650
        
        8

    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.)
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: When to use a JSP page?