Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

beginner question:error 404 servlet not available in Tomcat

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!