Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

useBean problem ?

 
Deepinder S Narang
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends

When i use the below code for testing :

<jsp:useBean id="DB1" class="com.example.model.DBNameForJSP" scope="application" >
<jsp:setProperty name="DB1" property="DBName" value="Narang" />
</jsp:useBean>

<b> The DB name stored in JSP is : </b><jsp:getProperty name="DB1" property="DBName"/>

it gives me Class Cast Exception. And it doesnt create an bean for me.

But when i set the attribute using the foll:

<%@ page import="com.example.model.*" %>

<% DBNameForJSP db=new DBNameForJSP();
db.setDBName();
getServletContext().setAttribute("DB1",db); %>

The useBean works fine for. Why when used without the above scriptlet it gives me class cast exception ?

Please help ! Thanks in advance !
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you list full stack trace of your exception and explain a bit more about the issue.

Also change your property name from "DBName" to "dbName" and setter/getter would be setDbName(String) and getDbName()
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure there's no other "DB1" variable in the application scope ?
Try to output it before the useBean : ${applicationScope.DB1}

Using useBean with the class attribute will make a new bean if there's not already one in the specified scope. I suppose that there might be already one in application scope, and trying to cast it to DBNameForJSP fails.
 
Deepinder S Narang
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends

Thanks for the effort ! The bean wasnt set properly so was getting the error. Now its working

Regards
Deepinder
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic