This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

redirect method: public or private method?  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!