Results 1 to 5 of 5

Thread: http status 500

  1. #1
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

    Default http status 500

    i am working on a web application called "fast learn", an online study center and ive encountered the following error for the code below.. plz help
    HTTP Status 500 -

    type Exception report

    message

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

    exception

    java.lang.ClassCastException: course cannot be cast to java.util.List
    ListCourses.doGet(ListCourses.java:51)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    org.netbeans.modules.web.monitor.server.MonitorFil ter.doFilter(MonitorFilter.java:393)
    //code
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.util.*;


    public class ListCourses extends HttpServlet {

    private List listcourse=null;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    listcourse=new LinkedList();
    listcourse.add(new course("c#"));
    listcourse.add(new course("asp.net"));
    String pageTitle="list of courses";
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>"+pageTitle+"</title>");
    out.println("</head>");
    out.println("<body bgcolor='white'>");
    out.println("<!--Page Heading-->");
    out.println("<table border='1' cellpadding='5' cellspacing='0' width='400'>");
    out.println("<tr bgcolor='#CCCCFF' align='center' valign='center' height='20'>");
    out.println("<td> <h3>"+pageTitle+"</h3> </td>");
    out.println("</tr>");
    out.println("</table>");
    out.println("<p>");
    out.println("list of courses are");
    out.println("</p>");
    out.println("<ul>");
    Iterator items=listcourse.iterator();
    while(items.hasNext())
    {
    List list=(List)items.next();
    out.println("<li>"+course.getTitle()+"</li>");
    }
    out.println("</ul>");
    out.println("</body>");
    out.println("</html>");
    }
    }

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Java Code:
            Iterator items=listcourse.iterator();
            while(items.hasNext())
            {
                List list=(List)items.next();
    Here items.next() will be an Object of type course, and you are trying to assign it to list, which is type List. Can't do that. I guess you probably meant to do something like:
    Java Code:
                list.add(items.next());
    -Gary-

  3. #3
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

    Default

    thnx a lot :)

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,181
    Rep Power
    20

    Default

    You also have a Bug In Waiting in that Servlet.

    Servlets should not have state. That is, in general, a Servlet should not have any attributes. Yours does, in the form of your listcourse attribute.

    You might ask why, but it's because a single instance of a Servlet will be handling multiple requests at one time, so need to be thread safe.

    So, say a request comes in and starts processing and gets to a point just after adding stuff to the list, but before displaying. Then another request comes in to be processed by this Servlet and gets as far as the new LinkedList() bit. This wipes the stuff in listcourses out. Now the original thread (request) takes over again and displays...well...nothing, because it's now an empty list.

    So don't put attributes onto your Servlets.

  5. #5
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

Similar Threads

  1. HTTP Status 500(problem)
    By waqar100 in forum NetBeans
    Replies: 1
    Last Post: 06-30-2010, 12:54 PM
  2. Replies: 7
    Last Post: 11-11-2009, 09:29 PM
  3. what does this means HTTP Status 500 -
    By gissah in forum New To Java
    Replies: 0
    Last Post: 03-28-2009, 04:27 AM
  4. Http Status 404 - LoginServlet.do
    By mbalas2 in forum Java Servlet
    Replies: 4
    Last Post: 03-05-2009, 06:52 PM
  5. problem with HTTP Status 404
    By orkun in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 02-05-2009, 10:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •