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
harmeek_80Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.