0

I am new to jsp and beans.I tried to make a small bean example but getting a jsp compile time error as follows:

Student.jsp:1:1: Needed class "find_record.StudentBean" is not found.
^
Student.jsp:4:34: The bean type "find_record.StudentBean" was not found.
<jsp:useBean id="findbean" class="find_record.StudentBean" scope="session" />
                             ^-----------------------^
Student.jsp:5:23: This bean name does not exist.
<jsp:setProperty name="findbean" property="rollno" />

i have a Student.jsp to call StudentBean.java but getting that error following are the code: Student.jsp

<html>
<body >
<jsp:useBean id="findbean" class="find_record.StudentBean" scope="session" />
<jsp:setProperty name="findbean" property="rollno" />
<%
 String status=findbean.findRecord();
  if(status.equals("success"));
%>
<jsp:include page="Success.jsp" />
<% else %>
<jsp:include page="Error.jsp" />

</body>
</html>

StudentBean.java

//StudentBean,java
package find_record;
import java.sql.*;
public class StudentBean{
String name ,rollno,grade;
int marks;
public StudentBean(){}
public void setName(String name){
     this.name=name;
     }
 public  void setRollno(String rollno){
     this.rollno=rollno;
     }
 public void setMarks(int marks){
      this.marks=marks;
     }

  public  void setGrade(String grade){
     this.grade=grade;
      }

   public String getName(){
       return(name);
      }
   public String getRollno(){
        return(rollno);
     }

    public int getMarks(){
         return(marks);
       }
    public String getGrade(){
       return(grade);
      }
     public String findRecord(){

    try{
          Class.forName("oracle.jdbc.driver.OracleDriver");
  Connection                 conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","user","password");
         Statement st=conn.createStatement();

        ResultSet rs=st.executeQuery("Select * from student where rollno="+rollno);
        int c=0;
        while(rs.next()){
      System.out.println(rs.getString(1)+"  "+rs.getString(2)+"   "+rs.getString(3)+"                "+rs.getString(4));
        c++;
                     } 
         if(c>0)
              {
                 System.out.println("success");
               return("success");
               } 
              conn.close();
                 throw new Exception("record not found");

           }catch(Exception e){
 System.out.println(e.getMessage());
  return("no record found"); 
         }
   }
 }
vladof81
  • 26,121
  • 9
  • 38
  • 41
Deep
  • 929
  • 2
  • 16
  • 32

2 Answers2

0

Please refer to this post here which describes how to create a JavaBean. Your classes must implement Serializable in order for it to be a JavaBean (and have a serialVersionUID assigned). This allows the bean to be "flattened" and reconstructed where necessary.

Let me know if this doesn't solve the problem entirely. You also need to make sure that bean is getting placed in the session through the use of a servlet (or scriptlet in the JSP page).

Above did not solve it.

Try this:

<jsp:useBean id="findbean" class="find_record.StudentBean" scope="session">
    <jsp:setProperty name="findbean" property="rollno"/>
</jsp:useBean>
Community
  • 1
  • 1
abalos
  • 1,301
  • 7
  • 12
  • There is no need to serialize it. that's not an issue. Have you tested the code before posting your answer? – Braj Jul 03 '14 at 18:29
  • yes tested and error which is coming is also posted :Student.jsp:1:1: Needed class "find_record.StudentBean" is not found. ^ Student.jsp:4:34: The bean type "find_record.StudentBean" was not found. ^-----------------------^ Student.jsp:5:23: This bean name does not exist. – Deep Jul 03 '14 at 18:36
  • I added an addendum to the above post with another thing to try. If you are instantiating the bean using the useBean property, it is likely that you need to place the setProperty between the opening and closing tags of useBean. – abalos Jul 03 '14 at 18:41
  • actually i have done this before but was not working – Deep Jul 03 '14 at 18:48
  • I am creating a project on my computer. I'll see if I can get it working. – abalos Jul 03 '14 at 18:56
0

The only thing I can tell about your code is that you should not end the IF sentence with ";", or event left it without brackets... Your code as it is, I was not able to run it, but this way I could, try this:

<%
    String status = findbean.findRecord();
    if (status.equals("success")) {
%>
<jsp:include page="Success.jsp" />
<% } else { %>
<jsp:include page="Error.jsp" />
<% } %>

For everything else, you are doing it ok for the first times. Good luck!

Heraldo
  • 407
  • 2
  • 11