aspose file tools*
The moose likes Servlets and the fly likes setting / accessgin Java Bean between page and servlet. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "setting / accessgin Java Bean between page and servlet." Watch "setting / accessgin Java Bean between page and servlet." New topic
Author

setting / accessgin Java Bean between page and servlet.

Faisal A. Khan
Greenhorn

Joined: Feb 15, 2009
Posts: 5
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Faisal A. Khan
Greenhorn

Joined: Feb 15, 2009
Posts: 5
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 . . . ?
Alok Kushwah
Ranch Hand

Joined: Jul 10, 2007
Posts: 31
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

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

Joined: Feb 15, 2009
Posts: 5
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:
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

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

Joined: Feb 15, 2009
Posts: 5
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
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setting / accessgin Java Bean between page and servlet.
 
Similar Threads
Using bean declared in JSP from Servlet
session scopt problem
jsp:include
Servlet equivalent to <jsp:useBean ../>
Help reqd. in displaying results