• Post Reply Bookmark Topic Watch Topic
  • New Topic

Paging using servlet and JSP.  RSS feed

 
Phong Thanh Vu
Greenhorn
Posts: 9
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to paging my product list into several pages. Each page contains 12 records. I used servlet to retrieve data from jsp file and JSTL to display data in JSP file. But my jsp file cant display anything. The main problem seems to be with "productList" not being available to the jsp
This is my servlet.



This is my productlist.jsp file.



And finally, my web.xml



I debugged my method getAllPaging() and it works perfectly, I checked and rechecked my jsp with JSTL syntax. It's ok. So I think the problem belongs to my servlet but I cant figure out what it is. Thank you for your help.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and welcome to Coderanch

Two things, firstly you are generating a response for the JSP. Then why set the productList in request object?
Second, the JSP (implicit) objects are scoped. You should be using the right scope (request/response/page,etc).

 
Phong Thanh Vu
Greenhorn
Posts: 9
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Ghorpade wrote:Hi and welcome to Coderanch

Two things, firstly you are generating a response for the JSP. Then why set the productList in request object?
Second, the JSP (implicit) objects are scoped. You should be using the right scope (request/response/page,etc).


Hi, thank you for being kind.
Firstly, I know I have to set response to the jsp file after invoking the servlet. But as I know I cant use response.setAttribute or I didnt know how to use it.
Secondly, what you mean is I should use request.getParameter method in jsp file to get the parameter currentPage, noOfPages and productList?
Sorry I'm pretty newbie.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest that you create a class that holds all that data you are passing to your page so that you only set one attribute (they are all related if you think about it).
Then as to your problem, does the productlist JSP display some parts except for the data part or is the whole page failing to display? Are you able to access any another attributes you are sending apart from the productList?
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a similar note to E Armitage's post, could you post the HTML produced for the product table?

One question (and it could be just me being blind), but the list you get seems to be the records for the current page.
So noOfRows is the number of rows on a single page.
Therefore, what does noOfPages represent, as it is noOfRows / rowsPerPage.
 
Phong Thanh Vu
Greenhorn
Posts: 9
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:I would suggest that you create a class that holds all that data you are passing to your page so that you only set one attribute (they are all related if you think about it).
Then as to your problem, does the productlist JSP display some parts except for the data part or is the whole page failing to display? Are you able to access any another attributes you are sending apart from the productList?

Yes the jsp file display some parts except the data part. I put these code into a template and as you can see here . Only the paging part and data part cannot be retrieved. I cant access anything that is sent by the servlet ( the variable page and noOfPages ).
 
Phong Thanh Vu
Greenhorn
Posts: 9
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:On a similar note to E Armitage's post, could you post the HTML produced for the product table?

One question (and it could be just me being blind), but the list you get seems to be the records for the current page.
So noOfRows is the number of rows on a single page.
Therefore, what does noOfPages represent, as it is noOfRows / rowsPerPage.

Yeah I feel really dumb when I realized I dont need noOfRows because I only need 12 rows per page. I want to represent the navigation button like this . That's why I use the variable noOfPages.
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what does getAllPaging return?

Have you debugged the servlet by logging the number of Products returned?

You could also print the values directly to the page to see what's actually being passed into the page.
 
Phong Thanh Vu
Greenhorn
Posts: 9
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:So what does getAllPaging return?

Have you debugged the servlet by logging the number of Products returned?

You could also print the values directly to the page to see what's actually being passed into the page.


The method getAllPaging return an object product list. Like this



This is how I print out directly the values.


The toString method is declared in ProductGUI


What I really want to achieve is displaying 12 records per page and my page has navigation button like (first, previous , current page, next, last).
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And does that all display on the JSP page?
If you do:

on that JSP page do you see a count? Or do you get an error?
 
Phong Thanh Vu
Greenhorn
Posts: 9
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I just tried another way without using servlet and it works perfectly fine. So thank you everyone who tried to help me. Peace
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"without using servlet"?

I hope that doesn't mean you stuck Java code in your JSP...
 
Phong Thanh Vu
Greenhorn
Posts: 9
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:"without using servlet"?

I hope that doesn't mean you stuck Java code in your JSP...

I think I get stuck because of using servlet. My servlet cant pass variable or list object into my jsp. I tried writing my code like this and it works.



This is not exactly the same I want but at this moment my deadline is coming I have no choice. I spend my time for this problem too long and I cant afford time anymore.
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However you have now taken a step back.
Your previous structure was correct, with a call to a servlet to get data that forwards to a JSP to display that data.
That is the way a JEE app is supposed to work.

What you have now has never been considered good practice for JSPs.
 
Phong Thanh Vu
Greenhorn
Posts: 9
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:However you have now taken a step back.
Your previous structure was correct, with a call to a servlet to get data that forwards to a JSP to display that data.
That is the way a JEE app is supposed to work.

What you have now has never been considered good practice for JSPs.

Thank you for keeping discussing with me, Dave. Finally, I figure out why my code doesnt work. The reason is I declare wrong get and set method in bean. I'm such a disgrace of my family (j/k).
Instead of getPrice() I wrote get_Price(), that's why my code doesnt work.

Thank you everyone who tried to help me.
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for posting the fix.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!