Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call servlet from action class

 
Farouk Sherefudheen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help plze
 
louise rochford
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much your help was much appreciated
It works now....
 
Farouk Sherefudheen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry wrong message
Help is much appreciated

Farouk
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic