Is it possible for a java servlet to call the function that is in another java servlet? And if it is possible, can you show me a simple example of how to do it?
Thank you in advance
Is it possible for a java servlet to call the function that is in another java servlet? And if it is possible, can you show me a simple example of how to do it?
Thank you in advance
Calling a servlet directly from another servlet is not recommended and considered bad practice because servlet instances are managed by the servlet container. You should follow the separation of concerns principle.
The servlets are responsible for the interface to clients only and shouldn't contain business logic. Put your business logic in a separate layer (e.g. classes in another package) and call it from the servlets only. So the business classes are responsible for the actual internal data and transformations and the servlets are responsible for different views to the outside.
Several options.
Making it static one way (I do not prefer).
Create a Class and supply required params and create instance in each servlet.
Somewhat hackish way:
RequestDispatcher dispatcher = request.getRequestDispatcher("/someServletOfYours");
dispatcher.forward(request, response);
Now, implement the method of yours in doPost of your "someServletOfYours" class. In case you need to pass parameters, call the setAttribute
method of ServletRequest, and fetch parameters from the request in your next servlet. From "someServletOfYourrs" you can redirect back to your original servlet. It will imitate a method call by means of http.
Servlet1
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Servlet1
*/
@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Servlet2 s = new Servlet2();
s.CreateUser();
response.getWriter().append("Served at: ").append(request.getContextPath());
}
}
Servlet2
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Servlet1
*/
@WebServlet("/Servlet2")
public class Servlet2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
public void CreateUser() throws FileNotFoundException{
System.out.println("Create Users...");
}
}
Output :