• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pass a variable from JSP to Servlet Action

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
I need to pass a variable from JSP page to a servlet action method.

i set the variable as the attribute in a request scope but not able to retrieve it in the LookupDispatchAction method.

below is my code snippet in c




and i am retrieving the test attribute in my action servlet as

public ActionForward testAction(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
....
System.out.println("Request attribute =" + request.getAttribute("test") );
.....
}

the above SOP returns null for me. i donno why?


i am submitting the temp.jsp page and forward the request to the action servlet through struts-config.xml.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Putting a value in the request in a JSP and then expecting to retrieve it in an Action class isn't going to work. When a JSP runs, it renders HTML code and sends it to the browser. Once it has done this, the life cycle of the request object is over and anything in request scope is gone. When the form is submitted, a new request is started, but one used when the page was generated is gone. You could have your JSP put values in the session and then retrieve them in the Action, but that is an awkward and non-standard way of doing things.

The means that Struts has provided to pass data from the JSP to the Action class is the ActionForm. Just provide input fields, either hidden or viewable in the JSP and match them up to properties of an ActionForm. Use the struts-config.xml file to associate the ActionForm to the Action that is called when the form is submitted. If you do this, struts will instantiate and populate the ActionForm when the form is submitted and pass it to the execute() method of your Action class, where you can read its properties.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i tried with the ActionForm for passing the variable to the struts action.

below is my part from jsp page


below is my action form.


the problem is the variable xmlPayLoad is populated with empty value,


snippet of my config file

 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i tried with the ActionForm for passing the variable to the struts action.

below is my part from jsp page


below is my action form.


the problem is the variable xmlPayLoad is populated with empty value,


snippet of my config file



i donno why action form is not populated correctly. if it populated correctly instead of returning empty string, full xml file should printed in console.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to understand that since we're using the HTTP protocol to pass data to and from the user's browser, the only data type we can really use is String. We can't use other data types such as Byte[] or java.util.Date.

There may be a way to pass on byteArrXmlFile to the next action without involving the JSP at all. What scope is byteArrXmlFile in? If it's in request scope, why not just have whatever process put it in request scope put it in session scope instead? If it's in session scope, the next action can retrieve it from the session without involving the JSP.
[ July 19, 2006: Message edited by: Merrill Higginson ]
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the only way to keep the Object between the requests is to put it in the session.

Thanks merill.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic