Spring

 View Only

 How can call java web servlet from spring controller. And this servlet is not part of spring context.

Mahesh Swarnkar's profile image
Mahesh Swarnkar posted Jul 12, 2019 04:33 PM

 

Mahesh Swarnkar's profile image
Mahesh Swarnkar

Tried below options to solve this problem. But did NOT work: -

 

  1. Calling through servlet request :-

HttpServletRequest newRequest = (HttpServletRequest) request;

String requestURI = newRequest.getRequestURI(); // modified URL

request.getRequestDispatcher(newURI).forward(request, response); // With new URI

 

  1. Using “forward” syntax: -

forward:/ReportServlet

Daniel Mikusa's profile image
Daniel Mikusa

I guess it depends on what you mean by "call". You could do a variety of things.

 

  • Use an HTTP client to send a request to the endpoint for your Servlet
  • Return an HTTP redirect that sends the user to the endpoint for your Servlet
  • Use a forward, like you tried (kinda surprised that didn't work, would be curious to know more about what happened)
  • Call the same code that is being called by the Servlet. For example, if the servlet uses a service or function, you could call the same service/function from your code.

 

There's probably other things you could do too. I guess it just depends the details of how you want this to work.