File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes First helloWorld servlet program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "First helloWorld servlet program " Watch "First helloWorld servlet program " New topic
Author

First helloWorld servlet program

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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"?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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

Joined: Apr 27, 2012
Posts: 168
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: First helloWorld servlet program