DG3
DG3

Reputation: 5298

Java - converting string to a List

Is this the right way to convert a string to a list?

List styles = (List)request.getParameter("styles");

    Model (BeerExpert.java)

package com.example.model;
import java.util.*;

public class BeerExpert {
    public List getBrands(String color){
        List brands = new ArrayList();
        if(color.equals("amber")){
            brands.add("Jack Amber");
            brands.add("Red Moose");
        }
        else{
            brands.add("Jail Pale Ale");
            brands.add("Gout Scott");
        }
        return brands;
    }
}

The next is the servlet class

BeerSelect.java

package com.example.web;

import com.example.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class BeerSelect extends HttpServlet {
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
                        throws IOException,ServletException{
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("Beer Selection Advice <br>");
        String c = request.getParameter("color");

        BeerExpert be = new BeerExpert();
        List result = be.getBrands(c);

        request.setAttribute("styles", result);
        RequestDispatcher view = request.getRequestDispatcher("results.jsp");
        view.forward(request, response);
    }
}

Finally the jsp.

 results.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.*" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1 align="center">Beer Recommendations in JSP!!!</h1>
        <%
            List styles = (List)request.getParameter("styles");
            Iterator it = styles.iterator();
            while(it.hasNext()){
                out.print("<br> try " + it.hasNext());
            }
        %>
    </body>
</html>

Thanks

Upvotes: 3

Views: 10657

Answers (4)

Giulio Piancastelli
Giulio Piancastelli

Reputation: 15808

With the additional servlet/JSP context you provided, it seems that the real mistake in your code is the use of request.getParameter in the JSP page: that method indeed returns a String, and you can't convert a String in a List, not with a cast, not even with any other operation allowed by the language or the data structures. You may insert a String into a List, using one of the methods already suggested (or transform a List into a String using other methods), but judging from the code that's not what you need.

In the servlet code, you set the styles attribute to the List containing the beer brands. So, to get that List back, you need to invoke request.getAttribute instead of getParameter. The getAttribute methods returns an Object, which really is a List, and you know that because you have set it to be as such, so in this case a cast is exactly the operation that is needed to get back the value with its original type. In code, this means

List styles = (List) request.getAttribute("styles");

in your JSP, in place of the line that got you troubles.

Upvotes: 5

Malcolm
Malcolm

Reputation: 41510

List<String> list = Arrays.asList(yourString);

Just keep in mind that you won't be able to change the contents of the list. If you need to do that, create another list from this one manually.

Upvotes: 6

Francisco Spaeth
Francisco Spaeth

Reputation: 23903

If you are trying to retrieve more "styles" you can use:

request.getParameterValues("styles")

that will return an array of String, that could be used to create a List as follows:

List<String> styles = java.util.Arrays.asList(request.getParameterValues("styles"))

Upvotes: 0

Johan Sj&#246;berg
Johan Sj&#246;berg

Reputation: 49187

It's not, what you're doing is trying to cast a String to a List, which is not the same. A cast doesn't convert object, it merely tries to tell what type an object is.

What you need to do is:

List<String> list = new LinkedList<String>();
list.add(request.getParameter("styles"));

Upvotes: 1

Related Questions