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 redirect method: public or private method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "redirect method: public or private method?" Watch "redirect method: public or private method?" New topic
Author

redirect method: public or private method?

SAM KUMAR
Greenhorn

Joined: Jan 02, 2002
Posts: 25
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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

Joined: Jan 02, 2002
Posts: 25
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

Joined: Feb 02, 2002
Posts: 1211

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: redirect method: public or private method?