• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

First helloWorld servlet program

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to make my first java servlet.
Its a simple hello world program. A button is clicked and this takes you to a new page displaying the message hello world.
When i run it i get an error page HTTP 404
The requested resource () is not available.
So i assume there is something wrong with my code.

Start page



Servlet


XML
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your form action is prefixed with "/servlets". That's not part of the mapping in the deployment descriptor. Why is it there?

Your form action should be prefixed with the context path. I assume that that's not "/servlets"?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, I realize that this is a learning exercise, but in the real world, servlets should never be used to emit HTML.
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have my servlet in a package named servlets in netbeans. I wrongly thought i would navigate to it using /servlets/ . .
It has worked now i just have the servlet name.
I have also just changed it to out.println("message"); instead of the html
Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tom davies wrote:I have my servlet in a package named servlets in netbeans. I wrongly thought i would navigate to it using /servlets/ . .

Right -- the package has nothing to do with the URL mapping.

It has worked now i just have the servlet name.

Warning! This will only work when the web app is the root web app. You should always fetch and include the context path when forming URLs in web pages (such as for images and form actions). For the root web app, the context path just happens to be the empty string. Your app will break if the app is depoyed using a differnt context path.

See the JspFaq for how to fetch the context path in a JSP.

I have also just changed it to out.println("message"); instead of the html

Emitting HTML is fine while you are learning. I just wanted to make sure it did not become a bad habit that you'll need to unlearn later.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic