Java – convert string to list

Is this the correct 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;
    }
}

Next comes 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, 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>

thank you

Solution

Using the additional servlet / JSP context you provided, the real error in your code seems to be using request. JSP in a JSP page Getparameter: this method does return a string, and you cannot convert strings in the list. There is no cast, or even any other operation allowed by the language or data structure You can insert a string into a list using one of the suggested methods (or convert a list to a string using another method), but you can judge by the code you don't need

In the servlet code, set the styles attribute to the list containing the beer brand Therefore, to restore the list, you need to call request Getattribute instead of getparameter The getattribute method returns an object, which is actually a list. You know, because you have set it to this, in this case, the cast is the operation required to retrieve the value with the original type In the code, this means

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

In your JSP, replace the lines that annoy you

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>