• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

redirect method: public or private method?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In my servlet, doGet method to calls another method which basically create bean and forward to JSP, my question is that whether that method should be public or should I declare that as a private method?

Code Snippet:

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
redToHomePage(req, res);
}

private void redToHomePage(HttpServletRequest req,HttpServletResponse res) {
....
}


I am not sure whether I should declare redToHomePage as public or private?

Thanks in advance.

Sam
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, I'd ask what class do you have that method in. Is it in the Servlet? If it is, remove it from there immediately. The role of a Servlet is to handle a GET, POST or HEAD. Place the method into a Plain Old Java Object, and in that case the method will be public so that the Servlet can call that method.

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe that was a little too harsh, after all the method is a redirect.

A redirect is kind of handling the GET, POST, or HEAD, so why a seperate method. How many lines of code do you have in there?

Mark
 
SAM KUMAR
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, its a servlet. In doGet method basically servlet redirect to various JSP based on request parameter. This redirection is handled in a seperate method for clarity and also that method creates the appropriate bean and then forward to appropriate JSP.

My question was whether I should declare the redToHomePage(HttpServletRequest req,HttpServletResponse res) as public or private?

Thanks.

Sam
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you only ever call that method from doGet of your own servlet, you can and should keep it private.
But if clarity and ease of maintenance is important, you should consider moving the redirectTo method to a class, and call that method (in the external class) passing it the request and response objects..
..a better approach might be to pass only the request object to that method and get back the path to the page that the request should be redirected to and then do the redirection back in the servlet using the response object
HTH
 
Hold that thought. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic