Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Pagination in Java is not working properly

 
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Here is my code:


Here is output:



Perhaps image links here not working. So I am copy pasting
https://pasteboard.co/K59yqqG.png
https://pasteboard.co/K59yP9j.png
https://pasteboard.co/K59z4Bu.png

This code has two problems:
1) First and second page is working fine but when goes to 3rd page its changing the current page number to 3 instead of moving cursor to the next 3rd page link
2) if 5 pages (numbers) to be displayed for paging like then when next clicked. It changes last displayed page as

I spent approximately 2 days but not able to fix it

Best Regards
 
Saloon Keeper
Posts: 24595
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We spend a lot of time discouraging people from trying to write complex application logic on JSPs using scriptlets.

Here, however. you've transgressed in the opposite direction.

Your life will become much easier if you use the popular servlet+JSP architecture where a servlet handles the business logic, collects the results in JavaBeans, then forwards to a JSP that uses the JavaBeans to populate the display and - in conjunctions with JSTL tags, to determine what blocks of HTML to show or hide based on the bean values.
 
Sheriff
Posts: 16718
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Farakh khan wrote:Hello,

Here is my code:


I hear a phone ringing... I bet it's the late 1990s calling to say "You need to stop doing that!"
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:We spend a lot of time discouraging people from trying to write complex application logic on JSPs using scriptlets.


This is not jsp. I am using servlet to handle all tasks that a JSP can do because JSP is being translated into Servlets and I don't think its complex. Its very simple code and even a beginner can understands very well.

Tim Holloway wrote:
Here, however. you've transgressed in the opposite direction.

Your life will become much easier if you use the popular servlet+JSP architecture where a servlet handles the business logic, collects the results in JavaBeans, then forwards to a JSP that uses the JavaBeans to populate the display and - in conjunctions with JSTL tags, to determine what blocks of HTML to show or hide based on the bean values.



Is this related to my question? None of the reply I got here is relevant to my question or nobody tried to fix my problem rather than talking about here and there
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Farakh khan wrote:Hello,

Here is my code:


I hear a phone ringing... I bet it's the late 1990s calling to say "You need to stop doing that!"




Share your 2021 call if you are able to fix my problem
 
Saloon Keeper
Posts: 1672
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am guessing people's replies wouldn't have been any different if this was in the "Servlets" forum?  Does it belong there regardless?
 
Tim Holloway
Saloon Keeper
Posts: 24595
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:I am guessing people's replies wouldn't have been any different if this was in the "Servlets" forum?  Does it belong there regardless?



Yes it does, and yes, I've moved it. It's definitely not "general" Java.

JFarakh khan wrote:
This is not jsp. I am using servlet to handle all tasks that a JSP can do because JSP is being translated into Servlets and I don't think its complex. Its very simple code and even a beginner can understands very well.



I feel my lip curling. No, this is really ugly code that could be made a lot more maintainable. It looks like something a beginner would write before learning better, in fact.

Yes, it's nicer to have everything in one file, but sometimes "nicer" isn't better.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:

Jesse Silverman wrote:
I feel my lip curling. No, this is really ugly code that could be made a lot more maintainable. It looks like something a beginner would write before learning better, in fact.
Yes, it's nicer to have everything in one file, but sometimes "nicer" isn't better.



I confess its very ugly code. I tried as under to optimize it:



Here is the problem which I am trying to fix it:

 
Tim Holloway
Saloon Keeper
Posts: 24595
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"optimization" is not the problem. Or rather, Java Strings are not the optimal way to express HTML. Which is why we also have JSPs.

Tweak it as you like, putting that much HTML in a servlet is not optimal. It doesn't provide Separation of Concerns, it's hard to read, and very error-prone to type in and maintain.

I recommend that you look into Model/View/Controller architecture relating to servlets and JSPs. MVC is a tried and true technology, predating the Internet itself, and readily adaptable in many forms for JEE webapps. Many JEE web frameworks are based on in, including Spring Data, Struts, JavaServer Faces and many less-common systems as well. But you don't need a formal framework to do web MVC and in fact, one of our senior moderators here pn the Ranch is just fine with roll-your own MVC solutions.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:"optimization" is not the problem. Or rather, Java Strings are not the optimal way to express HTML. Which is why we also have JSPs.



Thanks for your advice. I appreciate and will try to follow it. Please help me to fix my present problem
 
Marshal
Posts: 26915
82
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Farakh khan wrote:I am using servlet to handle all tasks that a JSP can do because JSP is being translated into Servlets...



You know that Java code is translated into byte-code, right? So you should be writing byte-code, because it can handle all tasks that Java code can do. Right?
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this forum I seen everybody is talking a lot about here and there but not coming to the point.

Can you fix my problem?
 
Jesse Silverman
Saloon Keeper
Posts: 1672
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Farakh khan wrote:In this forum I seen everybody is talking a lot about here and there but not coming to the point.

Can you fix my problem?



Hi Farakh:

I will dial the sarcasm meter down to 0 and explain things in a way which I hope can appeal to both of us.

Java is 25 years old now (it started even before, but has been publicly available for between 25 and 26 years at this point).

When it comes to programming, I am very much a fundamentalist, I don't like depending on API's and frameworks built on top of things I don't understand.

This starts at Core Java, the basic language, data types, expressions, statements, includes the core libraries and standard data structures, etc.

There are some things, many, actually, that are still in Java only because of history, and are no longer considered a good way to do things at this point in new code we are writing.

Some examples from core Java could be:

etc. etc.

I strongly support knowing and understanding the fundamentals underlying everything we are using.

It is legal and possible to do what you are trying to do 'with just servlets".

What they are saying is that even 20 years ago, you wouldn't want to be trying to do this anymore in real code.
It would quickly become a nightmare to develop, maintain, debug, etc.

There are many things even in the core of Java, C++, C#, etc. etc. that are still there and are always going to be there because of both backwards compatibility and because they are the actual basis underlying the "newer, better" ways of doing things that are available now.

I can tell you in C++ there are tons of things that people think "you should never write anymore" that the replacements are just "syntactic sugar" over them, the compiler actually invisibly generates the old stuff behind the scenes.  If you work on legacy code, it is good to understand the old ways, because you could see them in old code.  But people do NOT expect you to be coding them explicitly anymore in new code, we had developed the newer ways because working directly with these primitive elements was too hard to scale and maintain.

I have not done a lot of web programming in Java yet.

People here are generally trying to be helpful, with a few exceptions you haven't run into yet on this thread.

When you write something that was perfectly good in Java 8, but is deprecated in favor of something easier or better in say, Java 11 and up, they very politely show you both how to do it the older way you are using, and say "Look, here is this cool new better easier way that only works starting in Java 11" or something like that.

I believe your question 15 years ago would have gotten one of those answers, "OK, here's how we can do that, but starting in 2003, there's this new way (built on top of that) which is really much easier to write, debug, maintain and evolve.  You are going to have to learn some new stuff, but it will be totally worth it!"

Because so much time has evolved and past since then, and the first attempts to convey this to you failed, the sarcasm meter got dialed up a bit.

I think "What should we still know about Servlets when doing Jakarta EE programming in 2021?" would get interesting answers, tho not from me myself because I haven't done much of this.

I can vouch that the people who have replied to you with answers you did not find helpful are normally very helpful indeed, and can only infer that if you tried to do this on an interview for a current job, etc. they would think you had been stranded on a deserted island with a solar-powered laptop and no internet for 15 or 20 years and it would not go well.

I totally sympathize in that I hate using things that depend on fundamentals I am weak on, I want to understand what is going on behind the scenes, especially when things I write don't work the way I expect.

There are things that are simply deprecated, I hope you don't use Enumeration where you should be using Iterable, or having Vector all over your Java code.

There are other things that are still present and will continue to be present, but are no longer considered to be a productive way to write maintainable code when accessed directly, even where the replacements are ultimately always layered on top of them, sometimes invisibly, sometimes not.  Across the languages I use there are a lot of these, and I find it somewhat frustrating sometimes.

There were some EXTREMELY popular books that covered what you are doing from many years back.  I believe they are out of print and the websites accompanying them are no longer up.

We still have a "Servlets" forum here, so maybe I could ask "Hey, what do I still need to know about Servlets per se in 2021?  What should I still be doing with them directly now?  How should I be doing stuff I would have been coding directly with Servlets 22 years ago these days if I am making a website or going on interviews?"

I believe I have seen tutorial materials that say "I show a bunch of stuff about Servlets in this course, because the stuff we move on to later is kind of based on them underneath, please don't think I am stuck in the past!  I go on to show you the ways everyone does stuff today!"

I hope that helps.  If I knew more about this area I would join in trying to answer those things directly.
 
Jesse Silverman
Saloon Keeper
Posts: 1672
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When Tim said:

Your life will become much easier if you use the popular servlet+JSP architecture where a servlet handles the business logic, collects the results in JavaBeans, then forwards to a JSP that uses the JavaBeans to populate the display and - in conjunctions with JSTL tags, to determine what blocks of HTML to show or hide based on the bean values.



I am betting that "How do servlets usually fit in to today's modern world nowadays?" would be an expanded version of that long sentence, that might fit a paragraph for an intro, and a whole intro course with all the details.  I know about JavaBeans, know of (but don't know) JSP, and would have to learn about JSTL tags...
 
Jesse Silverman
Saloon Keeper
Posts: 1672
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I for one, am planning to read the article from 15 years ago that the author still referred to recently at the bottom of this (15 year-old) thread updated quite recently:
https://coderanch.com/t/359659/java/Advantages-servlets-JSP

Not today, tho, I am working on different stuff.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just WASTED my time here in this forum. Despite fixing my problem discussing issues about something else.
 
Paul Clapham
Marshal
Posts: 26915
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Farakh khan wrote:I just WASTED my time here in this forum. Despite fixing my problem discussing issues about something else.



No, you weren't really paying attention. People used to write code like that, 15 or 20 years ago. I had to do that then, too, and I hated it. JSP was so much better for that kind of thing when it came along. If you aren't using JSP (or some similar product) for generating HTML, you're doing it wrong.

So what you got here was the best possible advice for fixing that code: Convert it to JSPs. There's nobody here who wants to deal with your code as is, and the conclusion you should draw from that is this: That code isn't worth saving. That isn't an "issue about something else", it's an issue about the code you posted.

If you still don't agree with that, I'm sorry to hear it. I just hope that you aren't going to put that code somewhere where it might actually be used, and where future programmers might have to deal with it.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul I never said to not use JSP or never talked against JSP. If you can fix my problem in JSP show me. I am anxiously waiting
 
Paul Clapham
Marshal
Posts: 26915
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean, if I can solve it in JSP? Sure I could. But that isn't how this forum works. You don't come here to ask people to do your work, you come here to ask people when you have a problem in doing your work.

And so far I don't see any JSP which you're asking about. So that's the first step. Don't wait any longer, go and start writing JSP. Pagination is a common thing to do in JSP, but it's not a trivial thing especially if the pages aren't identical in format, so start with one page.
 
Tim Holloway
Saloon Keeper
Posts: 24595
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you can write HTML, you can write JSPs. The whole point is that JSPs are about 98% HTML, 0% executable Java source code (unless you want to annoy Bear), and about 2% JSP-specific directives.

The JSP directives come in 2 flavors:

1. Environmental directives (including the ability to include common JSP code in multiple JSP's)
2. custom tags that effectively expand the HTML tag set with things that are useful on a "smart" web page. Things like conditional display and enumeration of elements within a collection-type JavaBean (c:forEach tag).
Anyone can create custom tags, but there's also the Java Standard Template Library (JSTL) that comes with the webapp server.

In addition to the above, there's an Expression Language (EL) that allows you do reference the properties of JavaBeans without having to code specific "get" logic on the JSP.

At the end of the day, though JSPs are just Java's version of Microsoft's Active Server Pages (ASPs) or the PHP page language.

I've got entire book on servlets and JSPs that I'm about to give away because I no longer need it. Note that one book was enough to cover both!

And at the risk of admitting just how decrepit I am, I got started with servlets before JSPs were invented and I was VERY happy when JSPs came along and I no longer had to brute-force code HTML in servlet code!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic