This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Pass a variable from JSP to Servlet Action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Pass a variable from JSP to Servlet Action" Watch "Pass a variable from JSP to Servlet Action" New topic
Author

Pass a variable from JSP to Servlet Action

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
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.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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.


Merrill
Consultant, Sima Solutions
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
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

Joined: Mar 01, 2005
Posts: 485
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

Joined: Feb 15, 2005
Posts: 4864
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

Joined: Mar 01, 2005
Posts: 485
So the only way to keep the Object between the requests is to put it in the session.

Thanks merill.
 
wood burning stoves
 
subject: Pass a variable from JSP to Servlet Action
 
Similar Threads
Help in Multipart Request Urgent
Cannot set attribute in request in jsp
In JSF there is no form action
Retrieving information from previous page's form
get disabled values from HttpServletRequest parameter