Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Call a servlet method into a util class

Avatar

Level 5

I have a servlet with doGet and navigationLogic methods. I need to call this navigationLogic method in my util class. How can I do that as @reference is making the servlet and util class unsatisfied? Please suggest something.

1 Accepted Solution

Avatar

Correct answer by
Employee

@goyalkritika if your navigationLogic method is separate from doGetMethod and its not using request and response objects then you can simply create an object of servlet class and call the method.

ServletClass abc = new ServletClass();

abc.navigationLogic();

 

if navigationLogic method is common code that can be used in other places then better put that method in Util class and call in servlet. it will fulfil the purpose of util class.

 

View solution in original post

4 Replies

Avatar

Employee Advisor

@goyalkritika 

In my opinion create a service class and put in your navigationlogic methods into it. This service can be referenced in both your servlet and utility class.

Adding a commonly used module in a Servlet, totally defeats the purpose of having services. 

Avatar

Community Advisor

@goyalkritika 

 

I agree with @krati_garg . All reusable code should be part of Util or an independent Service.

These should in turn be used in Servlets / Sling models / Workflow Steps etc.

 


Aanchal Sikka

Avatar

Correct answer by
Employee

@goyalkritika if your navigationLogic method is separate from doGetMethod and its not using request and response objects then you can simply create an object of servlet class and call the method.

ServletClass abc = new ServletClass();

abc.navigationLogic();

 

if navigationLogic method is common code that can be used in other places then better put that method in Util class and call in servlet. it will fulfil the purpose of util class.

 

Avatar

Employee Advisor

Please refactor your code and move that logic into a dedicated service, outside of the servlet.