Search code examples
javaspringjspspring-mvcjavabeans

Trouble implementing Bean class in Java- ClassCastException


I am new to java beans and JSP. In my project, what i did is, I created multiple beans like LoginBean, RegisterBean, AddCourseBean etc.

Suppose m on page A.jsp which is using bean LoginBean for getting and setting some properties. After that m moving to page B.jsp which is using another bean say RegisterBean for setting some property or adding data into database.

While moving from page A to B m getting in error in tomcat

HTTP Status 500 - java.lang.ClassCastException: ankit.ShowCourseBean cannot be cast to ankit.LoginBean

type Exception report

message java.lang.ClassCastException: ankit.ShowCourseBean cannot be cast to ankit.LoginBean

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.ClassCastException: ankit.ShowCourseBean cannot be cast to ankit.LoginBean
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

java.lang.ClassCastException: ankit.ShowCourseBean cannot be cast to ankit.LoginBean
    org.apache.jsp.components.jsp.Login_jsp._jspService(Login_jsp.java:72)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.42 logs.

My code in JSP page to use the bean is as follow: Page A.jsp

    <%@   page   language="java"  %>
    <%@ page import="java.sql.*;" %>

    <%!  
    ResultSet rs;
    String coursename,courseid;
    %>

    <jsp:useBean   id="obj"  scope="application"  class="ankit.ShowCourseBean" />

    <%
        rs=obj.getCourse();
    %>
//my remaining Html and logic

Page B.jsp

<%@   page   language="java"  %>
<%!  
boolean b;
String myemail, mypassword,userName,userType;
%>

<jsp:useBean   id="obj"  scope="application"  class="ankit.LoginBean" />
<jsp:setProperty  name="obj"   property="myemail"  />
<jsp:setProperty  name="obj"    property="mypassword"  />
<% 
myemail=request.getParameter("myemail");
mypassword=request.getParameter("mypassword");

try{
  b=obj.checkLogin(myemail,mypassword);
  System.out.println(b);
if(b)
{
 userName=obj.checkUserName();
 userType=obj.checkUserType();
 System.out.println(userName);
 session.setAttribute("loggedUserName",userName);
 session.setAttribute("loggedUserType",userType);
 response.sendRedirect("http://localhost:8080/vt/index.jsp");
}
else
{
out.println("Wrong User Name and Password");
}
}catch(Exception  e)
 {  
 out.println("Wrong User Name and Password");
 e.printStackTrace();
 }

%>

Solution

  • You define the bean with name "obj" as scope="application" this means it is available globally.

    have a look at the different scopes (page, request, session, application) and use them appropriately. In this case you can avoid the problem as well by using different names for the objects. (instead of both 'obj').

    when you run your program an object is stored under name "obj" in the application scope (like a static variable) and then in the other jsp you try to cast it to another type. (useBean creates a bean when there is none under the name, or uses the bean found in place when there is one).

    so basically: switch to request scope if you just need the object for the duration of your request. if you need both for session/application scope then you'll have to use different names.

    also: never use generic names like obj and stuff and thing . name it after the thing it actually represents or the function it fills.