Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

redirect method: public or private method?

 
SAM KUMAR
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
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: 17278
6
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
 
Sonny Gill
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