-3

How to call "/bin/wcmcommand" servlet that has been inside Out of box CQ5 Jar from Java Class. HTTPClient and URL Connection both the way i am not able to make a POST call to this servlet. i am getting ERROR CODE:500.

  • What, have you taken the source for the servlet and copied it into another project or is the servlet running in situ within CQ's OSGi runtime? – jacks Aug 19 '15 at 10:36
  • Servlet is running in inside CQ bundle. OOTB features is servlet called from JSP. But i want to call that servlet from My java class. – Vijay Marudhachalam Aug 19 '15 at 12:28
  • Not what a servlet is designed for really. Arguably better to copy the source of whatever behaviour you wish to consume and package inside your own class. – jacks Aug 19 '15 at 13:21

2 Answers2

1

Not sure what you meant by "servlet that has been inside Out of box CQ5 Jar" . What I understood from your question is , you want to call (HTTP GET/POST) a servlet from a Java class (and not from JSP !).

I assume its a Sling Servlet and has been registered with a path/resource type or any other valid way of registering a servlet.

If its registered with a "path" , you can trigger a HTTP request in many ways .

References:

Using java.net.URLConnection to fire and handle HTTP requests

calling a servlet from a java class

Community
  • 1
  • 1
Sabya
  • 1,339
  • 1
  • 9
  • 14
1

This OOTB Sling servlet only accepts sling request object. so can't call directly using HTTPClient in java class like normal servlet. But my problem got resolved using servlet communication. I have changed calling class from java class to Sling servlet class then i communicated using servlet communication like this.

RequestDispatcher req = request.getRequestDispatcher("/bin/wcmcommand");
req.include(request,response);

I want to call this servlet using multiple times with different value. So i have used SlingServletRequestWrapper class for modifying the request.getParameter() value each and every call.