aspose file tools*
The moose likes Servlets and the fly likes Servlet HelloWorld problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet HelloWorld problem" Watch "Servlet HelloWorld problem" New topic
Author

Servlet HelloWorld problem

christopher gray
Greenhorn

Joined: Mar 22, 2005
Posts: 7
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.


SCJP 1.2; CCNA; MCSA
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Can you post the code to your servlet?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
christopher gray
Greenhorn

Joined: Mar 22, 2005
Posts: 7
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

Joined: Dec 11, 2004
Posts: 13410

and does welcome.html work?
[ March 27, 2005: Message edited by: Ben Souther ]
christopher gray
Greenhorn

Joined: Mar 22, 2005
Posts: 7
yes the html file seems to be working ok
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Mar 22, 2005
Posts: 7
I did change it to port 80
Sandeep Saha
Greenhorn

Joined: Mar 28, 2005
Posts: 5
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
Vijay Kiran
Greenhorn

Joined: Nov 18, 2004
Posts: 21
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 ]

Vijay<br />I blog, therefore I am<br /><a href="http://www.vijaykiran.com" target="_blank" rel="nofollow">www.VijayKiran.com</a>
christopher gray
Greenhorn

Joined: Mar 22, 2005
Posts: 7
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

Joined: Nov 18, 2004
Posts: 21
Have you created a context for your 'jspbook'?
Vijay Kiran
Greenhorn

Joined: Nov 18, 2004
Posts: 21
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

Joined: Mar 22, 2005
Posts: 7
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet HelloWorld problem
 
Similar Threads
Starting out on Servlets - from Head First Servlets and JSP
starting two servlets in load on startup
HelloWorld servlet not working
Context tag
Problem with webapplication during deploying