Solved

bean not found within scope

Posted on 2007-04-03
27
5,488 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
ID: 18842518
Please post the jsp code.
0
 

Author Comment

by:Styx218
ID: 18842639
<%@ 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
ID: 18842828
>><%@ 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Styx218
ID: 18842923
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
ID: 18842943
<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
ID: 18843004
How you want to set values in Bean? where are you setting it?
0
 

Author Comment

by:Styx218
ID: 18843020
i was using type and still got same error
0
 

Author Comment

by:Styx218
ID: 18843041
Do you want my Bean code?
0
 
LVL 11

Expert Comment

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

Expert Comment

by:Manish
ID: 18843075
>>If you type ,
If you use type
0
 
LVL 11

Expert Comment

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

Author Comment

by:Styx218
ID: 18843094
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
ID: 18843098
Post Bean code.
0
 
LVL 11

Expert Comment

by:Manish
ID: 18843106
0
 
LVL 11

Expert Comment

by:Manish
ID: 18843112
<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
ID: 18843113
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
ID: 18843131
public Team(String newTeam,int num)
    {
        id = id;
        name = name;
    }

dont pass parameter to constructor.
remove it.
0
 

Author Comment

by:Styx218
ID: 18843184
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
ID: 18843194
*Which is expecting a string and an int as parameters in constructor
0
 
LVL 11

Expert Comment

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

Expert Comment

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

Expert Comment

by:Manish
ID: 18844069
0
 

Author Comment

by:Styx218
ID: 18844209
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
ID: 18848610
Are you using type in usebean tag?
0
 
LVL 11

Expert Comment

by:Manish
ID: 18848615
what is flow of you application?
0
 

Author Comment

by:Styx218
ID: 18857102
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
ID: 18862546
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can I   ajax html table  rows? 20 78
eclipse apache tomcat admin console 52 121
runtime exception 2 41
running on tomcat not jboss eap 7.0 3 18
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.

860 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