Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes CastException: java.lang.String can not be cast to class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "CastException: java.lang.String can not be cast to class" Watch "CastException: java.lang.String can not be cast to class" New topic
Author

CastException: java.lang.String can not be cast to class

Prateek abhi
Greenhorn

Joined: Sep 08, 2013
Posts: 9
Greeting,
I have again posted my query with proper format.I have attached the code and error message. when servlet calls the view "xy.jsp"; This
view is not working and throwing a CastException.
I have identified the error point "it is at <jsp:useBean>". I have simply set a variable "result" to session at servlet and calls a View(xy.jsp). I want to fetch the value of "result" at JSP but only with the help of <jsp:useBean>.Kindly help me to resolve it.



this is a class "abc" inside "myPack" Package. it contains business logic which is being called




Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 316
    
    2

First of all may I suggest you use code tags around most of your post, it makes it much easier to read and provides line numbers so we can point to a section easier.

A CastException like A cannot be cast to B usually means you're trying to use = to do something it can't. In your case somewhere in there an abc object is being assigned a string but it's so hard to read I haven't found the exact line.

Joe


It's not what your program can do, it's what your users do with the program.
Prateek abhi
Greenhorn

Joined: Sep 08, 2013
Posts: 9
Hello Joe, I have mentioned the line with red color where exception is occurring.Kindly find it with Line numbers
I have attached the code and error message. when servlet calls the view "xy.jsp"; This
view is not working and throwing a CastException.Kindly help me to resolve it.

Index.html
---------------
1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
3. <html>
4. <head>
5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6. <title>JSP Page</title>
7. </head>
8. <body>
9. <form action="MyController">
10 .<input type="text" name="name"><br>
11. <input type="submit" name="b1" value="submit">
12. </form>
13. </body>
14. </html>

-----------
MyController.java (Servlet Program)
--------------------------------------
1. import java.io.IOException;
2. import java.io.PrintWriter;
3. import javax.servlet.ServletException;
4. import javax.servlet.http.HttpServlet;
5. import javax.servlet.http.HttpServletRequest;
6. import javax.servlet.http.HttpServletResponse;
7. import javax.servlet.http.HttpSession;
8. import myPack.abc;
9. public class MyController extends HttpServlet {
10. protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
11. response.setContentType("text/html;charset=UTF-8");
12. PrintWriter out = response.getWriter();
13. abc d=new abc();
14. d.setName((String)request.getParameter("name"));
15. String s=(String) d.getName();
16. HttpSession session=request.getSession();
17. session.setAttribute("result",s);
18. response.sendRedirect("xy.jsp");
19. }
20. @Override
21. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
22. processRequest(request, response);
23. }
24. @Override
25. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
26. processRequest(request, response);
27. }
28. @Override
29. public String getServletInfo() {
30. return "Short description";
31. }
32. }

-----------------
Business logic class (myPack.abc)
--------------------------------------
1. public class abc {
2. public abc() {
3. }
4. public String name = "";
5. public String getName() {
6. return name;
7. }
8. public void setName(String name) {
9. this.name = name;
10. }
11. }

-----------------------
Presentation Logic (xy.jsp)
----------------------------------
1. <%@page contentType="text/html" pageEncoding="UTF-8" import="myPack.abc" %>
2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
3. <html>
4. <head>
5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6. <title>JSP Page</title>
7. </head>
8. <body>
9. <jsp:useBean id="result" class="myPack.abc" scope="session"></jsp:useBean>
10. <jsp:getProperty property="name" name="result"></jsp:getProperty>
11. </body>
12. </html>
----------------------------------------
Error Page
-------------------
1. HTTP Status 500 -
2. type Exception report
3. message
4. descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
5. exception
6. org.apache.jasper.JasperException: java.lang.ClassCastException: java.lang.String cannot be cast to myPack.abc
7. root cause
8. java.lang.ClassCastException: java.lang.String cannot be cast to myPack.abc
9. note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.0.1 logs.
10. GlassFish Server Open Source Edition 3.0.1
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4369
    
    8

Hi Prateek. Welcome to the Ranch!

Have a look at UseCodeTags - that tells you the best way to make your code readable.

Anyway, I think your problem is a combination of this:

and this

You're adding a String as the "result" session attribute. And then in the JSP you try and say it's a myPack.abc. So the server tries to perform a cast, and that fails because it isn't a myPack.abc
Prateek abhi
Greenhorn

Joined: Sep 08, 2013
Posts: 9
Dear Mattew, You have identified the same line which i have also done. But i believe that the code what i have written is ok. at Jsp Container will look request first and find if any attribute is set or not.It will find
"result". If i declare id="result" then it will work like object of "myPack.abc". but why its not working....i want to fetch the value of result(sent by servlet) at jsp...kindly help.
As i am completely new here hence i dont know about tag codes.Thanks for the link i will follow it
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4369
    
    8

You can't magically convert a String to an instance of another class just by declaring it of that type. If you try to then you'll get a ClassCastException, which is exactly what you're seeing.

If you want to extract a session attribute as a particular type, you need to add it as that type in the first place.
Prateek abhi
Greenhorn

Joined: Sep 08, 2013
Posts: 9

hello Mattew,

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4369
    
    8

No, I don't mean that. That would cause exactly the same problem - you'd be trying to cast a String to an abc.

If you want the session to contain an abc object, the code you need to change is where you add the attribute to the session. You need to add an abc at that point, instead of adding a String. It's your servlet code that needs to change.
Prateek abhi
Greenhorn

Joined: Sep 08, 2013
Posts: 9
Hello Matthew,
I have modified the code as...and its working fine....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: CastException: java.lang.String can not be cast to class