aspose file tools*
The moose likes Servlets and the fly likes HTTP Status 404 - /HelloWorld Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "HTTP Status 404 - /HelloWorld" Watch "HTTP Status 404 - /HelloWorld" New topic
Author

HTTP Status 404 - /HelloWorld

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

hi ranchers ,

i have html form (index.html) which accepts the name from user




and the servlet file HelloWorld.java retrieve name of user and displays back to user



when i submit the html form i got HTTP Status 404 - /HelloWorld

note that i have not created any web.xml file

what is the problem here ?



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

In HTML, / means the root of the server, not the root of the web application. Your action is looking one level too high.

Also, servlets need a web.xml file with servlet and servlet-mapping entries. Without these servlets will never work.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Where is your servlet class located? My advice to you would be to make sure your servlet class is in a package, and put an entry that maps to your servlet in web.xml.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


here is web.xml under \WEB-INF dir.



Still getting same error.


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

If you are not going to pay attention to the replies, why are you posting in the first place?

Bosun Bello wrote:My advice to you would be to make sure your servlet class is in a package


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

Reason for not using package was that the servlet container will look for servlet in \WEB-INF\class directory by default.

but nervertheless i have placed HelloWorld in org package

so 1 line of code is changed in web.xml



also in index.html



Still
HTTP Status 404 - /SESSION/org.HelloWorld

Khalil Salman
Greenhorn

Joined: Sep 05, 2010
Posts: 11
Change action=”org.HelloWorld” to action=”poi”. Because poi is the name of your HelloWorld servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

naveen yadav wrote: Reason for not using package was that the servlet container will look for servlet in \WEB-INF\class directory by default.

While that is the base of the hierarchy, the classes must be in an explicit package. As of Java 1.4, classes in the unnamed default package cannot be imported.

You can argue with us, or you can learn how to do things correctly. The choice is yours.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

Khalil Salman wrote:Change action=”org.HelloWorld” to action=”poi”. Because poi is the name of your HelloWorld servlet.

While that is a step in the right direction, the action must be prefixed with the context path to ensure that it will work in all environments.
Daniel Val
Ranch Hand

Joined: Jan 09, 2012
Posts: 44
naveen yadav wrote:
here is web.xml under \WEB-INF dir.



Still getting same error.




- url pattern must be something /HellowWorld/* something like that, not what you have there; I suggest you use url patter *.something, then you change the action of your form to whatever.something and the url pattern of the servlet to *.something - please put whatever you want instead of that. I don't like url patterns that look like that /whatever/* - it is nothing wrong with them but I had issues in the past.

- in WEB-INF/classes (note the name is slightly different than what you got) you gotta put the compiled *.class file not the Java file. No issue if .java and .class are in the same folder although this is a bad practice, but you have to compile it first.

- root package is ok, should work. It is a (severely) discouraged practice, but for testing you should be fine.

D.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

Daniel Val wrote:url pattern must be something /HellowWorld/* something like that, not what you have there

Nope. It can be what he has there -- though I would strongly advise against using ".html"

I don't like url patterns that look like that /whatever/* - it is nothing wrong with them but I had issues in the past.

There should be no issues and it is a more modern approach than the old-fashioned *.do patterns. If you had issues it was not due to the type of pattern used.

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

Bear Bibeault wrote:

You can argue with us, or you can learn how to do things correctly. The choice is yours.


i was not arguing. i was explaining the reason why i use the default package there. that was a mistake and have learned to put class always in a package.


It's always helpful to learn from your mistakes because then your mistakes seem worthwhile.
Garry Marshall
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


so finally it worked after doing some changes.


change in web.xml instead of using /index.html



in index.html file instead of using the /HelloWorld



thank you guys.
Daniel Val
Ranch Hand

Joined: Jan 09, 2012
Posts: 44
Bear Bibeault wrote:
Daniel Val wrote:url pattern must be something /HellowWorld/* something like that, not what you have there

Nope. It can be what he has there -- though I would strongly advise against using ".html"


No, it is not that, please see his example:
- he's got a html page
- the form defined in it is with action="/HelloWorld"
- when he submits the form he wants to go to the servlet

So the servlet must be installed at the url pattern /HelloWorld/... that's what I was saying...

Bear Bibeault wrote:
I don't like url patterns that look like that /whatever/* - it is nothing wrong with them but I had issues in the past.

There should be no issues and it is a more modern approach than the old-fashioned *.do patterns. If you had issues it was not due to the type of pattern used.



True, and of course now I don't have issues with any of the approaches, but for the good old time sake I prefer the second one *.something...


D
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


index.html -----invokes------->org.Hello //working fine

first.html<--------response-------or.Hello //working fine

the mapping for the above flow control




first.html----------invoke---------->org.FirstQue ............//problem first.html could not invoke the servlet FirstQue,


the mapping for above flow of control is




and error is HTTP Status 404 - /ProjSession/org/first

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Your form action is "hello". That must mean you must have a url-mapping for "hello". You don't. Therefore it fails.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

One more time: the form action should be the servlet context followed by the servlet mapping.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


Bear you will be surprised to see , in following code ACTION have servlet context followed by url mapping BUT giving Http-404





But following code is working fine
web.xml



the strange behavior may be because for the mapping of index.html(which is welcome file for the context)









 
 
subject: HTTP Status 404 - /HelloWorld