2

enter image description here

I'm retreiving the list of product bought by the user but I'm getting an error since in the constructer product is of type List. I didn't knew how to convert it

String buyer = request.getParameter("buyer");
List prodlist = request.getParameter("product");
Bill bill = new Bill(buyer, prodlist);

here is the code of the constructeur

    public Bill(String buyer, List<Product> product) {
        super();
        this.buyer = buyer;
        this.product= product;
    }

the attribute of the class Product

    private int id;
    private String name;
    private float price;
jan
  • 109
  • 8
  • use generic list `List< Product> prodlist = request.getParameter("product");` @jan – Ryuzaki L Apr 28 '19 at 22:52
  • If `getParameter` returns a `String`, you'll need to use a method that returns a `List`, or find a way to parse that `String` into a `List`. – Jacob G. Apr 28 '19 at 22:53
  • @Deadpool same thing it is giving error "Type mismatch: cannot convert from String to List – jan Apr 28 '19 at 22:56
  • look at the @JacobG. comment, use method that returns `List` object or by if string is `json` then by using `Objectmapper` convert it to `List` – Ryuzaki L Apr 28 '19 at 22:57
  • can you show the `String str = request.getParameter("product");` output of this `str` @jan – Ryuzaki L Apr 28 '19 at 23:22
  • What is `Product` and how did you expect some text (value of `product` query parameter) to magically become a `Product`, let alone a list of them? --- My *guess* would be that `product` is a repeating query parameter with the product *ID*, not the entire `Product` object. Re-think what you're doing. – Andreas Apr 28 '19 at 23:28
  • @Deadpool the problem is with the creating of the new object it can't admit the parameter prodlist as String cause I defined it as List in the Bill class – jan Apr 28 '19 at 23:32
  • @Andreas the product has id, name and price as attribute – jan Apr 28 '19 at 23:35
  • What is the *text value* of the `product` query parameter? How did you expect that text to become a `Product` object? --- How did you expect us to help you when you don't show what the `Product` class is, or what value the `product` parameter has? – Andreas Apr 28 '19 at 23:38
  • @Andreas u can see the Product parameter – jan Apr 28 '19 at 23:45
  • `request.getParameter("product");` what does this return, i know it returns string type but i'm asking for value @jan – Ryuzaki L Apr 28 '19 at 23:47
  • @Deadpool depending on the case, here for example I gave in parameter keyboard and it returned it – jan Apr 28 '19 at 23:56
  • @jan why you want to use list here. the request.getParametre() is giving you only single parametre 'String' right ? – Onkar Musale Apr 29 '19 at 08:51
  • @OnkarMusale yes it gives only one but Ihave many products in the bill – jan Apr 29 '19 at 12:11
  • @Jan it's really unclear what you're asking. edit the question and give in detail description with your code, sample inputs, sample outputs. – Onkar Musale Apr 29 '19 at 12:23
  • @OnkarMusale in my application I have a user who selected many products in order to buy it. I have to create him a bill which contains these products as well as their sum. I want to retreive all the product in the form of a list in sevlet – jan Apr 29 '19 at 12:28

1 Answers1

2

(Edited: using an object class.)

Either

String[] products = request.getParameterValues("product");
List<Product> prodlist = new ArrayList<>();
for (String productName : products) {
    Product product = loadProduct(productName); // Or such
    prodlist.add(product);
}

or use

List<String> prodlist = Arrays.asList(request.getParameterValues("product"));

That getParameterValues is for multiple values for the same parameter "product" is possible. This is in general a String[]. The getParameter(String) method is for a parameter occurring only once; in fact a special case.

The same result would be for an URL "http: ... my.html?product=pc&product=phone&product=tablet".

You should check that indeed the HTML contains several <input name="product">, maybe using the browser developer tools, generally evoked by F12 inside the browser.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • thanks for answering but the list type should be product not String – jan Apr 29 '19 at 13:06
  • List prodlist = Arrays.asList(request.getParameterValues("product")); it needs a cast or parse – jan Apr 29 '19 at 13:09
  • I have edited my answer; you need somewhere to retrieve a Product (from a database?). – Joop Eggen Apr 29 '19 at 13:16
  • for the moment the product are sotored into session variables. I just didn't understand the "loadProduct" – jan Apr 29 '19 at 13:20
  • Having received product names from the request form, you need to take the Product with the same name from somewhere. Temporarily from the session you say, so get all possible products from there. You also try a drop-down list of products. In the web there are nice examples. – Joop Eggen Apr 29 '19 at 13:26
  • alright but this line >> Product product = loadProduct(productName); // Or such. is unclear for me :) – jan Apr 29 '19 at 13:30
  • Sorry, that was my phantasy. "Or such" is the more realistic meaning. I do not know how to retrieve the Product corresponding with a product name. – Joop Eggen Apr 29 '19 at 13:32
  • please explain more – jan Apr 29 '19 at 13:37
  • I think some sample from the internet will be more helpful and give more ideas that I can. Sorry. – Joop Eggen Apr 29 '19 at 13:41