Link to home
Start Free TrialLog in
Avatar of harmeek_80
harmeek_80

asked on

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
Avatar of fargo
fargo

in action class or in servlet
String providerName = request.getParameter("providerName");

Avatar of harmeek_80

ASKER

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
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.

>> 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.
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..
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.
ASKER CERTIFIED SOLUTION
Avatar of fargo
fargo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial