• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

setting / accessgin Java Bean between page and servlet.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to JSP. I wanted to send bean object from page to servlet and then change bean's data in servlet and then set it back to the page. So I created a bean and included it in the page as below

<jsp:useBean id="beanobj" class="mypkg.mybean" scope="application" />

When it try to get data in servlet using the following code, I get error. Null pointer exception.

mybean beanobj = new mybean();
beanobj = (mybean)req.getAttribute("beanobj");

I have tried both with and without following tag
<jsp:setProperty name="beanobj" property="*"/>

This is one part of the problem. javascript:emoticon('');

The other part is that I want to set the bean data in servlet and then send that bean to page so that by making --> this code <%=beanobj.getName%> as the value of my html text box, I could see the value from bean. This does not work, however if I use EL, that works i.e. ${beanobj.name}.

Can any one help me as to how could I use <jsp:useBean tag and yet achieve the functionality to send bean back and forth between servlet and page. I have explored JSTL but not interested in it yet because first I wanna get a good grip on simple JSP and Servlets and be able to move data between these two layers.

Many thanks in advance.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you put something away in the hall closet, would you go look for it later in the pantry?

Similarly, if you put a scoped variable into application scope, you will not later find it in request scope where you did not put it.
 
Faisal A. Khan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:If you put something away in the hall closet, would you go look for it later in the pantry?

Similarly, if you put a scoped variable into application scope, you will not later find it in request scope where you did not put it.



I have tried with all possibilities of what "scope" offers. Meaning by, session, page, application and request. Nothing works.
Any bright idea . . . ?
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scope of bean is application context scope, so try to get it from application not from request. To get it from application use ServletContext.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Faisal A. Khan wrote:Any bright idea . . . ?

Yes, look for an item in the same closet that you put it in.

If you put it in application scope, that's where you need to fetch it from.

 
Faisal A. Khan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
WHAT AM I MISSING.... I have got the same reply from many sources and nothing seem to work for me. Following is my source code.

JSP Code:



Servlet Code:


My Bean Code:
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're missing the fact that you shouldn't be using scriptlets for this. You should in no way use scriptlets in JSP.

Use EL. Use for example ${beanobj.name} instead of <%= beanobj.getName() %>.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are placing the bean in request scope during the execution of the JSP. When the JSP finishes executing, the response is sent to the browser and the request is dismissed. So are any scoped variables in request context. So when the page is submitted, the bean has long since been gone and cannot be retrieved from the servlet. This is basic HTTP.

The fact that you are trying to send a bean from a JSP to a servlet indicates a few problems. First, that you do not yet understand how JSP and HTTP requests work, and second, probably indicates a design flaw of some sort.

For the former issue, please read this article. Then you may in a position to determine where your design flaw lies.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bauke Scholtz wrote:You're missing the fact that you shouldn't be using scriptlets for this.


While I agree that scriptlets should not be used in JSP pages, that's the least of the poster's problem at this point.
 
Faisal A. Khan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You are placing the bean in request scope during the execution of the JSP. When the JSP finishes executing, the response is sent to the browser and the request is dismissed. So are any scoped variables in request context. So when the page is submitted, the bean has long since been gone and cannot be retrieved from the servlet. This is basic HTTP.

The fact that you are trying to send a bean from a JSP to a servlet indicates a few problems. First, that you do not yet understand how JSP and HTTP requests work, and second, probably indicates a design flaw of some sort.

For the former issue, please read this article. Then you may in a position to determine where your design flaw lies.



From your responce:

So when the page is submitted, the bean has long since been gone and cannot be retrieved from the servlet. This is basic HTTP.



If this is the case, then what good are "req" and "res" in the doPost method ?


Will "req" be long gone when it is used in the doPost method ? I doubt it . . . ! I am totally new to JSP but it really hunts me if we can not use these "req" and "res" objects.
Please see this page
MVC in JSP
section Listing 3: ShoppingServlet.java . I learned getting parameters and other stuff from this site and tried to get the bean object the same way and ran in to this problem of Null pointer exception. If there is some design problem, then compared to the URL of MVC, what exactly is wrong in my desgin . . . ?

Sorry for not posting the code properly earlier. I appreciate your help in this regard.

Thanks and look forward
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Faisal A. Khan wrote:
Will "req" be long gone when it is used in the doPost method ? I doubt it . . . ! I am totally new to JSP but it really hunts me if we can not use these "req" and "res" objects.



you misunderstood Bear's quote..

This is nothing to do with JSP .. its basic that request originates from client say, HTML ( after all are intrepreted as HTML ) and posted to server either using GET or POST method , which is handled by Servlet/JSP container (depending on whats your action is ) .

The request parameters ('req' which you mean) is used by doPost to process the input values and take appropriate action by calling another utility class , bean or redirests to JSP... The response is generated then and sent back to the client by Container. Thats it the scope of request and response attributes are gone by now.. and no way to gain it... and if you want to use some attributes between requests then "session" comes to picture..
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic