This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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.
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?
Joined: Jan 02, 2002
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?
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