Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you left out the name attribute: how would the tag know which bean you were trying to access?
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
name attribute is in the <jsp:getProperty name="nameOfTheBean" property="nameOfTheProperty" value="valueForThatProperty"/>. Please check the JSp Specification for further details.
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class attribute should be in full qualified form!
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you should give the relevent scope to the Standard actions.

Let's know that!
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems that you've missed one line there, and can you post the BookSelectedforDel.java?
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abimaran,

Here is my bean class:


Also, can you tell me what line I missed out?
 
Vallidevi Appana
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Preity,

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

Regards
Valli
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use this.


And follow JavaBean standards! I think that's the problem!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, Vallidevi Appana already answered!
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody help please?

I am badly stuck
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It says it can't find the class.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put the class inside a package, and check it. I couldn't identify where is the problem.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, put the class file inside a package and use it in the Standard action. Thanks Bear Bibeault!
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear , Abimaran,

Gracias, Danke , Thank you!!!11

I put it in a package and it works perfectly.

Thank you for helping me out.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic