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\We bRoot\WEB- INF\classe s.
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</ser vlet-name>
<servlet-class>HelloWWW</s ervlet-cla ss>
</servlet>
<servlet-mapping>
<servlet-name>anyname</ser vlet-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/locato r file not available
Please help
I have set the CLASSPATH to servlet-api.jar and got it compiled
1.The HelloWWW class file is in HOME\webapps\shop\shops\We
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</ser
<servlet-class>HelloWWW</s
</servlet>
<servlet-mapping>
<servlet-name>anyname</ser
<url-pattern>/locator</url
</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/locato
Please help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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.
is this correct???
what's your Web Project name??? (shop???)
try http://localhost:8080/shop/locator
Thank You.
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.
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\W ebRoot\WEB -INF\class es.
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-I NF\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
Anyway your file structure is wrong.
>HOME\webapps\shop\shops\W
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-I
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.
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.
Then use: <servlet-class>mypackage.H
Also, the HelloWWW class-file will go under the classes\mypackage folder.