getting a Javabean with request scope in a servlet

Hi all!

I have a JavaBean (A REQUEST SCOPE ONE!!) as an attribute to be attached to my HttpRequest.
In servlet enviroment all does work fine with:

request.setAttribute("login", loginBean);
...
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/Servlet1");
dispatcher.forward(request, response);
...


but if i forward it from a jsp page (trying to attached to the request as an attribute) to a servlet invocated by a submit form as in the following:

<jsp:useBean id="login" class="LoginBean" scope="request"/>
...
<form name="form" action="Servlet2" method="post">
 ....  
<input name="submit" type="submit" value="submit">
...
</form>


and i do in Servlet2:
...
LoginBean bean = (LoginBean) request.getAttribute("login");
login.setSomeProperty(request.getParameter("anotherparam"));

it raises me a NullPointerException as if the bean is not retrieved.
Has the request already expired?
I tried also in jsp the following:
<%
request.setAttribute("login", loginBean); %>

If so, am i forced to use a session-scope-Javabean?

for my application is compulsory having a request scope


Any suggestions would be highly appreciated!
Please help me!
Best Regards,
Fede
elisraiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
 You are not supposed to use the bean like this. When you define it in the request using the <jsp:useBean> tag you can only use it in the page you define it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kennethxuCommented:
when client request to the jsp page, this is one request. when client submit the form on the jsp page to servlet, it's another request. they are all different request, request scope beans doesn't survive from one request to the other. if you want you bean to survive cross requests, you must use session scope, and that's what session scope designed for.

>> for my application is compulsory having a request scope
I don't understand why you cannot, but if you cannot use session scope. you'll have to put all bean info in form's hidden fields, and reconstruct the bean again in the servlet receives the submitted form.
0
girionisCommented:
 So kennethxu do you say that if he does this:

  <jsp:useBean id="login" class="LoginBean" scope="session"/>

  then he will be able to retrieve the bean in another page by doing:


  LoginBean bean = (LoginBean) request.getAttribute("login");
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

elisraiAuthor Commented:
so what a request-scope-bean is for?
why can't i define a bean only for ONE request or for a limited number of requests?
or at least how can i destroy a session-scope-bean when it is not useful anymore without invalidating or destroying the whole session?

and why this works fine if i forward through servlets?

sorry if this sounds stupid:-)
fede
0
elisraiAuthor Commented:
so what a request-scope-bean is for?
why can't i define a bean only for ONE request or for a limited number of requests?
or at least how can i destroy a session-scope-bean when it is not useful anymore without invalidating or destroying the whole session?

and why this works fine if i forward through servlets?

sorry if this sounds stupid:-)
fede
0
girionisCommented:
 But you already define the bean for a requst. When your request is done to your JSP page (with the <jsp:useBean> tag) then this is one request. When you go to another page and try to read the bean there then that's a different request.

> and why this works fine if i forward through servlets?

  Because you forward the bean alongside with the request by setting the attribute on the request object.
0
kennethxuCommented:
>> so what a request-scope-bean is for?
for you to use the bean in current page and/or FORWARD the request to another servlet/jsp. please notice the word forward, it means server internal forward, client doesn't aware of this.

HTTP protocal, browser send a request to web server and gets response from web server, this is called one request.

>> why can't i define a bean only for ONE request or for a limited number of requests?
the request object itself is destroied when the request is processed and response is send to browser. it is by DESIGN and that's why it is called request scope. valide during the request processing, end when request is processed.
and session scope is DISGNED for bean need to be accessed across multiple request. browser process client's request concurrently, hundreds of browser can access same page in same time. when you say number of requests, you are basically talking about number of requests in the session, so you are involving session here anyway, again you at least need to put the counter in the session and reset the counter later, then what's the different between store a counter in session and store your bean in session, using counter only make things more complicated.

>> or at least how can i destroy a session-scope-bean
session.removeAttribute()

>> and why this works fine if i forward through servlets?
it is not because servlets, you can forward between servlets and jsps without problem. as I said above, forwarding is server internal thing, it is considered as one single request regardless of how many times you forwarded your requst.
0
elisraiAuthor Commented:
>  Because you forward the bean alongside with the request by setting the attribute on the request object.

it's the thing i want to do with the jsp
do u think it's not possible?
why can't attach attributes setted by
request.setAttribute to a request created via submit form?
0
elisraiAuthor Commented:
>  Because you forward the bean alongside with the request by setting the attribute on the request object.

it's the thing i want to do with the jsp
do u think it's not possible?
why can't attach attributes setted by
request.setAttribute to a request created via submit form?
0
kennethxuCommented:
>>  LoginBean bean = (LoginBean) request.getAttribute("login");
he need
 LoginBean bean = (LoginBean) session.getAttribute("login");
0
girionisCommented:
>it's the thing i want to do with the jsp
>do u think it's not possible?

  It is. What stops you fo doing this:

request.setAttribute("login", loginBean);
...
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/Servlet1");
dispatcher.forward(request, response);
...

  in your JSP page?

>but if i forward it from a jsp page (trying to attached to the request as an attribute) to a servlet invocated by a submit form as in the following:
>
><jsp:useBean id="login" class="LoginBean" scope="request"/>
>...
><form name="form" action="Servlet2" method="post">
>....  
><input name="submit" type="submit" value="submit">
>...
></form>

  No, you do not forward it, you just define it to be valid for one request. When the response comes back to the browser your bean becomes invalid.
0
kennethxuCommented:
or in servlet:
 LoginBean bean = (LoginBean) request.getSession().getAttribute("login");
0
girionisCommented:
 Substitute this:

request.setAttribute("login", loginBean);

  with

request.setAttribute("login", login);

  wheer "login" is the id of the bean you gave in the <jsp:useBean> tag.
0
girionisCommented:
 .. so your JSP page should look:

<jsp:useBean id="login" class="LoginBean" scope="request"/>
...
...
request.setAttribute("login", login);
...
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/Servlet1");
dispatcher.forward(request, response);
...

0
kennethxuCommented:
>> it's the thing i want to do with the jsp
this is no difference between jsp and servlet in terms of request scope bean and session scope bean.

>> why can't attach attributes setted by request.setAttribute to a request created via submit form?
it is conceptually wrong. form submit is NOT forward, they are completely different mechanism. forward is server internal action within one single browser request. form submit is a brand new request from browser.
0
elisraiAuthor Commented:
ok..i thin you are right kennetxu..so how can i destroy a session-scope-bean without destroying session?
is that possibile without calling session.invalidate() ?

0
elisraiAuthor Commented:
ok..i thin you are right kennetxu..so how can i destroy a session-scope-bean without destroying session?
is that possibile without calling session.invalidate() ?

0
elisraiAuthor Commented:
ok..i thin you are right kennetxu..so how can i destroy a session-scope-bean without destroying session?
is that possibile without calling session.invalidate() ?

0
elisraiAuthor Commented:
ok..i thin you are right kennetxu..so how can i destroy a session-scope-bean without destroying session?
is that possibile without calling session.invalidate() ?

0
girionisCommented:
 Please elisrai stop hitting refresh. If you want to refresh the page please do it from "refresh the question" link on the top left of the page.
0
elisraiAuthor Commented:
ok..i thin you are right kennetxu..so how can i destroy a session-scope-bean without destroying session?
is that possibile without calling session.invalidate() ?

0
girionisCommented:
 Just set it to null would do.
0
elisraiAuthor Commented:
ok..i thin you are right kennetxu..so how can i destroy a session-scope-bean without destroying session?
is that possibile without calling session.invalidate() ?

0
elisraiAuthor Commented:
ok..i thin you are right kennetxu..so how can i destroy a session-scope-bean without destroying session?
is that possibile without calling session.invalidate() ?

0
kennethxuCommented:
as I told you in my previous post,

in jsp:
session.removeAttribute("login");

in servlet:
request.getSession().removeAttribute("login");
0
kennethxuCommented:
BTW, refresh-retry will report your comment again and again, consider it as EE feature (or bug :). use the reload page link in the upper-left of page and then hit refresh.
0
girionisCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Split points between girionis and kennethxu

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.