aspose file tools*
The moose likes Servlets and the fly likes servlet not available Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet not available" Watch "servlet not available" New topic
Author

servlet not available

J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1066
    
  13

After a couple of hours reading about servlet mappings, I still can't figure this one out. (yes, I'm a greenhorn).

I have two pages using the same servlet. The page using this code works just fine.


But the page that submits a form using this code results in "The requested resource (/ProdGoalsServlet) is not available."


I realize the first is doing a GET and the second is doing a POST, but I've overridden both methods and have a breakpoint set on each one. The code is never reaching the breakpoint.

A hint, please?

"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

How did you map the URL to your Servlet? Can you post your Annotations you used on your Servlet or you web.xml file? Have you also verified that your Servlet's .class file is in the proper location for your web app?


OCJP6, OCWCD5
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1066
    
  13

With a better google search I found the answer. The code in the second page has to modified like so:


So now the question is why? Almost all the examples I found use the same format as my original post. Is the problem with my servlet mapping?

Here's the entry in web.xml:


Any and all enlightenment is welcome. Thanks.
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

Another way would be to use the c:url tag in JSTL to create your URLs.

This code is a little cleaner and it give you the option to reuse the url later in the page is you want. TO enable JSTL all you have to do is copy the JSTL jar files into you lib directory.

The reason your previous code was not working was that the URL did not include your web app name. Your Servlet's real url is www.host:8080/yourProjectName/ProdGoalsServlet , but your code was omitting the ProdGoalsServet part and linking to www.host:8080/ProdGoalsServlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Without the context path, how would the container know which web application the request is targeted to?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1066
    
  13

Thanks for the tip, Mike, I'll use that. I'd still like to know the why of it all.

Bear, you are exactly right and that seems obvious now, so why do so many examples like this one...

http://www.servletworld.com/servlet-tutorials/servlet-form-processing.html

...leave out that critical detail? Is it the way they map the servlets?

Sorry for questions that seem to have obvious answers, but this is all part of my process of learning JSTL and EL. I inherited a boatload of ugly scriptlet code, so as I'm making changes and additions I'm writing it all with "real" jsp and an MVC model with servlets and DAOs. The webserver here had NO servlets running at all. It makes for a long, steep learning curve, but I'll prevail...eventually.

Thanks again. This site is an invaluable resource. I'm learning the fastest way to find an answer with google is with "site:coderanch.com".
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

Jk Robbins wrote:
http://www.servletworld.com/servlet-tutorials/servlet-form-processing.html

...leave out that critical detail? Is it the way they map the servlets?


If you take a close look at the following line in the tutorial

Note that the value inside the action attribute does not start with a slash. In this case, it is providing a relative url. Since your Servlet started with a slash, it is an absolute url.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Mike Zal wrote:In this case, it is providing a relative url. Since your Servlet started with a slash, it is an absolute url.

Not quite.

Absolute URLs start with the protocol; e.g. http:

Page-relative URLs start with no slash or protocol. These are fragile and should always be avoided.

Server-relative URLs start with the context path and are the preferred format to use in web apps.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Here's the Ranch FAQ entry on that specific question: RelativeLinks. I wish I had read it before writing a web application, now I have a whole lot of places where the context name is hard-coded in my links. Cleaning that up is high in my list of refactoring issues.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1066
    
  13

Bear Bibeault wrote:
Mike Zal wrote:In this case, it is providing a relative url. Since your Servlet started with a slash, it is an absolute url.

Not quite.

Absolute URLs start with the protocol; e.g. http:

Page-relative URLs start with no slash or protocol. These are fragile and should always be avoided.

Server-relative URLs start with the context path and are the preferred format to use in web apps.


Ah, at last the light bulb goes on. That's what I was looking for. Thanks again, everyone.
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

Bear, thank you for that correction. I've been doing mostly self study for web/Java EE programming so it's nice to be corrected when using the wrong terminology.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet not available