Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Apache Tomcat »

Re: Bean and Servlet

Mohammed Zabin

2007-07-24

Replies:

Actually, I wanted to pass this list from within a Tag to be used inside
another Servlet class, (i.e. the tag class after finishing rendering its
elements, goes to a servlet, so, i need to pass this list to that servlet.
As i have stated above, i used the following to store the list in the
request object:

[code]pageContext.getRequest().setAttribute("QList", list);[/code]

In the servlet, i used the following to retrive the list:

List<Integer> list = (List<Integer>)request.getAttribute(
"QList");

I tried to access the above list by a small code snippet that prints its
size:
[code]out.println( list.size() );[/code]
But i got an exception stating that a NullPointerException has occured at
the size printing statement. That's mean, i think, the list didn't stored
correctly in the request object, am I true?


On 7/23/07, David Smith <dns4@(protected):
>
> Typical design is servlet forwarding to jsp for view. Tags being jsp
> elements occur after servlets have executed. So you can see how your
> problem is a little curious in that jsps don't typically forward to
> servlets. If you are forwarding a request from jsp to servlet, let us
> know.
>
> However if you are storing a object in a request during jsp execution
> intending it to be available to the servlet on the next request, that
> doesn't work. The request is cleared and recycled when jsps finish
> writing to the client. When a new request comes in, the request
> attribute list is empty. If you need this list to live between
> requests, you need to place it in the session.
>
> --David
>
> Mohammed Zabin wrote:
>
> > Thank you Johnny,
> > To be specific this is my question
> > Hi all
> >
> > I am trying to pass an object from a tag to a servlet. i did the
> > following,
> >
> > 1. In the tag class, i put:
> >
> > pageContext.getRequest().setAttribute("QList", list);
> >
> >
> >
> > The above Tag will go to a servlet, i need to read the above request
> > attribute in the servlet, how can i do this?, i tried
> >
> > List<Integer> list = (List<Integer>)request.getAttribute("QList");
> >
> > , but when i tried to access the read list, it gave me
> > NullPointerException...What do u think?
> >
> >
> > On 7/23/07, Johnny Kewl <john@(protected):
> >
> >>
> >> Hi Mohammed,
> >> Cant say I really understand the question....
> >>
> >> In general this is what the Session Objects are for....
> >> So say you have a servlet and a JSP page.... and say the servlet
> >> makes the
> >> bean with the Array List in it.... then if you
> >>
> >> session.setAttribute("MyBean", MyBean);
> >>
> >> you can get it (MyBean) back when the next call comes into say the JSP
> >> page....
> >>
> >> Now if you read up on this you will see you can also set 'request'
> >> objects.... and these are good for when you say dispatch a request to a
> >> JSP
> >> page from the servlet and want to pass a bean across.
> >>
> >> Anyway... if you just google for "servlet session and
> setAttribute"....
> >> you
> >> will be on your way... I think ;)
> >> This area of servlet programming is one of the things that make it
> >> such a
> >> powerful technology.
> >> Have fun...
> >>
> >> ----- Original Message -----
> >> From: "Mohammed Zabin" <jotnarta@(protected)>
> >> To: "Tomcat Users List" <users@(protected)>
> >> Sent: Monday, July 23, 2007 11:18 AM
> >> Subject: Bean and Servlet
> >>
> >>
> >> > Hi All
> >> > What is the best way to pass a list collection from a Bean to a
> >> Servlet?
> >> > Thank you
> >> >
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To start a new topic, e-mail: users@(protected)
> >> To unsubscribe, e-mail: users-unsubscribe@(protected)
> >> For additional commands, e-mail: users-help@(protected)
> >>
> >>
> >
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@(protected)
> To unsubscribe, e-mail: users-unsubscribe@(protected)
> For additional commands, e-mail: users-help@(protected)
>
>
©2008 junlu.com - Jax Systems, LLC, U.S.A.