• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

First helloWorld servlet program

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Maybe he went home and went to bed. And took this tiny ad with him:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic