File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to display data from a servlet to 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 "How to display data from a servlet to a jsp page" Watch "How to display data from a servlet to a jsp page" New topic
Author

How to display data from a servlet to a jsp page

yash_mca
Greenhorn

Joined: Jan 03, 2005
Posts: 15
I have mada a servlet which takes a query from one jsp page and process the data.But now i want to display the data to the jsp page.

When i write the code which i use to display the data to a jsp page it does not work

res.sendRedirect("localhost:8080/library/jsppages/Hello1.jsp");

thanking you

in advance
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Read up on the requestDispatcher.forward method and the HttpServletRequest object.

If you want to see these working in an example app, got to http://simple.souther.us and download SimpleMVC.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

"Yash"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ravi M S
Greenhorn

Joined: Jan 14, 2005
Posts: 1
Hi,

You can put that data which you want to display into session scope. Then you make use of that session variable to display in JSP.


Bye
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
using request scope is far preferred for data that doesn't need to be persisted between requests.
Saves a lot of potentially unwanted sideeffects if you (accidentally or on purpose) use the same attribute name in different servlets and JSPs (like I do, as I have the core of a whole range of servlets in an abstract baseclass which sets quite a few things for all the derived classes (in fact it sets just about everything).


42
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

"yashmca"

That still does not match the naming policy.

The Naming Policy requires a real first and last name seperated by a space.

Mark
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Your redirect approach is used in what I call the "Post, Redirect, Get" pattern.

This has some neat advantages over the servlet forwarding to the JSP

  • The user can refresh the display page without that annoying message about reposting a request
  • If the POST does updates, posting again may be dead wrong
  • The user can bookmark the display page
  • Nice separation of concerns - update code and display code. You get that already with servlet & jsp but this is stronger yet.


  • A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    David O'Meara
    Rancher

    Joined: Mar 06, 2001
    Posts: 13459

    Originally posted by Stan James:
    Your redirect approach is used in what I call the "Post, Redirect, Get" pattern.


    I use this almost constantly. I found that building sites around this behaviour takes a little more organisation, but many HTTP and browser problems disappear, and the page responsibilities are cleaner.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61094
        
      66

    Your redirect approach is used in what I call the "Post, Redirect, Get" pattern.


    I also use a similar pattern which differs just slightly:

    Browser POSTs an input form to the server
    The server updates resources accordingly
    The server redirects to a controller (display support) servlet
    The browser GETs the controller servlet
    The server reads the resources needed to display the supported page
    The servlet forwards to the page
    The server returns the display page

    All server 'hits' trigger a controller servlet -- each page has such a controller servlet to marshal any data that the page needs in order to render. The JSPs themselves are scriptless and use only the JSTL, EL and custom tags.

    The point of these patterns is, of course, to not leave the POST that performed an operation 'dangling" on the browser. And David's point is a good one: this makes for a nice "orderly" application.
    [ January 14, 2005: Message edited by: Bear Bibeault ]

    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    The drawback to this pattern is that you loose the first request.

    How do you generally persist your data between requests?
    Do you always store in session or do you have to make an extra trip to the database?

    Also, I've had the misfortune of having to deal with firewalls that don't allow redirects, but not recently.
    [ January 15, 2005: Message edited by: Ben Souther ]
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    Bear's extension with a controller is good. I was thinking of how my Wiki works and it's not quite like servlet / JSP.

    After doing the update, the redirect has to contain enough key information to retrieve the right data. The POST for the update might contain a ton of data, but the redirect & get might only need a primary key as a URL parameter.

    Oh, reading again that wasn't exactly your question. Yes, database storage has been my experience ... I've mostly done this when the POST will update something persistent.

    BTW: Is "post, redirect, get" a common name for this? Is it like a Sun pattern or anything? I saw it in a magazine I think and have used it heavily ever since.
    [ January 15, 2005: Message edited by: Stan James ]
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61094
        
      66

    Good question Ben.

    I do not apply this pattern to each and every operation. It is mostly applied to operations where, in order to display the results, a new trip to the server is required regardless of whether a forward or rediect would be used, or cases where 'replaying' the operation via a refresh would have detrimental consequences.There seems to be no physical force in the universe stronger than the urge for a user to click on that blasted refresh button.

    An example that pops to my head from something I was working on the other day: a page shows a summary list of records from the db. The list is paged and allows the user to select records for deletion. After clicking the Delete button and executing the delete on the back end, a new database fetch for the page is required to get the list of records for the current 'page'. So the fact that a redirect occured to 'clear' the delete post off the page introduces only minimal operational overhead.
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    There seems to be no physical force in the universe stronger than the urge for a user to click on that blasted refresh button.
    That's where you are wrong. The urge to click the back button is much stronger..

    I use this pattern myself when logging people in and out to prevent someone else from backing up and refreshing to log themselves into the last person's session.

    Thanks.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61094
        
      66

    That's where you are wrong. The urge to click the back button is much stronger..


    You're right! I stand corrected!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to display data from a servlet to a jsp page