Swap attributes from one Object to another

Asked

Viewed 200 times

0

In my java application I have a declosed object like Extrair extrair = new Extrair(); And I use this object and its attributes for my Serrvlet. But I need to pass this object to .jsp. In jsp I use extrair = request.getAttribute();, only that the request method returns an Object and I am assigning an Object to an Extract.

How can I fix this?

  • 5

    Maybe I didn’t quite understand the question, but it wouldn’t simply be the case to make a cast? extrair = (Extrair)request.getAttribute();

  • He won’t accept, it was the first thing I tried. @mgibsonbr

  • 2

    Does not accept syntax, or does not work (i.e. gives a ClassCastException)? I ask because as far as I know there is nothing in JSP that prevents a code inline to make a cast... If it is the second case, check if the way you are adding this object in the request (I believe I saw setAttribute) is correct. By the way, no parameter is missing in the getAttribute?

1 answer

1


Everything indicates that you need to simply cast (as @mgibsonbr indicated), however, to ensure that no one Classcastexception it is indicated to check if the object instance corresponds to the class you are using:

Object attribute=request.getAttribute();
if(attribute instanceOf Extrair)
    extrair = attribute;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.