This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Developing a Page Controller  RSS feed

 
Rajesh Khan
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , I was a bit curious about the page controller.
According to the article at http://www.javaranch.com/journal/200603/Journal200603.jsp#a5
it states

"a page can act as its own controller (which puts us squarely back into Model 1 territory), but more often the Page Controller is a servlet that is invoked as a result of the POST or GET action. This servlet performs any setup or processing necessary to prepare the page for viewing prior to forwarding the request to the JSP page template for view rendering. When such processing involves business logic or database interaction, frequently such processing is delegated to other classes. "


Now from reading the above passage i understand that a JSP page should not do a POST or GET to another JSP page ,but instead it should send it to a servlet which acts as a page controller ,and processes the information after which it forwards the user to a specific jsp page. Thus enforcing the concept of "using JSP for displaying only"
Here I understand the need of a servlet acting as a page controller. However I would like to know can a jsp page call another jsp page directly ? If not how would you implement a page controller in such a scenario ??
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure a JSP can submit to another JSP, but it's considered a poor practice. Doing so means that:
  • Any processing of the submitted data must be performed by the JSP (which is not an appropriate view responsibility)
  • and the set up for the JSP has to be performed by the JSP (same problem)

  • By submitting to a task controller that processes the submitted data, then redirecting to a page controller (see P-R-G pattern) JSPs are freed to only perform view activities while the two types of controllers handle the non-view and preparatory stuff.

    In fact, it's customary to place the JSPs under a folder in WEB-INF so that they can't be directly addressed.
     
    Rajesh Khan
    Ranch Hand
    Posts: 230
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Okay I understand the basics of the post redirect get pattern(PRG). And I understand that The browser should never call a JSP directly.
    Now suppose a JSP page or HTML page that does not contain a form or any information really worth processing needs to call a jsp page directly ,the normal thing to do in this scenario is to insert <a href="link_to_jsp"> page. But suppose we adopt the page controller method for this scenario. How would we implement that? Will we replace the <a href="link_to_jsp"> link with <a href="link_to_a_pageController_servlet"> link ??? IF so then how would the servlet know which page was the link actually aiming for ? will we also have to write the actual link in the request object so that the page controller servlet can read that link and forward us to the required jsp.



     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66203
    151
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rajesh Khan wrote:Will we replace the <a href="link_to_jsp"> link with <a href="link_to_a_pageController_servlet"> link ?

    Yes. If the job of the page controller is to prepare whatever data the page needs, then it makes no sense to go directly to the JSP. does it?

    IF so then how would the servlet know which page was the link actually aiming for ?

    Each page controller prepares one page. So each page has its own controller. Under rare circumstances, a page controller might service two or more pages (for example. two pages that display the same data but in different ways) but that's not usual.

    And don't get confused about the difference between controllers. Task controllers processes submitted data, and page controllers prepare a page for display. They live on different sides of the PRG pattern.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!