Requested Resource was not found?
I finally got my classpath and everything set up for j2ee. Now I'm running into a problem whenever I try to run tomcat, and then try to run the servlet. It says "The requested resource is not available".
I typed this in as the URL: http://localhost:8080/ch1/Serv1
Here is the java code:
Code:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Ch1Servlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
{
PrintWriter out = response.getWriter();
java.util.Date today = new java.util.Date();
out.println("<html>" +
"<body>" +
"<h1 align=center>Chapter 1 Servlet></h1>" +
today.toString() +
"</body>" +
"</html>");
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
XML Code..Although I have no clue what it's for, and what I wrote:
Code:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>Chapter1 Servlet</servlet-name>
<servlet-class>Ch1Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Chapther1 Servlet</servlet-name>
<url-pattern>/Serv1</url-pattern>
</servlet-mapping>
</web-app>
Please help me out!
Re: Requested Resource was not found?
First off, where are you saving the codes? Is it in the %CATALINA_HOME%/webapps/ROOT? All you need is, to add the Ch1Servlet.class in %CATALINA_HOME%/webapps/ROOT/WEB-INF/classes and just access it using http://localhost:8080/servlet/Ch1Servlet.
Now, you have a web.xml you are talking... So I assume you're saving your java files and the Ch1Servlet.class somewhere in your hard drive. See http://vbforums.com/showthread.php?t=337553 and lemme know what you'll find out.
Re: Requested Resource was not found?
I can't even get it to run when I put it in the default directory like you said. I did have my own directory like this:
Code:
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ch1\WEB-INF\Web.xml
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ch1\WEB-INF\classes\Ch1Servlet.java
I updated my XML file to look like yours:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>Ch1Servlet</servlet-name>
<servlet-class>Ch1Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Ch1Servlet</servlet-name>
<url-pattern>/Ch1Servlet</url-pattern>
</servlet-mapping>
</web-app>
Then I ran the server, and typed this in:
http://localhost:8080/Ch1Servlet/Ch1Servlet
I also added the Context tag in the server.xml file.
Thanks for the help you've given me neb. I've tried several forums, including the java.sun forum, but they aren't very nice people. I've searched the web for almost three days now, and I can't find nothing. I bought this book called Head First Servlets and JSP, but it's for certification and doesn't teach from the ground up. If you don't mind, along with helping me with this, could you post some good tutorials?
Re: Requested Resource was not found?
Here's what I've done testing your code. Project folder is in
VB Code:
%CATALINA_HOME%/webapps/ch1
+ WEB-INF
+ classes
+ Ch1Servlet.class
+ src
+ Ch1Servlet.java
+ web.xml
I'm sorry about my web.xml, it was 2.3 specification. Yours is the new one. Inside it btw, is
VB Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>Chapter1 Servlet</servlet-name>
<servlet-class>Ch1Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>[color=red]Chapter1 Servlet[/color]</servlet-name>
<url-pattern>/Serv1</url-pattern>
</servlet-mapping>
</web-app>
Note that I colored it red, servlet-name should match the servlet-name mapped. Yours is misspelled. Now, edit your %CATALINA_HOME%/conf/server.xml and add the Context path,
VB Code:
. . .
<Context path="/ch1" reloadable="true" docBase="C:\java\tomcat\tomcat 5.5\webapps\ch1" />
</Host>
</Engine>
</Service>
</Server>
ch1 now is our path which we access by http://localhost:8080/ch1 and the document base is your %CATALINA_HOME%/webapps/ch1. I have a different location on my tomcat installation so you should change it to point in your ch1 project.
Back in server.xml, we have our servlet-name Chapter 1 Servlet mapped as Serv1 so accessing it by http://localhost:8888/ch1/Serv1 will display your Ch1Servlet servlet.
Btw, I don't have good online tutorials on this stuff. I have our Tomcat book here by Wrox (Wrox - Professional Apache Tomcat 5) and Core-Servlets-and-JSP O'Reilly. I can't mail them though, 10.+ MB and 11.+ MB are the sizes respectively.
Edit: You are correct saying java.sun.com forums aren't nice people. I was there. Hehehe.
Re: Requested Resource was not found?
I followed everything, and it still didn't work. There has to be something simple, or something to this new version of tomcat that's stopping me. Do you think I should place the servlet in a package? If so, I would change the <servlet-classname> tag to something like this:
<servlet-classname>Package.Filename</servlet-classname>
Is that correct?
By the way, thank you SOOO much for the help so far. Not many people would be willing to offer the amount of help you have. I wish there was some other way I could thank you besides a simple thanks, and added reputation.
Re: Requested Resource was not found?
I think there is something I need to mention. Everytime I try to install Tomcat 5.5, I get this error message in the middle saying Tomcat 5.5 service could not be installed, do you wish to ignore? I always click yes to it.
Re: Requested Resource was not found?
I'm switching to tom 4 right now. I'll set that up and see how it goes. I've been reading on the internet that a lot of people are having problems with tom 5.5, and it's recomended to stay with 5, or 4.
Re: Requested Resource was not found?
Ok, it worked with tomcat 4.1. I guess that means that it was just the version. Sorry for putting you through so much trouble, I know it was getting annoying.
Anyways, I was wondering if you could help me with one more thing...
What do the lines in bold/red mean?
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>webtest</servlet-name>
<servlet-class>webtest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>webtest</servlet-name>
<url-pattern>/webtest</url-pattern>
</servlet-mapping>
</web-app>
Re: Requested Resource was not found?
I don't know much about XML so I can't help you explaining that thing. It's Document Type Definition that Suns pre-defined. Check http://www.w3schools.com/dtd/default.asp for more details. I'm sorry can't help much.
Btw, it's ok (you're not a bother or something)... Keep it up.
Edit: Also check http://java.sun.com/dtd/web-app_2_3.dtd to check the definition.
Re: Requested Resource was not found?
Had you actually compiled the .java file? It doesn't happen automatically.
Re: Requested Resource was not found?
Also, setting a Context path needs a restart on Tomcat (I could be wrong).
Re: Requested Resource was not found?
It does. Any change to server.xml does, unless done via the Administration servlet.
Re: Requested Resource was not found?
I did compile the source code, and placed it in the correct directory and everything too. I also set the classpath to the servlet api file, and it still didn't work and I changed it when I installed the later version of Tomcat. I did read on the internet that a lot of people were having trouble with 5.5, and they called it "uncharted territory", and shouldn't use it unless you want to sort out all the problems. I would like to use 5.5, but I don't want to deal with all that mess again. I believe I might email apache and ask them, or check out there documentation to see what the problem is.