aspose file tools*
The moose likes JSP and the fly likes How to display questions in several pages? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to display questions in several pages?" Watch "How to display questions in several pages?" New topic
Author

How to display questions in several pages?

Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Hi,

I'm trying to do a sample survey application,which has got a few questions.I have the questions stored in the database.My current JSP displays all the questions in the same page,rather i would like to have a single question in a page and a next button to go to the next question.How do i do this?

Regards,
Srikkanth.M
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

You're going to need to be more specific about what's confusing you. I mean, it's easy to create as many pages as you want each with one question. What's the confusion?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Hi,
How do i display a single question in a page?
If i click on next i should get the next question,so where should i redirect?

And where should i save the user's responses? (I think i should save it in session attributes)
Currently i have all the questions with options in the same page..but that does'nt look right( 30 questions in a same page)

Thanks,
Srikkanth
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

Originally posted by Srikkanth Mohanasundaram:
Hi,
How do i display a single question in a page?
Don't put more than one on a page.

If i click on next i should get the next question,so where should i redirect?
To the page with the next question.

And where should i save the user's responses?
Wherever you want. The session? The database? it depends how you want to use the answers.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

There are many ways to do "pagination". Do a search on google or this forum to get some good results


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

Originally posted by John Meyers:
There are many ways to do "pagination". Do a search on google or this forum to get some good results


That will yield hundreds of thousands of results all pertaining to pagination of database records. Which is not what I think that the OP is asking about.

My confusion is still: why is the OP confused as to how not to put more on the page than intended?
[ April 30, 2007: Message edited by: Bear Bibeault ]
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Originally posted by Bear Bibeault:


That will yield hundreds of thousands of results all pertaining to pagination of database records. Which is not what I think that the OP is asking about.

My confusion is still: why is the OP confused as to how not to put more on the page than intended?

[ April 30, 2007: Message edited by: Bear Bibeault ]


Srikkanth are you looking to put more than one question on a page (say 5) or just one question on a page ? Either way it should be simple. If its just one question you want to put on the page can you be more specific about what it is that you want to know ? Reducing the view to just one question from 30 should be pretty simple to do. The "next" button can fetch the next question from the database based on session attributes or parameters. You can save the results however you like, like Bear suggested.
Suman Sharma
Ranch Hand

Joined: May 16, 2005
Posts: 72
I think you can use the following steps:

1) Get all the questions from the database and save each question to a different instance of the same JavaBean object. As you do it, keep adding each JavaBean object to a vector object.
2) Save the vector object in the HttpSession object.
3) In each JSP page, access only one JavaBean object from the vector from the session object and retrive the data.
4) Go to next JSP page and repeat the step 3 and so on.

I hope it works.
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Hi everybody,
I currently use the resultset from the database to create Simple Javabean question objects.And i populate a list with these question objects.
From my servlet i set this list as a request attribute and access the list of question objects in the JSP.

Suppose i have 30 question objects in my List.. do i need to have 30 JSP pages?
What should be the "target" of the next button? Can i send it to the same JSP, if so how should the list be accessed so that i don't repeat the questions?(Should i delete the question object from the list once i have displayed it,Is that the way to go about it?)

Thanks for your replies,

Srikkanth.M

Regards
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

OK, now that you've added a little more substance to your question we can get to work!

Originally posted by Srikkanth Mohanasundaram:
Hi everybody,
Suppose i have 30 question objects in my List.. do i need to have 30 JSP pages?


Of course not. That's exactly what the dynamic nature of JSP is for. A single page should do.

What should be the "target" of the next button? Can i send it to the same JSP, if so how should the list be accessed so that i don't repeat the questions?


As was mentioned earlier, storing the list in the session will let you access the list in each request. You can also maintain a counter in the session that identifies the "current question". A next operation merely incrementes the count and accesses the next question. This would also allow you to imlement a back operation easily, if desired.

(Should i delete the question object from the list once i have displayed it,Is that the way to go about it?)


No, it's not ncessary and will just introduce the possibility of problems.
[ April 30, 2007: Message edited by: Bear Bibeault ]
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Hi Bear,

Thanks for your reply I'll try out that logic and get back to you.

Regards,
Srikkanth.M
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Hi Bear,

Now i have one question per page,but how do i save the user's responses?

I mean how to save the user's answers.
Do i have to do the processing in the JSP?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

Originally posted by Srikkanth Mohanasundaram:

Do i have to do the processing in the JSP?


No. In fact, it's considered a poor practice to perform processing in a JSP.

Rather, your JSP should submit to a servlet that does the processing (in this case, saving the answers in the DB or to file or whatever) and then to forward to the next JSP.

This article might be helpful in understanding the flow patterns.
[ May 01, 2007: Message edited by: Bear Bibeault ]
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Bear,
That article was helpful.

Regards,
Srikkanth.M
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Bear,
I often end up using scriptlets in my page.I have two attributes set in my Session(one is the counter and other is the actual list)
What i wanted is:

The EL expression didnt work and i had to use something like this

I know this isn't the right way to go about it...

Regards,
Srikkanth.M
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

list.size will not work in the EL since size is not a proper bean-patterned property of the collection classes. Assuming that you are using JSP 2.0 and JSTL 1.1, you should use the fn:length() function to obtain the size of a collection.
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Wow that's cool.
Then it becomes simple to get the size and compare using EL.

Bear thanks for your valuable suggestion.

Regards,
Srikkanth.M
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

Originally posted by Srikkanth Mohanasundaram:
Then it becomes simple to get the size and compare using EL.
Exactly. The test becomes:



(don't forget proper quoting).
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Bear,

How do i eliminate the scriplet from this piece of code?


li is a List(session attribute) that contains Question Objects and counter is a session attribute.
Regards,
Srikkanth.M
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

"li"? Not very readable. Let's call it questions.



Now is the time for you to make sure that you have copies of the JSP Spec (as an EL reference) and a copy of the JSTL Spec.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

The only thing that puzzles me is the cast to Person. What's up with that?
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Bear,
Sorry that was a typo
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

Be careful with that. Typos like that can lead to a lot of wasted time. Please read this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to display questions in several pages?