0

I have placed my HTML page in webapps\root\WebForte folder of my Tomcat. I want to open a servlet page webapps\classes\HelloWorld.

I have already mentioned the following in web.xml:

<servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>HeloWorld</servlet-class>
 </servlet>
 <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/classes/HelloWorld</url-pattern>
 </servlet-mapping>

and called the class from the following

<form  id="docContainer" action = "HelloWorld" method="POST">

I am getting HTTP Status 404 - /WebForte/HelloWorld error. Can anyone please help me solve this issue?

halfer
  • 19,824
  • 17
  • 99
  • 186
Yash
  • 1
  • 3
  • 3
    Do a google search on the meaning of `url-pattern`. – Sotirios Delimanolis Oct 08 '13 at 19:18
  • shouldnt you place your application under webapps\WebForte ? – Frederic Close Oct 08 '13 at 19:19
  • Or, read our servlets tag wiki page for an extensive Hello World example. Hover the `[servlets]` tag which you placed on the question until a black info box shows up and then click therein the *info* link. – BalusC Oct 08 '13 at 19:19
  • Hello Frederic, I tried the same but still the error is same. even though i have mapped the page on web.xml. I want to keep all the classes in the same page so that it can be on different position. – Yash Oct 08 '13 at 19:23

2 Answers2

0

I don't think you need the root directory,just place your webapp under "webapps"

According to your web.xml to access yor servlet you need this url:

/classes/HelloWorld

In other words whenever tomcat sees "/classes/HelloWorld" it maps it to your servlet and sends the request there.

My guess is that according the 404 error your url should be /WebForte/classes/HelloWorld

Also keep in mind that "/classes/HelloWorld" is somthing arbitrary, you put wahtever you like there

To figure out what's going on try the url from a browser

The directory structure should look like this:

webapps     
| 
- WebForte
  |
  - WEB-INF
    |
     -web.xml
     -classes
       |
         -HelloWorld.class
Spiff
  • 3,873
  • 4
  • 25
  • 50
  • Hi, thanks for the reply, I tried what you have mentioned in your reply but the wrror message not shows just "HTTP Status 404 -". I hope something is not wrong with my tomcat7 version or maybe the code I have written in my servlet page? Thanks in advance. – Yash Oct 08 '13 at 19:58
  • try http://localhost:8008 in yr browser does it work? – Spiff Oct 08 '13 at 20:20
  • Yes it works, Even my index page works at http://localhost:8080/WebForte/ works properly. but on submit button it shows 404 error – Yash Oct 08 '13 at 20:26
  • try http://localhost:8080/WebForte/classes/HelloWorld in yr browser, does it work? – Spiff Oct 09 '13 at 04:30
  • HTTP Status 500 - Error instantiating servlet class HeloWorld is the error that comes up when i put http://localhost:8080/WebForte/classes/HelloWorld in url – Yash Oct 09 '13 at 05:31
  • Is your directory structure like the one above in the answer? – Spiff Oct 09 '13 at 05:37
  • Yes i have used the same path now. but still the same issue. – Yash Oct 09 '13 at 05:41
  • That's not correct. It should be C:\tomcat-7\webapps\WebForte\WEB-INF\classes\HelloWorld.class – Spiff Oct 09 '13 at 05:43
  • Yes, I have just tried this path also. but this HelloWorld doesnt seem to work at all – Yash Oct 09 '13 at 05:48
0

you need put the parameter <display-name> in your definition exactly like this:

 <servlet>
        <display-name>HelloWorld</display-name>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>com.yourpackage.servlet.HelloWorld</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/HelloWorld</url-pattern>
    </servlet-mapping>

and your servlet would be visible to request.

  • Hi Christian, Thanks a lot for your reply, I tried it with Display name also but it didnt work. – Yash Oct 08 '13 at 19:59