www.webdeveloper.com
Results 1 to 11 of 11

Thread: Cannot access servlet through jsp page

  1. #1
    Join Date
    Mar 2006
    Posts
    10

    Question Cannot access servlet through jsp page

    Hello everyone.

    I'm new to working with Tomcat or jsp and servlets. In my jsp file is a form which calls out to a servlet like so: <form name="order" action="/trial/Ecom/ChocServlet" method="POST"> ..and this is fairly common I spose.

    The compiled class file lies beside the jsp file i.e. in tha SAME folder as the jsp file. But when I try to access it through the jsp page, it give me an error "The requested resource (/trial/Ecom/ChocServlet) is not available."

    Do I have to do something like servlet mapping in the web.xml file? Is that whats wrong? I dont understand how to do this whole mapping thingie.. I have tried going through many many pages looking for how to fix this. And have tried searching through thi forum as well. Help on how to get my servlet to work would be GREATLY appreciated.

    THanks.

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    servlet must be in WEB-INF/classes folder as well as there has to be a web.xml file in the WEB-INF folder that points to the servlet.....
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Mar 2006
    Posts
    10
    I honestly cannot understand how to make the web.xml file 'point' to the servlet! Could you please explain it to me? <servlet-name>.. <url-pattern>, nothing really makes sense!!

    Thanks in advance..

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    ok this will take some time..but here it is. I will explain first the tomcat directory structure and then deploying a servlet on tomcat as well.

    1. Download tomcat from here
    2. Install tomcat. On my windows 2000 box after installation the tomcat root dorectory is at this location
    C:\tomcat5.5.15
    It may be different on your machine.
    if you go in the root directory, there are some directories you want to know to be the least. Mind you there are several more directories in there but these are which u may be working with mostly.

    Code:
    C:\tomcat5.5.15
    ----------------\conf ----> it has server..xml file that contains configurations
    ----------------\webapps ----> this is where a new application may be dropped.(A complete webapplication contains a root folder, WEB-INF folder, classes folder and a web.xml file)

    if you go to webapps folder, you will see that it has a folder named "ROOT", this is the folder where there are default html pages that are served when u start your tomcat server. jsp's could be here or in any other web application folders.
    For a simple testing of a servlet you can use the default web-inf folder that is in the ROOT folder.
    Here is example jsp page, a servlet and part of the web.xml file for our testing.
    mytest.jsp
    mytestServlet.class
    for this testing I will place mytest.jsp in the ROOT folder and servlet in the classes folder like below

    C:\tomcat5.5.15\webapps\ROOT
    ------------------------------\mytest.jsp
    ------------------------------\WEB-INF\web.xml
    ------------------------------\WEB-INF\classes\mytestServlet.class


    Now make sure that you have exact structure as above( ofcourse file names could be the files names that you actually have on your machine)
    Here is the entry that you require in the web.xml to access this servlet.

    Code:
    <servlet>
        <servlet-name>mytestServlet</servlet-name>
        <display-name>mytestServlet</display-name>
        <servlet-class>mytestServlet.class</servlet-class>
    </servlet>
    	
    <servlet-mapping>
        <servlet-name>mytestServlet</servlet-name>
        <url-pattern>/servlets/mytestServlet</url-pattern>
    </servlet-mapping>
    See in the first part in element servlet, you are letting the server know that there is a servlet that u want to use, therefore server will recognise that name.<servlet-name>, this value must always be the same as in url-mapping elements <servlet-name> element.
    Then you must tell tomcat that where this file is at, i n this case we know its here,
    ------------------------------\WEB-INF\classes\mytestServlet.class
    so since there is no package the path to this class file ins as below

    <servlet-class>mytestServlet.class</servlet-class>

    Now we need to set the mapping for the servlet so that we can tell tomcat how the above servlet may be accessed.
    The first element is the servlet name which points to a class file in the tomcats web-inf dir
    <servlet-name>mytestServlet</servlet-name>
    The second element is the pattern which you want to type in the browser window to access this servlet.For our purposes we can say the url-pattern is as below

    <url-pattern>/servlets/mytestServlet</url-pattern>

    there you go, Now reboot the tomcat.
    And you can access the servlet by typing the following in the browser window (tomcat installation by default sets the port:8080 for itself)

    From Browser.
    http://localhost:8080/servlets/mytestServlet

    or as in your case your forms action elements value
    action="/servlets/mytestServlet"


    Hope this helps...good luck
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  5. #5
    Join Date
    Mar 2006
    Posts
    10
    Wow I understand it now!

    I did exactly like you said, but it gives we the same error that the resource is not available.

    So I thought of uncommenting only the part which -I'd- written into the file. THen it gave me this:

    ---------------------------------------------------
    '
    '
    '
    exception

    javax.servlet.ServletException: Wrapper cannot find servlet class HelloServlet.class or a class it depends on
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
    org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProt ocol.java:667)
    org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
    org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
    org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
    java.lang.Thread.run(Unknown Source)


    root cause

    java.lang.ClassNotFoundException: HelloServlet.class
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1352)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1198)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
    org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProt ocol.java:667)
    org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
    org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
    org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
    java.lang.Thread.run(Unknown Source)


    note The full stack trace of the root cause is available in the Apache Tomcat/5.5.15 logs.
    -----------------------------------------------------------

    Should I make a new web.xml file? for each new servlet?

    This was for the test servlet.
    The structure for my main folder is like this..

    Tomcat5.5\webapps\Ecom -->with my jsps n htmls in here
    Tomcat5.5\webapps\Ecom\WEB-INF\classes\ChocServlet.class -->u get the idea.

    So in this case, can the <servlet-class> still be ChocServlet.class?

    But my test servlet isnt working either. Any idea what I might be doing wrong? THanks for ur time..

  6. #6
    Join Date
    Mar 2006
    Posts
    10
    Someone... anyone!

  7. #7
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    post the web.xml part here both servlet and mapping elements.
    Second post the locations where jsps are and where web-inf is and where web.xml is and where the servlet is.
    Seems like the path to servlet is wrong in web.xml
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  8. #8
    Join Date
    Mar 2006
    Posts
    10
    well, i uninstalled everything. and reinstalled it all (for the gazillionth time.. ok ok maybe the 3th time) :P and did everythign exactly the way i'd been doing it for the past two days.. (exactly how uve laid down up there) but this time i didnt forget to ask Allah and it worked!! subhaaanallah..

    thanks again for all ur help. it cleared everything up.

  9. #9
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    lol....glad to help....however...programming is black and white there is no gray area where you need to say duaa for it..anyways whatever makes you happy and glad to be of any help
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  10. #10
    Join Date
    Mar 2006
    Posts
    10
    i'm afraid you're mistaken and ull realise one day that grey areas DONT EXIST. "innahu 3ala kulli shai2in qadeer" what if i forget one darned semicolon, over and over again. no black and white eye could find that if that is not His Will programming may be black n white, and if thats the case, then i have a splash of colours with me! take care!

  11. #11
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    whatever makes u happy my friend..(as I said before)..good luck..:-)
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles