Link to home
Start Free TrialLog in
Avatar of cakurien
cakurien

asked on

not able to run a servlet

the steps i did to run a servlet HelloWWW.java is given below but I get Http 404 error

I have set the CLASSPATH to servlet-api.jar and got it compiled
1.The HelloWWW class file is in HOME\webapps\shop\shops\WebRoot\WEB-INF\classes.

2. in web.xml I have made the flg changes
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
      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
      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>anyname</servlet-name>
<servlet-class>HelloWWW</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>anyname</servlet-name>
<url-pattern>/locator</url-pattern>
</servlet-mapping>

</web-app>

3.when I run the flg path in my browser I get the flg error
http://localhost:8080/shop/shops/WebRoot/locator
ERRORHTTP Status 404 - /shop/shops/WebRoot/locator file not available

Please help
Avatar of Mayank S
Mayank S
Flag of India image

Try adding your class to a package, like mypackage.HelloWWW

Then use: <servlet-class>mypackage.HelloWWW</servlet-class>

Also, the HelloWWW class-file will go under the classes\mypackage folder.
ASKER CERTIFIED SOLUTION
Avatar of Siva Prasanna Kumar
Siva Prasanna Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>http://localhost:8080/shop/shops/WebRoot/locator

is this correct???

what's your Web Project name??? (shop???)

try http://localhost:8080/shop/locator

Thank You.
Avatar of leobaz2
leobaz2

Your appserver is will first try to match your web application context before matching your servlet path.  Therefore, if you application context is "shop", then the app server will try to match the rest of your url to a servlet.  The rest of the url (without shop) for the url you tried is now "/shops/WebRoot/locator".  Since this has no servlet mapping it will try "/shops/WebRoot" and then "/shops".  None of these have url mapping to servlets.  Therefore, you should try the url mentioned above.

http://localhost:8080/shop/locator

Other urls such as http://localhost:8080/shop/locator/something/something should work also.
Please tell us which web app server you are using.  Is it Tomcat ?  I will presume so.
Anyway your file structure is wrong.  
>HOME\webapps\shop\shops\WebRoot\WEB-INF\classes.
The WEB-INF directory must be a subdirectory of webapps. Otherwise Tomcat will not even recognize it as a web app. You can see this if you use Tomcat's manager( ask us if need help setting it up).    So, if  WebRoot is your context then  put  your HelloWWW class file  into  
HOME\webapps\WebRoot\WEB-INF\classes.   and then use   http://localhost:8080/WebRoot/locator  in your browser.    
As suggested by mayankeagle it is good practice to use packages. But they are not required( see first link posted by shivaspk) for servlets. Packages are required for javabeans but that another lesson.   rrz
>The WEB-INF directory must be a subdirectory of webapps  
I am sorry. I meant to say that your WEB-INF must be a subdirectory of your context. Your context is a subdirectory of webapps.