Generics

Ola…

estou com um problema…

esta ocorrendo um erro no List…

de uma olhada…

	List styles = (List) request.getAttribute	("styles");
	Iterator it = styles.iterator();
	while(it.hasNext()){
		out.print("<br> try: "+ it.next());
	}

Eu coloquei no List = request.getAtributer(“styles”);
E no Iterator tbm, Iterato it = styles.iterator();

mas ele mostra erro no request.getAtribute…

me ajudem…

obrigado…

Qual é o tipo que vem no atributo “styles”?

Quem seta o atributo “styles” deve fazê-lo com um List<String> senão vai ocorrer um ClassCastException.

Vc deve fazer:

List<String> styles = (List<string>) request.getAttribute("styles");
Iterator<String> it = styles.iterator();

while(it.hasNext()){
   out.print("<br> try: "+ it.next());
}

O Servlet esta mandando este “styles”

olhe o codigo do Servlet


		String c = request.getParameter("color");
	//  out.println("<br>Got beer color" + c);
	
		
		BeerExpert be = new BeerExpert();
		List<String> result = be.getBrands(c);
	
	//	java.util.Iterator<String> it =  result.iterator();
		
		request.setAttribute("styles", result);
		
		RequestDispatcher view = request.getRequestDispatcher("resut.jsp");
		view.forward(request, response);

muito obrigado…

Qual o erro? tem certeza que esse cara nao esta null? Tenta colocar um teste antes

List<String> style = new ArrayList<String>();
if(request.getAttribute("styles") != null){
  style = (List<string>) request.getAttribute("styles");  

} else {
  System.out.println("Erro");

}

mas o

style = (List<string>) request.getAttribute("styles");

esta acusando um warning do Eclipse…

o q eu faço. ?

muito obrigado.

[quote=Juvenal]mas o

style = (List<string>) request.getAttribute("styles");

esta acusando um warning do Eclipse…

o q eu faço. ?

muito obrigado.[/quote]

Acontece que os generics servem justamente para garantir que em uma List como:

List<String>
…só haverá realmente Strings. Se você faz um cast (como no caso) o compilador gera um warning, algo como: “Você está fazendo o cast, logo a responsabilidade é sua. Se seu programa der pau, não vai dizer que não avisei.”. A maneira de eliminar esse aviso (sob sua responsabilidade) é usar a anotação ‘SuppressWarnings(“unchecked”)’.