• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet HelloWorld problem

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

I'm sure this is very simple but I have no experience with servlets and I'm having problems getting a HelloWorld servlet to run after following the text from Servlets and JSP - (Falkner, Jones).

HelloWorld.java is in ..\jspbook\WEB-INF\classes\com\jspbook as per the books example, I have produced web.xml and placed in the WEB-INF folder which is as follows

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4">
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.jspbook.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>welcome.html</welcome-file>
</welcome-file-list>
</web-app>

I run ant and says build successful, compiled 1 source file, which creates HelloWorld.class in the same folder as the source file.

Now when I try and hit http://127.0.0.1/jspbook/HelloWorld I get the following error report from tomcat HTTP Status 404 - Servlet HelloWorld is not available

the tomcat logs contain this exception
java.lang.ClassNotFoundException: com.jspbook.HelloWorld

I really want to start making headway with this book so any help is greatly appreciated.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the code to your servlet?
 
christopher gray
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my servlet code is as follows

package com.jspbook;

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

public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
}
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and does welcome.html work?
[ March 27, 2005: Message edited by: Ben Souther ]
 
christopher gray
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes the html file seems to be working ok
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What app server are you using?
If it's Tomcat, unless you've changed it, you'll need to specify port 8080.

http://127.0.0.1:8080/jspbook/HelloWorld
 
christopher gray
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did change it to port 80
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The HelloWorld class (Servlet) will be under directory

WEB-INF/classes/com/jspbook/HelloWorld.class

According to the discreptor you have written.


and the directory in which you are putting the web-application is not clear to me. If you are doing it in the root then no problem. But if you are doing it in some directory under root then give the extension of the directory as .war eg. testing.war would be the directory name.

I have worked on web-logic and jboss. Both follows the same rule.


The Directory structure will be

testing.war
|___WEB-INF
| |_____classes(Directory)(Servlets will be kept here)
| |_____ web.xml (Deployment Discreptor)
|
|
|___JSP Files
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't matter where your .java files are. You need create the war file from a directory which is having the following structure
test
|_WEB-INF
| |_web.xml (Deployment Discriptor)
| |__classes(Directory)
| |__com
| |___jspbook
| |____HelloWorld.class
|
|_All your JSP Files
[ March 28, 2005: Message edited by: Vijay Kiran ]
 
christopher gray
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't got up to a point which tells me to create a .war file yet. This is a hello world servlet. ie. It should be very simple, I have followed the instructions accurately as far as I can tell by moving the source code to the directory (in full) as follows:

c:\program files\Apache Software Foundation\Tomcat 5.0\webapps\jspbook\WEB-INF\classes\com\jspbook\HelloWorld.java

The book then doesn't actually say to run ant from the WEB-INF directory but I thought that it must need that as it needs to compile the source code, I did that and it produces HelloWorld.class as expected in the same directory.

at this point tomcat has been stopped and restarted as part of the ant operation however I still get the 404 error. When hitting the URL previously mentioned and the tomcat logs indicate that the class file isn't found. I'm obviously missing something pretty major...

but thanks for the help so far.
 
Vijay Kiran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you created a context for your 'jspbook'?
 
Vijay Kiran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are not deploying a WAR file, then you might have to create a context using tomcat manager, or

move your application into ROOT context, i.e., keep your com\jspbook\HelloWorld.class in
c:\program files\Apache Software Foundation\Tomcat 5.0\webapps\ROOT\WEB-INF\classes\com\jspbook\

and add your servlet xml from web.xml to the web.xml found in
c:\program files\Apache Software Foundation\Tomcat 5.0\webapps\ROOT\WEB-INF\web.xml

Then your servlet should work fine
[ March 28, 2005: Message edited by: Vijay Kiran ]
 
christopher gray
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
strange, maybe ant did not stop and restart tomcat even though it said it did, because I just restarted it manually and voila.. Hello World!

thanks for the help everyone, I will know in future to restart tomcat manually
 
Your mind is under my control .... your will is now mine .... read this tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic