This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Class cast Exception  RSS feed

 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I have already deployed an EJB jar (stateful session bean) file with Weblogic5.1 as App server.I wish to call thazt EJB from a servlet client.Ths code is as follows

package examples.sampleejb;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.naming.*;
import javax.rmi.*;
import javax.ejb.*;
import java.rmi.*;
public class client extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
/**Initialize global variables*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
/**Process the HTTP Get request*/
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Hello its here");
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String s = "";
try
{
Context ctx = getInitialContext();
Object obj = ctx.lookup("sample");
sampleHome home = (sampleHome) PortableRemoteObject.narrow(obj, sampleHome.class);
sampleRemote remote = home.create();
s = remote.say("Mary");
}
catch (Exception e)
{
e.printStackTrace();
}
out.println("<html>");
out.println("<head><title>client</title></head>");
out.println("<body>");
out.println(s);
out.println("</body></html>");
}
static public Context getInitialContext() throws Exception {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
return new InitialContext(p);
}

/**Clean up resources*/
public void destroy() {
}
}
=====================================================
The error on the weblogic server is as shown below.
============================================================
Sun Jan 14 16:28:24 GMT+05:30 2001: <ServletContext-General> samp: init
Hello its here
java.lang.ClassCastException: examples.sampleejb.sampleBeanHomeImpl_ServiceStub
at examples.sampleejb.client.service(client.java:30)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:865)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm
pl.java:105)
at weblogic.servlet.internal.ServletContextImpl.invokeServlet(ServletCon
textImpl.java:742)
at weblogic.servlet.internal.ServletContextImpl.invokeServlet(ServletCon
textImpl.java:686)
at weblogic.servlet.internal.ServletContextManager.invokeServlet(Servlet
ContextManager.java:247)
at weblogic.socket.MuxableSocketHTTP.invokeServlet(MuxableSocketHTTP.jav
a:361)
at weblogic.socket.MuxableSocketHTTP.execute(MuxableSocketHTTP.java:261)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java, Compiled Code)
===============================================================

Pl tell me where I am wrong in the servlet code
Thanx in advance
 
James Kiley
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, not to sound fatuous, but the stack makes it look like the problem is coming when you're fetching the original context and casting it into the bean type. Without any of my references in front of me I can't say for sure what's going on, but make sure you're fetching the right info out of the initialcontext and casting it properly.
 
Chee
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mary,
I have the answer for you....
First of all the lookup you are doing is wrong.
When you are looking up through a servlet you shouldn't use PortableRemoteObject.narrow ..You can directly get the home object through JNDI Name.
From your code :
Context ctx = getInitialContext();
Object obj = ctx.lookup("sample");
sampleHome home = (sampleHome) PortableRemoteObject.narrow(obj, sampleHome.class);
sampleRemote remote = home.create();
s = remote.say("Mary");
Modified Code:
Context ctx = getInitialContext();
sampleHome home = (sampleHome) ctx.lookup("sample");
sampleRemote remote = home.create();
s = remote.say("Mary");
The second reason for your exception ie classCastException is improper setting of your classpath or the Home & Remote class is not availabe in your package which you have stored in your \\weblogic\myserver\servletclasses folder
Cheers..Johnson
 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Johnson,
Thanx for ur reply

================================================================
The second reason for your exception ie classCastException is improper setting of your classpath or the Home & Remote class is not availabe in your package which you have stored in your \\weblogic\myserver\servletclasses folder
================================================================
I have stored the servlet in weblogic\myserver\servletclasses folder.
My EJB jar file is in \\weblogic\examples\sampleEJB\sampleEJB.jar .
PL tell me where I am going wrong?
My classpath settings is OK i guess.
Pl reply
 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
Is anyone there to solve my problem
Pl reply back
 
Mike Brown
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If WebLogic is throwing casting exceptions, or instanceof is not working as it should, the problem can sometimes be fixed by commenting out the weblogic.httpd.servlet.classpath in the weblogic.properties file.
 
Mad Sric
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,
If you have already tried to solve the problem using the Lookup method, then you could try removing the tempdeployment files in the weblogic, you remove all class files and jar files of that particular bean try compile the bean again - deploy it see weather it works,
I have gone th'u this and it had given some positive results...
Srikishore and Madhavi.
 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Thanx for your suggestions,
I finally got it working
 
vijaysai degalahal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I seem to be having the same problem when i use a bean with Jsp.
It works fine after i remove the server cache and recompile. But once if i restart the server it comes back.
Please give a permanet fix. Will serialization work??

------------------
Vijay Sai
 
krishnan sampath
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi mary,
Please tell me the steps you have followed to get the class cast exception solved. Because I am also getting the same problem and It does not get solved even after following the given suggestions.
with thanks
s.krishnan
Originally posted by Mary, Cole:
Hi all,

Thanx for your suggestions,
I finally got it working

 
glenn
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently, the Client.jar file must in the classpath during execution therefore
classpath = client.jar; etc...
Hope it helps
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!