bean not found within scope

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?
Styx218Asked:
Who is Participating?
 
ManishConnect With a Mentor LeadCommented:
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
 
ManishLeadCommented:
Please post the jsp code.
0
 
Styx218Author Commented:
<%@ 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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
ManishLeadCommented:
>><%@ 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
 
Styx218Author Commented:
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
 
ManishLeadCommented:
<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
 
ManishLeadCommented:
How you want to set values in Bean? where are you setting it?
0
 
Styx218Author Commented:
i was using type and still got same error
0
 
Styx218Author Commented:
Do you want my Bean code?
0
 
ManishLeadCommented:
If you type , the bean class should be availabe in request or session.
0
 
ManishLeadCommented:
>>If you type ,
If you use type
0
 
ManishLeadCommented:
Use class if you want to instantiate it like new in java.
0
 
Styx218Author Commented:
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
 
ManishLeadCommented:
Post Bean code.
0
 
ManishLeadCommented:
0
 
ManishLeadCommented:
<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
 
Styx218Author Commented:
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
 
ManishLeadCommented:
public Team(String newTeam,int num)
    {
        id = id;
        name = name;
    }

dont pass parameter to constructor.
remove it.
0
 
Styx218Author Commented:
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
 
Styx218Author Commented:
*Which is expecting a string and an int as parameters in constructor
0
 
ManishLeadCommented:
 public Team(String newTeam,int num)
    {
        id = id;
        name = name;
    }
  public Team()
    {
        id = id;
        name = name;
    }
0
 
ManishLeadCommented:
sorry
  public Team(String newTeam,int num)
    {
        id = id;
        name = name;
    }
  public Team()
    {
       
    }
0
 
Styx218Author Commented:
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
 
ManishLeadCommented:
Are you using type in usebean tag?
0
 
ManishLeadCommented:
what is flow of you application?
0
 
Styx218Author Commented:
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
All Courses

From novice to tech pro — start learning today.