0

the question is strange some way I think, and I can't find a direct answer to it, so please be patient with me

I am working on a service at company using Servlets.

based on some condition in code i will proceed the request to one of two servlets.

if(SOME_CODITIONS == VALUE1)
    getServletContext().getRequestDispatcher("/SERVLET_1").forward(request,response);
else 
    getServletContext().getRequestDispatcher("/SERVLET_2").forward(request,response);

Some one told me that using a bean that contains the code and calling that method to do the same job the servlet has to do is better, and this (the above one) has weak performance and the bean way is better.

Is that correct and does forwarding the request will allocate more memory?

NOTE: the code that written in SERVLET_1 or SERVLET_2 will not be called directly from browser

hope any one has one tip to share it with me.

Omar.Nassar
  • 379
  • 1
  • 3
  • 14
  • In short words he is correct. The `RequestDispatcher.forward()` mehtod should be used to forward to JSP files, as they are allowing easier development of UI code, and separate presentation code (html, javascript, etc) from Java. Forwarding to servlet means that presentation code is hard-coded in the Java class which is a bad practice. So if that code must be in the class and your servlets are not accessible as servlets, there is no point of making them servlets and should be just beans. But you should rather use MVC pattern, check this: http://stackoverflow.com/a/3542297/3701228 – Gas Oct 13 '14 at 21:50
  • ok, nice and I agree with you, but the main point he was talking about is the performance of the bean is better than the servlet. seriously the servlet that I called does not contain any presentation code, second, I imagined that we may need this servelt for another services. – Omar.Nassar Oct 14 '14 at 05:27

0 Answers0