• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 438
  • Last Modified:

How to pass a parameter from jsp to servlets in struts

Hi Friends,
I have this form:

<html:form action="/viewproviders">
    <html:select property="providerName">
    <html:options collection="providerslist" property="providerName" labelProperty="providerName" />
</html:select>

I fill this combo box and then i want that when the user selects a entry and clicks submit,I should be able to retrieve the value in my action class,please please tell me how to do that...thanks
0
harmeek_80
Asked:
harmeek_80
  • 3
  • 2
  • 2
1 Solution
 
fargoCommented:
in action class or in servlet
String providerName = request.getParameter("providerName");

0
 
harmeek_80Author Commented:
Thanks Fargo,that worked.Can I please ask you one more small question,even if you won't reply I would award you full points for the original question.
When,the user clicks the "search" link, I get the values from the session and make a combo box and present to him.The problem is,I have to store the values in the session first,so that when the user clicks the search link,the combo box has the values.
Rite now,I have placed an extra query in the loginAction,which queries the database and sets the session variable all the time beforehand the user clicks the search link,but i dont think this is a good way ,so can you please suggest me the rite way to do so..thanks
0
 
dumanbCommented:
If you have the correct configuration, namely the  Action and ActionForm classes are defined correctly, then you have a method called execute in your Action class like below.

public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest req,
HttpServletResponse res)
{
}

I mean the Action class that  you should already defined for the path "/viewproviders" in your struts-config file. When the form is submitted (when  user clicked the submit button)
Action class gain the control and the form submitted is a parameter to your action (the second param. ActionForm form) and you can cast it to the type you use as below


public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest req,
HttpServletResponse res)
{
    YourFormClass yform = (YourFormClass) form;
}

Then you have the form submitted and you can get each value your form using the getter method for each field. (yform.getProviderName() in your case)

As a final word, I recommend you to avoid using request.getParameter() methods for accessing the fields of your form in Struts since this is not pure JSP and you already have the Actionform with all the fields set properly.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
fargoCommented:
>> Rite now,I have placed an extra query in the loginAction,which queries the database and sets the session variable all the time beforehand the user clicks the search link

I don't understand this line.
0
 
harmeek_80Author Commented:
See,this is my code to display the combo box which contains the list of providers to the user:

<html:form action="/viewproviders">
    <html:select property="providerName">
    <html:options collection="providerslist" property="providerName" labelProperty="providerName" />
</html:select>

The combo box reads the session variable "providerslist",but that has to be set before rite.so,I just want to know where to set that variable.Rite now,in the Login Action when the user logs in,I set that variable in the background,so when the user clicks the "search" link,the session is already set and the user is able to see the list of providers.This is a how i do in it  LoginAction.java

ArrayList pList = csDAO.getCouponProviders();
        if(pList  != null){
          request.getSession().getServletContext().setAttribute("providerslist", pList);
        }

So,please suggest me the rite way...
Thanks a lot "dumanb" , I would try to use that option too...thank you bro..
0
 
dumanbCommented:
Before going to bed, I may give another advice to you about lists you use for your select lists. :)

If all the users share the same list, you may store your list at application scope. So all users can access the same list avoiding memory overhead. you can access the application scope by
action's servlet attribute

such as you may have an action to put the list in to application scope

public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest req,
HttpServletResponse res)
{
....
this.getServlet().getServletContext().setAttribute("list",yourListObject);

}

and then you can get this list in any of your actions' execute method  like below


this.getServlet().getServletContext().getAttribute("list");



Hope this helps.
0
 
fargoCommented:
in my opinion, the way u r doing is fine.

u may also set it in session like
request.getSession().setAttribute("providerslist", pList);

or in the application scope as said above.

The way said above using the action form is absoloutely fine. I posted b'coz in your title it was servlet instead of action.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now