wood burning stoves 2.0*
The moose likes Struts and the fly likes Call servlet from action class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Call servlet from action class" Watch "Call servlet from action class" New topic
Author

Call servlet from action class

Farouk Sherefudheen
Ranch Hand

Joined: Jan 27, 2004
Posts: 32
Hello All

I have a action class from where i have a forward. Actually I have to call a servlet from the action class and add 2 request parameters to it.
CAn you tell me how can i do this please
The servlet is defined as /XYZ inside the web.xml
Thanks in advance
Farouk
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
In your struts-config, just define a forward that points to your servlet and have your Action return to that forward. If the parameters you want to add are static, make them part of your forward. If the parameters you want to add are dynamic, once you get get the ActionForward in your Action (via mapping.findForward), you can do a forward.getPath() to get the String for your path, append "?p1=x&p2=y" or whatever parameters you need to the String, then pass it to forward.setPath() before returning the forward.

Farouk Sherefudheen
Ranch Hand

Joined: Jan 27, 2004
Posts: 32
This is what i get if i do what you asked me to do.
[05/04/05 15:55:03:891 BST] 754ac7a4 WebGroup X Servlet Error: Configuration is frozen: java.lang.IllegalStateException: Configuration is frozen
at org.apache.struts.config.ForwardConfig.setPath(ForwardConfig.java:208)
at com.rbsg.cesev.application.cqms.client.create.test.CQMSAction.execute(CQMSAction.java:48)
at org.apache.struts.action.RequestProcessor.processActionPerform
RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.ibm.servlet.engine.webapp.StrictServletInstance.doService
ServletManager.java:827)
at com.ibm.servlet.engine.webapp.StrictLifecycleServlet._service(StrictLifecycleServlet.java:167)
at com.ibm.servlet.engine.webapp.IdleServletState.service(StrictLifecycleServlet.java:297)
at com.ibm.servlet.engine.webapp.StrictLifecycleServlet.service(StrictLifecycleServlet.java:110)
at com.ibm.servlet.engine.webapp.ServletInstance.service(ServletManager.java:472)
at com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch(ServletManager.java:1012)
at com.ibm.servlet.engine.webapp.ServletInstanceReference.dispatch(ServletManager.java:913)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:678)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:331)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:117)
at com.ibm.servlet.engine.srt.WebAppInvoker.doForward(WebAppInvoker.java:134)
at com.ibm.servlet.engine.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:239)
at com.ibm.servlet.engine.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:67)
at com.ibm.servlet.engine.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:151)
at com.ibm.servlet.engine.oselistener.OSEListenerDispatcher.service(OSEListener.java:315)
at com.ibm.servlet.engine.http11.HttpConnection.handleRequest(HttpConnection.java:60)
at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:323)
at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:252)
at com.ibm.ws.util.CachedThread.run(ThreadPool.java:137)
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Don't call setPath() then, and instead try:



The gist of it is to dynamically create an ActionForward. The javadocs will show you what constructors and methods are available.
Farouk Sherefudheen
Ranch Hand

Joined: Jan 27, 2004
Posts: 32
Thanks for your reply
I do appreciate it.
I get this error now. It is not able to find the servlet to be loaded it is defined in the Web.xml
and when we use the link pointing to this URL it is able to load it.


Error 404: File not found: AutomaticEventEARWeb/ServletTestRunner
Please help
Farouk Sherefudheen
Ranch Hand

Joined: Jan 27, 2004
Posts: 32
Finally now in the action foward i am able to find the servlet and load it as it has not implemented doPost method, i am getting the following error message

Error 405: HTTP method POST is not supported by this URL

Question:
1. Why does it invoke a POST method for loading this servlet from action method.
2. Is there any way to convert it as GET

Thanks
Would appreciate
Farouk
Farouk Sherefudheen
Ranch Hand

Joined: Jan 27, 2004
Posts: 32
Can anyone help plze
louise rochford
Ranch Hand

Joined: Apr 04, 2002
Posts: 119
Sounds like you don't have the doPost() method defined in your servlet. Try calling the existing doGet() from inside a new doPost() method.
[ April 06, 2005: Message edited by: louise rochford ]
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Originally posted by Farouk Sherefudheen:
1. Why does it invoke a POST method for loading this servlet from action method.


I suspect you called the Action via a POST from a form. As the generated request's method is POST, and that request is simply passed on to the Servlet (no new request is generated), that would explain why the container is looking for the servlet's doPost() method.

2. Is there any way to convert it as GET

Your best bet will be to implement doPost() in the servlet to call its doGet() method. If for some reason you can't modify the servlet, extend it and implement your own doPost() method which would call the superclass's doGet() method.
Farouk Sherefudheen
Ranch Hand

Joined: Jan 27, 2004
Posts: 32
Thanks very much your help was much appreciated
It works now....
Farouk Sherefudheen
Ranch Hand

Joined: Jan 27, 2004
Posts: 32
Sorry wrong message
Help is much appreciated

Farouk
 
jQuery in Action, 2nd edition
 
subject: Call servlet from action class
 
Similar Threads
calling a listener class
Can we apply struts tag libraries like struts-html.tld to a servlet file?
HTTP 404 error
Can i call action class getRedirectUrl() method from another class?
how to call Servlet with parameters