Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6717155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:49:09+00:00 2026-05-26T08:49:09+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:49:10+00:00Added an answer on May 26, 2026 at 8:49 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this the right way to return an object from a function? Car getCar(string
Is there any way to convert a string to a variable in Javascript? Right
Is this the right way to do a nested navigation? <dl> <dt>Struktur</dt> <dd> <ul
I don't know if I'm thinking of this the right way, and perhaps somebody
I don't know if this is the right way to phrase the question or
Can anyone help me to do this in the right way?I mean.. like that
Sorry, I'm not really sure of the right way to ask this one so
I want to preface this by saying I believe the Right™ Way to handle
What is the most efficient way to do this? Right now I am doing
Wondering if I am going about this the right way. I am creating a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.