File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Want to convert my code in method

 
Farakh khan
Ranch Hand
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using java servlet with Oracle XE

my pagging code is working fine but due to my inexperty I am not able to convert my pagging code into function to reuse it. Please help me

My code


Best Regards
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by refactoring--it will make things easier to pull out once you have some separate pieces to start with.
 
Farakh khan
Ranch Hand
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Start by refactoring--it will make things easier to pull out once you have some separate pieces to start with.


I tried to divide and commented like Pagging part-1 starts here and end here.similarly pagging part-2 started & ended

Is that you please mean?? Sorry as my English is not good

Best Regards
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not what I meant, but on the right track--but instead of just putting comments between sections, take the "sections" out and put them in to new methods.

The Wikipedia "refactoring" article gives a broad overview. In this case, with your code, I would start by pulling out small "chunks" of functionality into separate methods. This has two benefits:

- The code that *doesn't* deal with paging becomes easier to read

As you do that, you'll also find a good way (or several good ways, and occasionally some bad ways :) to make the paging code work for any type of object, on any page, and so on.

By the way, having the connection and result set be instance variables is a really bad idea--servlets must be thread safe.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For what it's worth, after some refactoring, I'm up to 11 methods, and am at the point where the abstraction/extension points are pretty clear, assuming we're generating our HTML in a servlet (not a great idea).

The paging logic (somewhat convoluted, has at least one strange redundancy) is around 150 lines (too long!). If getting the total record count, and paging link URL generation, the code is marginally general-purpose as-is.
 
Farakh khan
Ranch Hand
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:For what it's worth, after some refactoring, I'm up to 11 methods, and am at the point where the abstraction/extension points are pretty clear, assuming we're generating our HTML in a servlet (not a great idea).


Thanks for your favorable reply. 11 methods!!! great I am not thinking even a single method due to my incompetency. Can you please explain why generating HTML with servlet is not a good idea?

2) can you please copy paste some of your developed methods from 11 methods

David Newton wrote:
By the way, having the connection and result set be instance variables is a really bad idea--servlets must be thread safe.


Can you please explain it little bit more as to why I not instantiate them?

Best Regards
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Farakh khan wrote:
Can you please explain it little bit more as to why I not instantiate them?


Here is a good explanation -- http://www.javaworld.com/javaworld/jw-07-2004/jw-0712-threadsafe.html
 
Farakh khan
Ranch Hand
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Mercs wrote:
Farakh khan wrote:
Can you please explain it little bit more as to why I not instantiate them?


Here is a good explanation -- http://www.javaworld.com/javaworld/jw-07-2004/jw-0712-threadsafe.html


Thanks Sam for your favorable reply but my original question is still unanswered

Best Regards
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True -- But before you actually make it re-usable the first thing do to would be Separate the View from the Model. Look up for MVC and JSP to get some idea. Once you have some of the view separated out from the code -- you'll see that it is pretty manageable...

Also look for paging in JSP -- you should find some code snippets that would help you ...
 
Farakh khan
Ranch Hand
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Mercs wrote:True -- But before you actually make it re-usable the first thing do to would be Separate the View from the Model. Look up for MVC and JSP to get some idea. Once you have some of the view separated out from the code -- you'll see that it is pretty manageable...

Also look for paging in JSP -- you should find some code snippets that would help you ...


I am learning. Really I learned a lot at this forum

Please keep this thread open and I'll back to you

Best Regards
 
Farakh khan
Ranch Hand
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Mercs wrote:True -- But before you actually make it re-usable the first thing do to would be Separate the View from the Model. Look up for MVC and JSP to get some idea. Once you have some of the view separated out from the code -- you'll see that it is pretty manageable...

Also look for paging in JSP -- you should find some code snippets that would help you ...


I tried a lot but unable to fix the problem

Can you please split my code according to MVC. that'll be very helpful to start with MVC

Best Regards
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about showing us what you've done so far? Break out the functionality piece-by-piece: we're not going to do it for you, but we're happy to provide hints, etc. For example, here's a list of the methods I created during my first draft refactoring:

buildPagingTable
buildResultRows
closeDbArtifacts
countTotalRecords
createQuery
cvsUrl
cvsUrl
displayUrl
doGet
getCurrentPageNo
writeRow
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic