Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Tomcat and Servlet

  Asked By: Adelina    Date: Aug 24    Category: Java    Views: 1103
  

I have very basic question. I am trying to run the servlet through
Tomcat but its not running. can you please help me.

Here what I am doing.

I have a servlet file,like follows:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class a1 extends HttpServlet{
public void doGet(HttpServletResponse req,HttpServletResponse
resp) throws ServletException,IOException{
PrintWriter pw=resp.getWriter();
pw.println("<html><body>Hello World!</body></html>");
pw.close();
}
}

web.xml is follows:
<?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>a1</servlet-name>
<servlet-class>a1</servlet-class>
</servlet>
</web-app>

I use Tomcat5.0,and I put a1.class into \Tomcat 5.0\webapps\test\WEB-
INF\classes,and I have add a sentence into server.xml like follows:

<Context path="/test" docBase="test" debug="0"/>
restart tomcat
then run http://localhost:8080/test/servlets/a1

I got error says
HTTP Status 404 - /test/servlets/a1

type Status report

message /test/servlet/a1

description The requested resource (/test/servlet/a1) is not
available.

Can Anyone please help me with this. I tried but did not get any
luck.

Share: 

 

8 Answers Found

 
Answer #1    Answered By: Logan Bouchard     Answered On: Aug 24

Your web.xml file  should be like:

<web-app>
<servlet>
<servlet-name>a1</servlet-name>
<servlet-class>a1</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>a1</servlet-name>
<url-pattern>/a1</url-pattern>
</servlet-mapping>

 
Answer #2    Answered By: Aidyn Smith     Answered On: Aug 24

Thanks for reply, But I am still having the same problem.

 
Answer #3    Answered By: Abbad Akhtar     Answered On: Aug 24

Well, all this got me a little scared because I
hadn't tried running  compiled servlets under
Tomcat yet. Gave it a try and having the same
difficulty. One of the weaknesses of Java
technologies is that the learning curve can be
horrifying compared to the alternatives. I
remember when I finally got JDBC to work with
my JSP pages, I was so elated that I was dancing
around the house. I find that the available
documentation is either inadequate, conflicting,
or erroneous, and in some cases all three.

I'll tinker with this some more as time
permits and get back if I find out anything,
unless -- preferably -- someone else comes in
with an answer first.

 
Answer #4    Answered By: Cais Nguyen     Answered On: Aug 24

OK, a little more info. Aside from a couple of
syntax errors in my xml <blush> I checked the log
and found that was initializing class  repository to
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\_
Don't know why or how to change that path, but by
moving the servlet  there, it could find it. I'm sure
your path will be different; check the log to find out.

But the problem is not solved. It's in a package
(I named mine testservlet so it's testservlet.testservlet)
and the loader complains that the name doesn't match.
It's wanting testservlet/testservlet. I tried moving
it to a testservlet subdirectory, renaming my servlet
class in web.xml to testservlet.testservlet, and both,
still to no avail. Might try building it outside of a
package, but there has to be a way to make this work.

C'mon folks. Somebody out there has to know about this
than we do. Pretty please.... :D

I think Java would wipe out Microsoft if it weren't so
cryptic.

 
Answer #5    Answered By: Jaspreet Kapoor     Answered On: Aug 24

With the web.xml below,
you should call the servlet  using this url:
http://localhost:8080/test/a1

not
http://localhost:8080/test/servlets/a1

 
Answer #6    Answered By: Elaine Stevens     Answered On: Aug 24

Did you ever get this figured out. With Leonard's
prodding, I finally got mine to work. The hitch
was that the class  was in a package of the same name
(a standard practice) and both the class name in
web.xml and the path to the class must be fully
qualified. In your case (if this is your problem)
you would use:

<servlet>
<servlet-name>a1</servlet-name>
<servlet-class>a1.a1</servlet-class>
</servlet>

and your class should be at <appBase>/classes/test/a1/a1.class

When you have everything like this, restart Tomcat
and try again. As Leonard said, with your servlet
mappin you should invoke it with:

http://localhost:8080/test/a1

If this doesn't help, we can keep plugging. I'm still
a little concerned about how that "test" subdirectory
plays out. I didn't have that complication. As you
can see, I'm no Java guru myself.

 
Answer #7    Answered By: Alexis Castillo     Answered On: Aug 24

I found the solution of the problem. All i had to do is.. change
my web.xml file.


in my Webapps folder I create folder test

test/WEB-INF/
In Web-Inf I have folder classes/a1.java and a1.class file
I also have web.xml file  in WEB-INF folder

here is web.xml file
<?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>

<display-name>Servlet 2.4 Examples</display-name>
<description>
Servlet 2.4 Examples.
</description>


<!-- Define servlets that are included in the example
application -->

<servlet>
<servlet-name>a1</servlet-name>
<servlet-class>a1</servlet-class>
</servlet>


<servlet-mapping>
<servlet-name>a1</servlet-name>
<url-pattern>/servlet/a1</url-pattern>
</servlet-mapping>
</web-app>

And I also add one line in tomcat/conf/server.xml
<Context path="/test" docBase="test" debug="0"/>
Add this life before </Host> tag

 
Answer #8    Answered By: Dot net Sachin     Answered On: Aug 24

No, you should put it on <appBase>/WEB-INF/classes/a1/a1.class

 
Didn't find what you were looking for? Find more on Tomcat and Servlet Or get search suggestion and latest updates.




Tagged: