aspose file tools*
The moose likes Servlets and the fly likes Creating a java bean in a servlet and accessing it in a jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Creating a java bean in a servlet and accessing it in a jsp" Watch "Creating a java bean in a servlet and accessing it in a jsp" New topic
Author

Creating a java bean in a servlet and accessing it in a jsp

Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

I want yo create a java bean in servlet and set its properties there.
I want to access this bean in a jsp.
Is this possible?

I tried the following :
Wrote a java bean.
Created its instance in the servlet and set its properties.


Next I put the bean tags in the jsp to access the attribute values:


When the jsp executes it get the following error message:
org.apache.jasper.JasperException: /delbookchoose2.jsp(47,0) UseBean: Mandatory attribute id missing

Is it possible to set the attribute for the bean BookSelectedforDel in the servlet?

Priety.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Priety Sharma wrote:Is this possible?

Yes.

Created its instance in the servlet and set its properties.

You must create a scoped variable using the bean via request.setAttribute()

<jsp:useBean class="/onlinelib/BookSelectedforDel" />

The class attribute must match the bean classname -- I have no idea what that is that you put there. And you need an id attribute that matches the scoped variable name.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Bear,
Thanks for the info.

I did the following:
Servlet code:


Here BookSelectedforDel is my bean class.

The code in the jsp to which I am forwarding the request:


I have put the bean class file in the classes folder of my application "onlinelib".

Now I get the following error when the jsp execute:
org.apache.jasper.JasperException: /delbookchoose2.jsp(48,0) GetProperty: Mandatory attribute name missing

Also, I am using Tomcat 5.0.28
The exact location of my bean class is:
Tomcat 5.0\webapps\onlinelib\WEB-INF\classes
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Well, you left out the name attribute: how would the tag know which bean you were trying to access?
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi David,
You are right. I have not specified the name attribute.
I am confused here.
When you create a java bean in a jsp it has a name attribute in jsp:usebean tag.
But I have create this one in a servlet. So how do I set the name attribute here?

Well I tried to give a name attribute in the jsp:


Then I got the error:
rg.apache.jasper.JasperException: /delbookchoose2.jsp(47,0) UseBean has invalid attribute: name

So looks like it expects the name attirbute to be set when the java bean is created in the servlet.
How do I set this name attribute in the servlet?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Please pay attention. The name attribute doesn;'t go on useBean. Get a good tag reference. Otherwise you are flying blind.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

name attribute is in the <jsp:getProperty name="nameOfTheBean" property="nameOfTheProperty" value="valueForThatProperty"/>. Please check the JSp Specification for further details.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi All,

Thanks for the inputs.
Yes I was inattentive.

This is what I have now in my jsp which is trying to access the bean created in the servlet:


As mentioned earlier the bean class is in the WEB-INF\classes folder of my application [onlinelib].
I now get the following error on the jsp:
org.apache.jasper.JasperException: /delbookchoose2.jsp(47,0) The value for the useBean class attribute BookSelectedforDel is invalid.

Why is that?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

The class attribute should be in full qualified form!
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Abimaran ,

I agree to what you say, but I have do not have a package for the bean class.
So how do I qualify it?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

So, in which scope, did you bind the attribute? Default scope for this standard action is page, which you can't set from a servlet. So have a look on that!
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Abimaran,

This how I have put the bean in request scope in my servlet:

I tried to check if the bean is available in the jsp with the following code:


And this is what gets shown on the page:
trying to access attribute bkinfobean :BookSelectedforDel@1162a9c

But when I try to use usebean tag it still gives the error mentioned earlier.
How do I get it working?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

So, you should give the relevent scope to the Standard actions.

Let's know that!
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Abimaran,

I did what you said:


Now I get the following error message:
org.apache.jasper.JasperException: Cannot find any information on property 'author' in a bean of type 'BookSelectedforDel'

Is it again creating a new bean here and hence the error above?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Seems that you've missed one line there, and can you post the BookSelectedforDel.java?
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Abimaran,

Here is my bean class:


Also, can you tell me what line I missed out?
Vallidevi Appana
Greenhorn

Joined: Jun 30, 2006
Posts: 25
Hi Preity,

The setters and getters in a bean should have public access modifier.

Regards
Valli
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Use this.


And follow JavaBean standards! I think that's the problem!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Oh, Vallidevi Appana already answered!
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi,

I have made all bean getter and setter methods public now.

Now getting great amount of errors:
org.apache.jasper.JasperException: Unable to compile class for JSP An error occurred at line: 49 in the jsp file: /delbookchoose2.jsp Generated servlet error: D:\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\onlinelib\org\apache\jsp\delbookchoose2_jsp.java:99: cannot find symbol symbol : class BookSelectedforDel location: class org.apache.jsp.delbookchoose2_jsp BookSelectedforDel bkinfobean = null; ^ An error occurred at line: 49 in the jsp file: /delbookchoose2.jsp Generated servlet error: D:\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\onlinelib\org\apache\jsp\delbookchoose2_jsp.java:101: cannot find symbol symbol : class BookSelectedforDel location: class org.apache.jsp.delbookchoose2_jsp bkinfobean = (BookSelectedforDel) _jspx_page_context.getAttribute("bkinfobean", PageContext.REQUEST_SCOPE); ^ An error occurred at line: 49 in the jsp file: /delbookchoose2.jsp Generated servlet error: D:\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\onlinelib\org\apache\jsp\delbookchoose2_jsp.java:103: cannot find symbol symbol : class BookSelectedforDel location: class org.apache.jsp.delbookchoose2_jsp bkinfobean = new BookSelectedforDel(); ^ An error occurred at line: 50 in the jsp file: /delbookchoose2.jsp Generated servlet error: D:\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\onlinelib\org\apache\jsp\delbookchoose2_jsp.java:109: cannot find symbol symbol : class BookSelectedforDel location: class org.apache.jsp.delbookchoose2_jsp out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((BookSelectedforDel)_jspx_page_context.findAttribute("bkinfobean")).getAuthor()))); ^ An error occurred at line: 51 in the jsp file: /delbookchoose2.jsp Generated servlet error: D:\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\onlinelib\org\apache\jsp\delbookchoose2_jsp.java:112: cannot find symbol symbol : class BookSelectedforDel location: class org.apache.jsp.delbookchoose2_jsp out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((BookSelectedforDel)_jspx_page_context.findAttribute("bkinfobean")).getBookname()))); ^ An error occurred at line: 52 in the jsp file: /delbookchoose2.jsp Generated servlet error: D:\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\onlinelib\org\apache\jsp\delbookchoose2_jsp.java:115: cannot find symbol symbol : class BookSelectedforDel location: class org.apache.jsp.delbookchoose2_jsp out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((BookSelectedforDel)_jspx_page_context.findAttribute("bkinfobean")).getCopies()))); ^ 6 errors
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Can somebody help please?

I am badly stuck
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It says it can't find the class.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Put the class inside a package, and check it. I couldn't identify where is the problem.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Have you read through the entries in the JSP FAQ? If so, you'd discover that all classes used in web applications must be in a package other than the default.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

So, put the class file inside a package and use it in the Standard action. Thanks Bear Bibeault!
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Bear , Abimaran,

Gracias, Danke , Thank you!!!11

I put it in a package and it works perfectly.

Thank you for helping me out.

 
jQuery in Action, 2nd edition
 
subject: Creating a java bean in a servlet and accessing it in a jsp