Solved

bean not found within scope

Posted on 2007-04-03
27
5,352 Views
Last Modified: 2013-11-24
I am getting error  bean newBean not found within scope. I have changed the scope from session to request, and the bean is imported to the jsp page.What could be causing this error?
0
Comment
Question by:Styx218
  • 17
  • 10
27 Comments
 
LVL 11

Expert Comment

by:Manish
Comment Utility
Please post the jsp code.
0
 

Author Comment

by:Styx218
Comment Utility
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>


<%@ page import="com.football.Team"%>

<jsp:useBean id = "newTeam" type = "com.football.Team" scope ="session"/>
<jsp:setProperty name = "newTeam" property = "name"  />


<import javax.servlet.http.HttpSession/>


<c:set value = "Admin Page " var = "title" />

<sql:query var = "example" dataSource = "jdbc:mysql://localhost/footydb,com.mysql.jdbc.Driver,footy,footy">
 SELECT * from teams
</sql:query>

<html>
  <head>
    <script type="text/javascript" >


<form action="AdminServlet" method="POST">
      
      <input type="hidden" name="ACTION" value="LIST_ADD" />
      <tr>
        <td colspan="2"><b>Add new Team</b></td>
        <td><input type = "text"  name="name"  size="20"  /></td>
        <td><input name="ADD" type="submit" value="Add" /></td>
        <td>&nbsp;</td>
      </tr>
      

  </table>
        
        
        
        
        
                    
<a href="menu.html">Main Menu</a>
 <a href="admin.jsp">Admin Home</a>
             </body>
             </html>
0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
>><%@ page import="com.football.Team"%>
No need to import.

<jsp:useBean id = "newTeam" type = "com.football.Team" scope ="session"/>
where are you adding this in request or session?

Is Team class in classpath?
Are you using any IDE?
0
 

Author Comment

by:Styx218
Comment Utility
where are you adding this in request or session?

i dont understand.

Is Team class in classpath?
No, i have to add complete path to classpath? why is this?

I am using NetBeans 4.1

Thanks for your help :)

0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
<jsp:useBean id = "newTeam" class = "com.football.Team" scope ="session"/>

Is this give you error?

If you use type instead of class, this bean should be added in request or session , so that it will available in jsp.
0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
How you want to set values in Bean? where are you setting it?
0
 

Author Comment

by:Styx218
Comment Utility
i was using type and still got same error
0
 

Author Comment

by:Styx218
Comment Utility
Do you want my Bean code?
0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
If you type , the bean class should be availabe in request or session.
0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
>>If you type ,
If you use type
0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
Use class if you want to instantiate it like new in java.
0
 

Author Comment

by:Styx218
Comment Utility
well i have tried both and am getting same error,  i think problem is to do with jsp:setProperty, do i have to have a corresponding jsp:getProperty??
0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
Post Bean code.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:Manish
Comment Utility
0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
<jsp:useBean id="myBean2" class="com.mycompany.MyBean" scope="session">
        <%-- this body is executed only if the bean is created --%>
   
        <%-- intialize bean properties --%>
        <jsp:setProperty name="myBean2" property="prop1" value="123" />
    </jsp:useBean>
0
 

Author Comment

by:Styx218
Comment Utility
package com.football;

import java.beans.*;
import java.io.Serializable;

public class Team  implements Serializable
{
    private int    id;
    private String name;

    public Team(String newTeam,int num)
    {
        id = id;
        name = name;
    }

    public int getId()
    {
        return id;
    }

    public String getName()
    {
        return name;
    }

    public boolean equals(Object obj)
    {
        if (obj instanceof Team)
        {
            Team compare = (Team)obj;
            return (id == compare.getId());
        }
        return false;
    }

    public int hashCode()
    {
        return name.hashCode();
    }

    public String toString()
    {
        return getClass() + "[" + id + "] : [" + name + "]";
    }



    private void setId(int id)
    {
        id = id;
    }

    private void setName(String name)
    {
        name = name;
    }
0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
public Team(String newTeam,int num)
    {
        id = id;
        name = name;
    }

dont pass parameter to constructor.
remove it.
0
 

Author Comment

by:Styx218
Comment Utility
The problem with not passing parameter is this
 try

       {
                                           manager = getFootyManager();  //call method to get connection
               manager.addNewTeamToList(new Team(0, newTeam));

I pass instance of Bean to method addNewTeamToList
0
 

Author Comment

by:Styx218
Comment Utility
*Which is expecting a string and an int as parameters in constructor
0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
 public Team(String newTeam,int num)
    {
        id = id;
        name = name;
    }
  public Team()
    {
        id = id;
        name = name;
    }
0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
sorry
  public Team(String newTeam,int num)
    {
        id = id;
        name = name;
    }
  public Team()
    {
       
    }
0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
0
 

Author Comment

by:Styx218
Comment Utility
exception

javax.servlet.ServletException: bean newTeam not found within scope
      org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:846)
      org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
      org.apache.jsp.index_jsp._jspService(index_jsp.java:195)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)


root cause

java.lang.InstantiationException: bean newTeam not found within scope
      org.apache.jsp.index_jsp._jspService(index_jsp.java:98)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)

0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
Are you using type in usebean tag?
0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
what is flow of you application?
0
 

Author Comment

by:Styx218
Comment Utility
As above

<jsp:useBean id = "newTeam" type = "com.football.Team" scope ="session"/>
<jsp:setProperty name = "newTeam" property = "name"  />


The flow of my application is as follows

index.jsp provides a form to add users to database.Name is typed into form and "Add" button is pressed. Parameter should be passed to servlet within bean. Servlet  processes request and passes parameter to method within another class containing sql command. index.jsp should then be refereshed, displaying added information from database
0
 
LVL 11

Accepted Solution

by:
Manish earned 125 total points
Comment Utility
If you are using type in jsp in useBean tag.
You should add bean in session or request before coming to this jsp.
request.setAttribute("bean",new Team());
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
tenRun challenge 28 81
commonTwo  challenge 63 98
java continue statement 10 70
strDist challenge 35 84
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now