Cannot access servlet through jsp page
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.
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.....
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..
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
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.
----------------\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.
for this testing I will place mytest.jsp in the ROOT folder and servlet in the classes folder like below
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.
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,
so since there is no package the path to this class file ins as below
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
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
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)
or as in your case your forms action elements value
Hope this helps...good luck
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:
javax.servlet.ServletException: Wrapper cannot find servlet class HelloServlet.class or a class it depends on
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..
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
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.
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
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!
whatever makes u happy my friend..(as I said before)..good luck..:-)
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)