?
Solved

getting a Javabean with request scope in a servlet

Posted on 2003-03-18
28
Medium Priority
?
445 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:elisrai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 9
  • 7
28 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 152 total points
ID: 8159497
 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
 
LVL 14

Assisted Solution

by:kennethxu
kennethxu earned 148 total points
ID: 8159696
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
 
LVL 35

Expert Comment

by:girionis
ID: 8159763
 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:elisrai
ID: 8159908
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
 

Author Comment

by:elisrai
ID: 8159963
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
 
LVL 35

Expert Comment

by:girionis
ID: 8159988
 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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8160063
>> 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
 

Author Comment

by:elisrai
ID: 8160064
>  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
 

Author Comment

by:elisrai
ID: 8160094
>  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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8160135
>>  LoginBean bean = (LoginBean) request.getAttribute("login");
he need
 LoginBean bean = (LoginBean) session.getAttribute("login");
0
 
LVL 35

Expert Comment

by:girionis
ID: 8160142
>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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8160143
or in servlet:
 LoginBean bean = (LoginBean) request.getSession().getAttribute("login");
0
 
LVL 35

Expert Comment

by:girionis
ID: 8160150
 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
 
LVL 35

Expert Comment

by:girionis
ID: 8160170
 .. 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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8160187
>> 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
 

Author Comment

by:elisrai
ID: 8160276
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
 

Author Comment

by:elisrai
ID: 8160277
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
 

Author Comment

by:elisrai
ID: 8160293
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
 

Author Comment

by:elisrai
ID: 8160303
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
 
LVL 35

Expert Comment

by:girionis
ID: 8160324
 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
 

Author Comment

by:elisrai
ID: 8160339
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
 
LVL 35

Expert Comment

by:girionis
ID: 8160479
 Just set it to null would do.
0
 

Author Comment

by:elisrai
ID: 8160491
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
 

Author Comment

by:elisrai
ID: 8161237
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8161586
as I told you in my previous post,

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

in servlet:
request.getSession().removeAttribute("login");
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8161610
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
 
LVL 35

Expert Comment

by:girionis
ID: 10060144
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question