"Error: 500 Internal Servlet Error" question

Hi !

I   am trying to run my first Java servlet (as a matter of fact it's the first example from
"Professional Java Server Programming" book).

1. I compile my HelloWorld.java
2. I place it at jsdk2.1\webpages\WEB-INF\servlets path.
3. I start startserver.bat
4. I enter the following URL in browser : http://localhost:8080/servlet/HelloWorld

Here is the error that I get :

Error: 500
Internal Servlet Error:
java.lang.ClassCastException: HelloWorld
      at com.sun.web.core.ServletWrapper.loadServlet(ServletWrapper.java:90)
      at com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:109)
      at com.sun.web.core.InvokerServlet.service(InvokerServlet.java:169)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:840)
      at com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:140)
      at com.sun.web.core.Context.handleRequest(Context.java:375)
      at com.sun.web.server.ConnectionHandler.run(Compiled Code)

What is wrong ?
 
I am using Windows NT 4 SP5, JDK 1.2, JSDK 2.1

Thanks a lot !

john1313Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sgomsCommented:
i guess its a servlet registration problem.

how abt,

1. Compile HelloWorld.java

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;


/**
 * This is a simple example of an HTTP Servlet.  It responds to the GET
 * and HEAD methods of the HTTP protocol.
 */
public class HelloWorld extends HttpServlet
{
    /**
     * Handle the GET and HEAD methods by building a simple web page.
     * HEAD is just like GET, except that the server returns only the
     * headers (including content length) not the body we write.
     */
    public void doGet (
      HttpServletRequest      request,
      HttpServletResponse      response
    ) throws ServletException, IOException
    {
      PrintWriter            out;
      String                  title = "Simple Servlet Output";

      // set content type and other response header fields first
        response.setContentType("text/html");

      // then write the data of the response
      out = response.getWriter();

        out.println("<HTML><HEAD><TITLE>");
      out.println(title);
      out.println("</TITLE></HEAD><BODY>");
      out.println("<H1>" + title + "</H1>");
      out.println("<P>This is output from HelloWorld.");
      out.println("</BODY></HTML>");
      out.close();
    }
}


2. Have it in a directory c:\myservlets
3. in ur classpath have set Classpath=c:\jsdk2.0\bin
4. start ur servletrunner
>servletrunner -p 8080 -d c:\myservlets

from ur browser use,
http://localhost:8080/servlet/HelloWorld

does this work?

-sgoms
0
Ravindra76Commented:
Hi john1313,

1.First check the servlet is public class or not.

2.Check ths class name and file name.

3.IF still problem, it is registration problem

BEst of luck
0
g_senthilkumarCommented:
hi,
  If you are using servletrunner available in JSDK means you have to register you servlet first.Go the servlet.properties file and register your servlet there.
     After registration put servlet file in appropriate directory where the servletrunner will look for servlets class files.
      Dont forget to declare your class as public.
    I hope this will work out.
Best of luck
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

john1313Author Commented:
Thanks for all your answers, but I still have a problem...

1. I checked that the servlet class is public
2. I checked the name of the class

Sgoms :
I use JSDK 2.1, there is no bin directory there. Maybe be because of
this the browser is unable to find your servlet.

Concerning servlet.properties :
I have to enter the following enteris :

# <servletname>.code=<servletclass>
# <servletname>.initparams=<name=value>,<name=value>

What is the <servletname> ? My classname is HelloWorld.class. What
should be the <servletname> ?
I have the Sun's example which works. There are the following enteries there :
snoop.code=SnoopServlet
snoop.initparams=initarg1=foo,initarg2=bar

Ans yet another question :
Do I need to add some ClassPath of JSDK. According to my book, in JDK 1.2 I have to move servlet.jar to JDK/JRE/LIB/EXT. Do I need something else ?
Does the  ClassPath palys the role during the  compilation and running as well or only during the compilation ?

Thanks
0
Ravindra76Commented:

Hi,

Once post your code.

I think classcastexception menas it is not extending servlet

0
Ravindra76Commented:
Hi john123,

1.class path will be like this

c:\jsdkhome\lib\jsdk.jar

In JSDK2.0, servletrunner will try to load from

c:\jsdkhome\examples directory.

To run your servlets in yourdirecotyr

servletrunner -d c:\yourdir

Best of luck

2.HelloWorld.code = HelloWorld

Your your servlet alias is Hello

Hello.code = HelloWorld
and request is

http://yourserver:port/servlets/Hello

Best of luck

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
john1313Author Commented:
Finally it works ! Thanks for all your answers !

Guess, what was the problem ?!

I begun to rename my servlet to HelloWorld ->Hello or HelloWorld ->HelloWorld1 and it worked....

I have some other HelloWorld.class in my ClassPath...:-):-):-)


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.