Web Hosting Forum | Lunarpages


*
Welcome, Guest. Please login or register.
Did you miss your activation email?



Login with username, password and session length
October 21, 2014, 07:11:19 AM

Pages: [1]   Go Down
  Print  
Author Topic: Helloworld servlet is throwing class not found exception.  (Read 13847 times)
hatcc3
Space Explorer
***
Offline Offline

Posts: 8


« on: April 24, 2007, 07:41:53 PM »

I am getting the following exception when I tried my first servlet in lunarpages.  I uploaded the file at 11:00pm and tried to view the servlet at 11:30pm.  Here are further details.

http://www.hatccadmin.org/servlet/myServlet.do

500 Servlet Exception
javax.servlet.ServletException: Class `myServlet.do' was not found in classpath.
Classes normally belong in /WEB-INF/classes.
   at com.caucho.server.http.Application.instantiateServlet(Application.java:3198)
   at com.caucho.server.http.Application.createServlet(Application.java:3104)
   at com.caucho.server.http.Application.loadServlet(Application.java:3065)
   at com.caucho.server.http.QServletConfig.loadServlet(QServletConfig.java:435)
   at com.caucho.server.http.Application.getFilterChainServlet(Application.java:2809)
   at com.caucho.server.http.Application.buildFilterChain(Application.java:2765)
   at com.caucho.server.http.Invocation.service(Invocation.java:313)
   at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135)
   at com.caucho.server.http.RunnerRequest.handleRequest(RunnerRequest.java:346)
   at com.caucho.server.http.RunnerRequest.handleConnection(RunnerRequest.java:274)
   at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
   at java.lang.Thread.run(Thread.java:595)


==============================
I have uploaded the class file under / public_html / WEB-INF / classes / com / test /

package com.test;

import java.io.*;

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

public class test extends HttpServlet {
  public void doGet (HttpServletRequest req,
                     HttpServletResponse res)
    throws ServletException, IOException
  {
    PrintWriter out = res.getWriter();

    out.println("Hello, world!");
    out.close();
  }
}
===================================
This is my web.xml entry under /WEB-INF

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <servlet>
        <servlet-name>myServlet</servlet-name>
        <servlet-class>com.test.test</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

</web-app>
Logged
RandyT
Guest
« Reply #1 on: April 24, 2007, 08:29:10 PM »

OK, try it now Wink


Hope this helps,
RandyT
Logged
RandyT
Guest
« Reply #2 on: April 24, 2007, 08:38:30 PM »

If you are having errors on your other pages you may want to check this out: https://desk.lunarpages.com/faq.php?do=article&articleid=315

I hope this was helpful,
RandyT
Logged
hatcc3
Space Explorer
***
Offline Offline

Posts: 8


« Reply #3 on: April 25, 2007, 04:01:04 AM »

Randy,

Thanks the servlet is working.  But, I do not understand what was done?  I thought the issue was LunarPages had not enabled servlets for my domain.  I am keen to know the changes done by you. 

Also you have referred me to Log4j FAQ.   Could you please help me understand how this is related to the Helloworld servlet. 

Thanks,
hatcc
Logged
katrina1
Guest
« Reply #4 on: April 25, 2007, 04:17:13 AM »

JSP is enabled on the account. The paths specified in your web.xml files were wrong. A CSR corrected them for you. I believe you have Log4j installed and it is not installed correctly.
Logged
hatcc3
Space Explorer
***
Offline Offline

Posts: 8


« Reply #5 on: April 25, 2007, 10:52:14 AM »

Thanks for a very prompt response. 

For the benefit of the forum users.......

With regard to Helloworld servlet following was the change done to web.xml. 

I had
<url-pattern>*.do</url-pattern>


this was corrected to
<url-pattern>/servlet/*.do</url-pattern>

With regard to Log4J following are the corrections done to web.xml.

I had
<servlet-class>logging.Log4jInit</servlet-class>

this was corrected to.
<servlet-class>com.logging.Log4jInit</servlet-class>
Logged
katrina1
Guest
« Reply #6 on: April 25, 2007, 08:47:47 PM »

Thank you hatcc3! We truly appreciate you posting the corrections and taking the time to help the rest of the forum users who may see this. Users like you are what make forums so very helpful.  Clapping
Logged
mark_koscak
Space Explorer
***
Offline Offline

Posts: 6


« Reply #7 on: May 31, 2007, 08:20:57 PM »

A useful post and I hope I can add to it.

I have three hello world servlets sitting in different directories.

HelloWorldExamplebb.class sitting in
/public_html/WEB-INF/classes/

HelloWorldExample.class sitting in
/public_html/WEB-INF/classes/helloworld/

and test.class sitting in
/public_html/WEB-INF/classes/com/test/

My web.xml file which runs all three is
Code:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <servlet>
        <servlet-name>newServlet</servlet-name>
        <servlet-class>helloworld.HelloWorldExample</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>newServlet</servlet-name>
        <url-pattern>/servlet/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>myServlet</servlet-name>
        <servlet-class>com.test.test</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myServlet</servlet-name>
        <url-pattern>/servlet/*</url-pattern>
    </servlet-mapping>


</web-app>

The key in setting up the web.xml file is to declare your java class with the appropriate package.

You don't have to worry about packages if you are just placing your java class in the /public_html/WEB-INF/classes directory see code below no package declared
Code:
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id: HelloWorldExample.java 267129 2004-03-18 16:40:35Z jfarcand $
 *
 */

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * The simplest possible servlet.
 *
 * @author James Duncan Davidson
 */

public class HelloWorldExamplebb extends HttpServlet {


    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        //ResourceBundle rb =
          //  ResourceBundle.getBundle("LocalStrings",request.getLocale());
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");

    String title = "Hello World top level dir"; //rb.getString("helloworld.title");

    out.println("<title>" + title + "</title>");
        out.println("</head>");
        out.println("<body bgcolor=\"white\">");

// note that all links are created to be relative. this
// ensures that we can move the web application that this
// servlet belongs to to a different place in the url
// tree and not have any harmful side effects.

        // XXX
        // making these absolute till we work out the
        // addition of a PathInfo issue

    out.println("<a href=\"../helloworld.html\">");
        out.println("<img src=\"../images/code.gif\" height=24 " +
                    "width=24 align=right border=0 alt=\"view code\"></a>");
        out.println("<a href=\"../index.html\">");
        out.println("<img src=\"../images/return.gif\" height=24 " +
                    "width=24 align=right border=0 alt=\"return\"></a>");
        out.println("<h1>" + title + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

When I place my Hello World test in the /public_html/WEB-INF/classes/helloworld/ directory, I must declare HelloWorldExample.java which compiles to HelloWorldExample.class to be a part of package helloworld; note the package and how web.xml refrences it
Code:
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id: HelloWorldExample.java 267129 2004-03-18 16:40:35Z jfarcand $
 *
 */
package helloworld;

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * The simplest possible servlet.
 *
 * @author James Duncan Davidson
 */


public class HelloWorldExample extends HttpServlet {


    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        //ResourceBundle rb =
          //  ResourceBundle.getBundle("LocalStrings",request.getLocale());
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");

    String title = "Hello World mod nested deep"; //rb.getString("helloworld.title");

    out.println("<title>" + title + "</title>");
        out.println("</head>");
        out.println("<body bgcolor=\"white\">");

// note that all links are created to be relative. this
// ensures that we can move the web application that this
// servlet belongs to to a different place in the url
// tree and not have any harmful side effects.

        // XXX
        // making these absolute till we work out the
        // addition of a PathInfo issue

    out.println("<a href=\"../helloworld.html\">");
        out.println("<img src=\"../images/code.gif\" height=24 " +
                    "width=24 align=right border=0 alt=\"view code\"></a>");
        out.println("<a href=\"../index.html\">");
        out.println("<img src=\"../images/return.gif\" height=24 " +
                    "width=24 align=right border=0 alt=\"return\"></a>");
        out.println("<h1>" + title + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

Similarly test.java which sits in /public_html/WEB-INF/classes/com/test/ must declare in test.java that it is part of package com.test; (Thanks to Hatcc3 for providing this example) Again note the package and how web.xml references it.
Code:

package com.test;

import java.io.*;

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

public class test extends HttpServlet {
  public void doGet (HttpServletRequest req,
                     HttpServletResponse res)
    throws ServletException, IOException
  {
    PrintWriter out = res.getWriter();

    out.println("Hello, world!");
    out.close();
  }
}
Then in the web.xml see above file use the dot notation relative to /public_html/WEB-INF/classes to access
the servlet functionality you desire.

This should provide enough of a detailed example to allow you to experiment and add new packages to you servlet class tree.

Happy hacking  Smile


PS A http://www.caucho.com/resin-3.0/config/webapp.xtp#servlet-mapping useful link on web.xml files.
Logged
remmons
Newbie
*
Offline Offline

Posts: 1


« Reply #8 on: October 24, 2007, 06:13:10 AM »

Hi,

I know this topic has been dead for a long time, but I hope someone is still listening since I am having a similar problem.  I am trying to run a test servlet at:

    http://www.amexspecialist.com/HelloWorldServlet.do

but I get the following error message:

I get the following error:

    Not Found
    The requested URL /HelloWorldServlet.do was not found on this server.

    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the   
    request.
    Apache/1.3.39 Server at amexspecialist.com Port 80

Here is my java class location:

    public_html/WEB-INF/classes/HelloWorldServlet.class

Here is my Deployment Descriptor public_html/WEB-INF/web.xml:

    <web-app xmlns="http://caucho.com/ns/resin">

    <servlet>
        <servlet-name>HelloWorldServletID</servlet-name>
        <servlet-class>HelloWorldServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWorldServletID</servlet-name>
        <url-pattern>/HelloWorldServlet.do</url-pattern>
    </servlet-mapping>

</web-app>

Can anyone see what the problem is?

Logged
scott46
Newbie
*
Offline Offline

Posts: 1


« Reply #9 on: November 07, 2010, 08:27:31 AM »

I know it's sometimes frowned upon bringing old threads back, but this is a helpful thread and I have a useful bit of info to add to it.  I've discovered that you can get the same error by having the Servlet class files compiled with a newer Java version than is supported by the server.  I wanted to add this little bit of info because, when this happens, the error message is misleading. 

In my case, I accidentally placed a copy of my Servlet compiled with Java 6 on my account which appears to only have Java 5 available to it.  When I recompiled with Java 5, the Servlet worked.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to: