aspose file tools*
The moose likes Servlets and the fly likes unable to run servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "unable to run servlets" Watch "unable to run servlets" New topic
Author

unable to run servlets

anubha goel
Greenhorn

Joined: Jul 06, 2007
Posts: 22
hi,
i am not able to run servlet..i am able to compile them but
when i run it in explorer i get error 404...
please help....

Thanks in advance
Anubha.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Welcome to JavaRanch!

In an effort to help you get the most from our forums, we've compiled a
list of tips for asking questions here. You can find the list in our
FAQ section here.
In particular please see:
TellTheDetails

In particular we would need to see:
  • You servelet declaration (in web.xml)
  • Your servlet mapping (also in web.xml)
  • The URL that you're using (either in your browser's address window, your hyperlink's href attribute or your form's action attribute)

  • And anything else you think might help someone solve this problem.
    Again, welcome to JavaRanch and good luck with your question.
    -Ben


    Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 42289
        
      64
    Where is the servlet class file located? Which URL are you trying to access? How is the servlet mapped in the web.xml file - post the servlet and servlet-mapping elements.


    Ping & DNS - my free Android networking tools app
    anubha goel
    Greenhorn

    Joined: Jul 06, 2007
    Posts: 22
    hii,
    my web.xml file is:

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE web-app (View Source for full doctype...)>
    <web-app>
    <servlet>
    <servlet-name>colour</servlet-name>
    <servlet-class>colour</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>colour</servlet-name>
    <url-pattern>servlet/colour</url-pattern>
    </servlet-mapping>
    </web-app>
    -------------------
    i hav placed my class file in: C:\apache-tomcat-5.5.23\webapps\anu\WEB-INF\classes\colour

    -----------------------
    i am trying to access my servlet through following URL:
    http://localhost:8080/anu/col.html
    -------------------------------
    my java servlet is :
    COLOUR.JAVA

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class colour extends HttpServlet
    {
    public void service(HttpServletRequest rq,HttpServletResponse rs)throws ServletException, IOException
    {
    PrintWriter out=rs.getWriter();
    String s=rq.getParameter("colour");
    out.println("<body bgcolor="+s+"><H1><font color=red>this is get parameter example</font></H1></body>");
    out.close();
    }
    }
    -----------------------
    my html file is:
    COL.HTML

    <form action="http://localhost:8080/anu/colour" method="Post">
    name of color<input type=text name=color><BR>
    <input type=submit value=set>
    </form>

    Thanks And Regards,
    Anubha.
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 42289
        
      64
    <url-pattern>servlet/colour</url-pattern>

    That should be
    <url-pattern>colour</url-pattern>


    Also, the class file should be in "C:\apache-tomcat-5.5.23\webapps\anu\WEB-INF\classes", not in "C:\apache-tomcat-5.5.23\webapps\anu\WEB-INF\classes\colour".
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

  • Check the url-pattern you have configured in web.xml is different than that of what you access at runtime. Make sure they both are same.
  • The class what you have specified is of default package. Be careful in that case where your actual .class files reside. Have a look at that as well.


  • [ July 30, 2007: Message edited by: Raghavan Muthu ]

    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    anubha goel
    Greenhorn

    Joined: Jul 06, 2007
    Posts: 22
    Hi,
    i made all the changes but still getting the same error-404....i am too buged up..
    please help...

    Thanks and Regards,
    Anubha.
    [ July 30, 2007: Message edited by: anubha goel ]
    William Brogden
    Author and all-around good cowpoke
    Rancher

    Joined: Mar 22, 2000
    Posts: 12806
        
        5
    <servlet-class>colour</servlet-class>


    ALL java classes used in servlets should be in a package and located under \classes using the package name. Failure to do so results in exactly what you are seeing - the servlet container has its own idea of what the "current" directory is.

    Bill
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Yeah, thats correct as william Brodgen said.

    Its always better to keep your files in some specific packages rather than leaving it to the default packages!

    Thanks for pointing out Bill.
    Amol Nayak
    Ranch Hand

    Joined: Oct 26, 2006
    Posts: 218
    Hi anu,

    See this web.xml

    <?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/dtds/web-app_2_3.dtd">

    <web-app>
    <servlet>
    <servlet-name>colour</servlet-name>
    <servlet-class>colour</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>colour</servlet-name>
    <url-pattern>/colour</url-pattern>
    </servlet-mapping>
    </web-app>


    You hould start the url pattern with a / unless its something like

    so it should be /colour and not just color

    Some other suggestions ( These are not causing any problems but just some suggestions)

    -> In the html the action can be just colour and you need not put the whole
    path.

    -> I would suggest you to use use camel case for the class name.
    keep it Colour and not color.

    -> Make sure web.xml is in <webapp>/WEB-INF and class file in
    <webapp>/WEB-INF/classes

    everything else looks fine. I think adding a / in the
    <url-pattern>/colour</url-pattern> tag should be ok.

    Amol..
    anubha goel
    Greenhorn

    Joined: Jul 06, 2007
    Posts: 22
    hii,
    i tried all solutions but nothing is working...
    can somone telme an example so that i can try to run that on my system...
    it mite be helpful...

    Thanks & Regards,
    Anubha.
    Amol Nayak
    Ranch Hand

    Joined: Oct 26, 2006
    Posts: 218
    Hi anu.

    Thats strange, do one thing can you mail me the zip of the files you are using. Let me go through it and see whats goin wrong..

    mail to amolnayak311@gmail.com..

    Amol..
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Originally posted by anubha goel:
    hii,
    i tried all solutions but nothing is working...
    can somone telme an example so that i can try to run that on my system...
    it mite be helpful...

    Thanks & Regards,
    Anubha.


    There is a very simple "hello, world" servlet on my site.
    Look for Simple Servlet.
    http://simple.souther.us
    anubha goel
    Greenhorn

    Joined: Jul 06, 2007
    Posts: 22
    Hi,
    thanks a lot all of you and specially Amol.
    my servlet is running successfully now.

    Thanks & Regards,
    Anubha.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61434
        
      67

    Originally posted by Amol Nayak:
    mail to amolnayak311@gmail.com..


    Amol, in the future please do not take the conversation out of the forums. Please read this for more information.


    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Originally posted by anubha goel:
    Hi,
    thanks a lot all of you and specially Amol.
    my servlet is running successfully now.

    Thanks & Regards,
    Anubha.


    What was it?
    Amol Nayak
    Ranch Hand

    Joined: Oct 26, 2006
    Posts: 218
    Bear, i am aware of it.. I told her to mail me the files so that i can take a look at it.

    It was the problem with web.xml file.. I have already posted the web.xml she needs to use..
    anubha goel
    Greenhorn

    Joined: Jul 06, 2007
    Posts: 22
    Hi,
    i am really very sorry if i prohibited any rules...

    Thanks & Regards,
    Anubha.
    hatim osman
    Ranch Hand

    Joined: Jun 10, 2005
    Posts: 105
    Hi there,

    I gained alot from this post, especially that I suffered similar problems many times. I have a final question though, assuming that the servlet classes reside in the default package, or folder, "classes/", what are the urls that correspond to the following mappings, particularly when trying to invoke either servlet through a relative url:



    and


    Thank you very much in advance
    Hatim
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61434
        
      67

    assuming that the servlet classes reside in the default package


    Such a practice is strongly discouraged.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: unable to run servlets