This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Tomcat and the fly likes beginner question:error 404 servlet not available in Tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "beginner question:error 404 servlet not available in Tomcat" Watch "beginner question:error 404 servlet not available in Tomcat" New topic
Author

beginner question:error 404 servlet not available in Tomcat

may Lee
Ranch Hand

Joined: Nov 20, 2003
Posts: 68
I have apache and tomcat4.1.30 on redhat 9 box.
I made a webapp.conf on Apache,change server.xml also.
I can view http://localhost/examples, see the servlet and jsp examples.
Then I put welcom.html on my apache home folder:
/var/www/html/welcom.html,
I put the servlet.class at /webapps/examples/hello.class and also /webapps/examples/servlets/hello.class

Then I access http://192.168.0.2/welcom.html, when I click "get html", the error is:
The requested resource (/examples/servlets/servletTest) is not available
or
The requested resource (/examples/servletTest) is not available

What else change I should make? Thanks very much!

Details of my configuration file and source code:

webapp.conf under /etc/httpd/conf.d
LoadModule webapp_module modules/mod_webapp.so
ServerName localhost:8008
WebAppConnection warpConnection warp localhost:8008
webAppDeploy examples warpConnection /examples

server.xml
<Server port="8005" shutdown="SHUTDOWN" debug="0">

<Service name="Tomcat-Apache">

<!-- Define a webapp Connector for Apache and Tomcat on port 8008 -->
<Connector className="org.apache.catalina.connector.warp.WarpConnector"
port="8008" minProcessors="5" maxProcessors="75"
enableLookups="true" appBase="webapps"
acceptCount="10" debug="0"/>

<Engine className="org.apache.catalina.connector.warp.WarpEngine" name="Apache" debug="0">

<Logger className="org.apache.catalina.logger.FileLogger"
prefix="apache_log." suffix=".txt"
timestamp="true"/>

<Realm className="org.apache.catalina.realm.MemoryRealm" />

</Engine>

</Service>

</Server>

welcom.html
<HTML>

<HEAD>
<TITLE>
Servlet example
</TITLE>

</HEAD>

<BODY>
<FORM ACTION="http://localhost/examples/servlets/servletTest" METHOD="GET">
<p> Click button</p>
<INPUT TYPE="submit" VALUE="GET HTML">
</FORM>



</BODY>

</HTML>

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

public class servletTest extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
PrintWriter output;
res.setContentType("test/html");
output=res.getWriter();

StringBuffer buf=new StringBuffer();
buf.append("<HTML><HEAD><TITLE>\n");
buf.append("< A servlet test");
buf.append("</TITLE></HEAD><BODY>\n");
buf.append("<H1>Welcom!</H1>\n");
buf.append("</BODY></HTML>\n");
output.println(buf.toString());
output.close();
}
}
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12758
    
    5
put the servlet.class at /webapps/examples/hello.class and also /webapps/examples/servlets/hello.class


Why in the world did you do that?

Couldn't you see that the class files in /examples/ are all under WEB-INF - thats required by the Servlet API.
See
this discussion on the JR FAQ for why you should NOT be using the /servlets/ nomenclature for serious work.
Bill
may Lee
Ranch Hand

Joined: Nov 20, 2003
Posts: 68
I try another way after reading your suggestion,still problem, please have a look for my question.

I made a hello.class (as followings) and put this class at CATALINA_HOME\webapps\examples\WEB-INF\classes\, but when I put
http://localhost/examples/servlet/hello, it asked my to download this file.
While I can view the other servlet examples from Tomcat by the same way such as
http://localhost/examples/servlet/RequestInfoExample, Why? Where to change? THanks!

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

public class hello extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
PrintWriter output;
res.setContentType("test/html");
output=res.getWriter();
output.println("<HTML><HEAD><TITLE>Hello</TITLE></HEAD>");
output.println("<BODY>");
output.println("Hello Test");
output.println("</BODY></HTML>");
}
}
may Lee
Ranch Hand

Joined: Nov 20, 2003
Posts: 68
In the \webapps\examples/WEB-INF\web.xml
they use

<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>

So I don't need to an explicit mapping, right?

Sign so many problem to begin servlet
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

you don't *need* an explicit mapping, if the servlet invoker has been enabled, but you may need to put your servlets in packages (everything you write for web applications should be in packages).

You *should* be using an explicit mapping, even though it looks like you don't need to.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

To clarify 'packages'...



The complied class should then be placed at:

WEB-INF/classes/foo/Hello.class

And the part of your web.xml that you'll need to add is:

And you access it like so:

http://domain.goes.here/contextNameHere/hello

If you are using the default context, then omit /contextNameHere
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: beginner question:error 404 servlet not available in Tomcat
 
Similar Threads
What is Tomcat virtual host, What is different from the Apache Tomcat?
tomcat 4 - apache - mod_webapp problem
Security Contraints setup..
Tomcat 6 as a service
DataSource : No suitable driver...?????