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 Want to convert my code in method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Want to convert my code in method" Watch "Want to convert my code in method" New topic
Author

Want to convert my code in method

Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
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

Joined: Sep 29, 2008
Posts: 12617

Start by refactoring--it will make things easier to pull out once you have some separate pieces to start with.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Mar 22, 2008
Posts: 732
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

Joined: Feb 08, 2009
Posts: 606

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


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
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

Joined: Feb 08, 2009
Posts: 606

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

Joined: Mar 22, 2008
Posts: 732
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

Joined: Mar 22, 2008
Posts: 732
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

Joined: Sep 29, 2008
Posts: 12617

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
 
 
subject: Want to convert my code in method